From ea88ff1214beb3451916a8385962322cfd80237e Mon Sep 17 00:00:00 2001 From: Orh <71973546+O4h@users.noreply.github.com> Date: Sun, 15 May 2022 20:33:16 +0200 Subject: [PATCH] feat: add proxy support (#45) (#46) * added `proxy` and `proxy_auth` parameters for `DiscordOauthClient` * all requests now use these proxy parameters --- fastapi_discord/client.py | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/fastapi_discord/client.py b/fastapi_discord/client.py index f023555..1efe212 100644 --- a/fastapi_discord/client.py +++ b/fastapi_discord/client.py @@ -74,13 +74,27 @@ class DiscordOAuthClient: Discord application client secret. redirect_uri: Discord application redirect URI. + proxy: + Optional proxy url + proxy_auth: + Optional aiohttp.BasicAuth proxy authentification """ - def __init__(self, client_id, client_secret, redirect_uri, scopes=("identify",)): + def __init__( + self, + client_id, + client_secret, + redirect_uri, + scopes=("identify",), + proxy=None, + proxy_auth: aiohttp.BasicAuth = None, + ): self.client_id = client_id self.client_secret = client_secret self.redirect_uri = redirect_uri self.scopes = "%20".join(scope for scope in scopes) + self.proxy = proxy + self.proxy_auth = proxy_auth self.client_session: aiohttp.ClientSession = aiohttp.ClientSession() def get_oauth_login_url(self, state: Optional[str] = None): @@ -104,10 +118,20 @@ async def request(self, route: str, token: str = None, method: Literal["GET", "P if token: headers = {"Authorization": f"Bearer {token}"} if method == "GET": - async with self.client_session.get(f"{DISCORD_API_URL}{route}", headers=headers) as resp: + async with self.client_session.get( + f"{DISCORD_API_URL}{route}", + headers=headers, + proxy=self.proxy, + proxy_auth=self.proxy_auth, + ) as resp: data = await resp.json() elif method == "POST": - async with self.client_session.post(f"{DISCORD_API_URL}{route}", headers=headers) as resp: + async with self.client_session.post( + f"{DISCORD_API_URL}{route}", + headers=headers, + proxy=self.proxy, + proxy_auth=self.proxy_auth, + ) as resp: data = await resp.json() else: raise Exception("Other HTTP than GET and POST are currently not Supported") @@ -118,7 +142,12 @@ async def request(self, route: str, token: str = None, method: Literal["GET", "P return data async def get_token_response(self, payload: PAYLOAD) -> TokenResponse: - async with self.client_session.post(DISCORD_TOKEN_URL, data=payload) as resp: + async with self.client_session.post( + DISCORD_TOKEN_URL, + data=payload, + proxy=self.proxy, + proxy_auth=self.proxy_auth, + ) as resp: return await resp.json() async def get_access_token(self, code: str) -> Tuple[str, str]: