1
- import asyncio
2
1
from typing import Dict , List , Optional , Tuple , Union
3
2
4
3
import aiohttp
5
- import nest_asyncio
6
4
from aiocache import cached
7
5
from fastapi import Depends , Request
8
6
from fastapi .security import HTTPAuthorizationCredentials , HTTPBearer
@@ -72,7 +70,7 @@ class DiscordOAuthClient:
72
70
scopes : str
73
71
proxy : Optional [str ]
74
72
proxy_auth : Optional [aiohttp .BasicAuth ]
75
- client_session : aiohttp .ClientSession
73
+ client_session : aiohttp .ClientSession = None
76
74
77
75
"""Client for Discord Oauth2.
78
76
@@ -93,24 +91,27 @@ class DiscordOAuthClient:
93
91
"""
94
92
95
93
def __init__ (
96
- self ,
97
- client_id ,
98
- client_secret ,
99
- redirect_uri ,
100
- scopes = ("identify" ,),
101
- proxy = None ,
102
- proxy_auth : aiohttp .BasicAuth = None ,
94
+ self ,
95
+ client_id ,
96
+ client_secret ,
97
+ redirect_uri ,
98
+ scopes = ("identify" ,),
99
+ proxy = None ,
100
+ proxy_auth : aiohttp .BasicAuth = None ,
103
101
):
104
102
self .client_id = client_id
105
103
self .client_secret = client_secret
106
104
self .redirect_uri = redirect_uri
107
105
self .scopes = "%20" .join (scope for scope in scopes )
108
106
self .proxy = proxy
109
107
self .proxy_auth = proxy_auth
110
- nest_asyncio .apply ()
111
- asyncio .get_running_loop ().run_until_complete (asyncio .create_task (self .initialize ()))
112
108
113
- async def initialize (self ):
109
+ async def init (self ):
110
+ """
111
+ Initialized the connection to the discord api
112
+ """
113
+ if self .client_session is not None :
114
+ return
114
115
self .client_session = aiohttp .ClientSession ()
115
116
116
117
def get_oauth_login_url (self , state : Optional [str ] = None ):
@@ -135,18 +136,18 @@ async def request(self, route: str, token: str = None, method: Literal["GET", "P
135
136
headers = {"Authorization" : f"Bearer { token } " }
136
137
if method == "GET" :
137
138
async with self .client_session .get (
138
- f"{ DISCORD_API_URL } { route } " ,
139
- headers = headers ,
140
- proxy = self .proxy ,
141
- proxy_auth = self .proxy_auth ,
139
+ f"{ DISCORD_API_URL } { route } " ,
140
+ headers = headers ,
141
+ proxy = self .proxy ,
142
+ proxy_auth = self .proxy_auth ,
142
143
) as resp :
143
144
data = await resp .json ()
144
145
elif method == "POST" :
145
146
async with self .client_session .post (
146
- f"{ DISCORD_API_URL } { route } " ,
147
- headers = headers ,
148
- proxy = self .proxy ,
149
- proxy_auth = self .proxy_auth ,
147
+ f"{ DISCORD_API_URL } { route } " ,
148
+ headers = headers ,
149
+ proxy = self .proxy ,
150
+ proxy_auth = self .proxy_auth ,
150
151
) as resp :
151
152
data = await resp .json ()
152
153
else :
@@ -159,10 +160,10 @@ async def request(self, route: str, token: str = None, method: Literal["GET", "P
159
160
160
161
async def get_token_response (self , payload : PAYLOAD ) -> TokenResponse :
161
162
async with self .client_session .post (
162
- DISCORD_TOKEN_URL ,
163
- data = payload ,
164
- proxy = self .proxy ,
165
- proxy_auth = self .proxy_auth ,
163
+ DISCORD_TOKEN_URL ,
164
+ data = payload ,
165
+ proxy = self .proxy ,
166
+ proxy_auth = self .proxy_auth ,
166
167
) as resp :
167
168
return await resp .json ()
168
169
0 commit comments