Skip to content

Commit ea88ff1

Browse files
authored
feat: add proxy support (#45) (#46)
* added `proxy` and `proxy_auth` parameters for `DiscordOauthClient` * all requests now use these proxy parameters
1 parent 3138887 commit ea88ff1

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

fastapi_discord/client.py

+33-4
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,27 @@ class DiscordOAuthClient:
7474
Discord application client secret.
7575
redirect_uri:
7676
Discord application redirect URI.
77+
proxy:
78+
Optional proxy url
79+
proxy_auth:
80+
Optional aiohttp.BasicAuth proxy authentification
7781
"""
7882

79-
def __init__(self, client_id, client_secret, redirect_uri, scopes=("identify",)):
83+
def __init__(
84+
self,
85+
client_id,
86+
client_secret,
87+
redirect_uri,
88+
scopes=("identify",),
89+
proxy=None,
90+
proxy_auth: aiohttp.BasicAuth = None,
91+
):
8092
self.client_id = client_id
8193
self.client_secret = client_secret
8294
self.redirect_uri = redirect_uri
8395
self.scopes = "%20".join(scope for scope in scopes)
96+
self.proxy = proxy
97+
self.proxy_auth = proxy_auth
8498
self.client_session: aiohttp.ClientSession = aiohttp.ClientSession()
8599

86100
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
104118
if token:
105119
headers = {"Authorization": f"Bearer {token}"}
106120
if method == "GET":
107-
async with self.client_session.get(f"{DISCORD_API_URL}{route}", headers=headers) as resp:
121+
async with self.client_session.get(
122+
f"{DISCORD_API_URL}{route}",
123+
headers=headers,
124+
proxy=self.proxy,
125+
proxy_auth=self.proxy_auth,
126+
) as resp:
108127
data = await resp.json()
109128
elif method == "POST":
110-
async with self.client_session.post(f"{DISCORD_API_URL}{route}", headers=headers) as resp:
129+
async with self.client_session.post(
130+
f"{DISCORD_API_URL}{route}",
131+
headers=headers,
132+
proxy=self.proxy,
133+
proxy_auth=self.proxy_auth,
134+
) as resp:
111135
data = await resp.json()
112136
else:
113137
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
118142
return data
119143

120144
async def get_token_response(self, payload: PAYLOAD) -> TokenResponse:
121-
async with self.client_session.post(DISCORD_TOKEN_URL, data=payload) as resp:
145+
async with self.client_session.post(
146+
DISCORD_TOKEN_URL,
147+
data=payload,
148+
proxy=self.proxy,
149+
proxy_auth=self.proxy_auth,
150+
) as resp:
122151
return await resp.json()
123152

124153
async def get_access_token(self, code: str) -> Tuple[str, str]:

0 commit comments

Comments
 (0)