Skip to content

Commit

Permalink
feat: add proxy support (#45) (#46)
Browse files Browse the repository at this point in the history
* added `proxy` and `proxy_auth` parameters for `DiscordOauthClient`
* all requests now use these proxy parameters
  • Loading branch information
O4h authored May 15, 2022
1 parent 3138887 commit ea88ff1
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions fastapi_discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")
Expand All @@ -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]:
Expand Down

0 comments on commit ea88ff1

Please sign in to comment.