22
33import datetime
44import json
5- import typing
65
76import httpx
87import starlette .requests
@@ -17,7 +16,7 @@ async def fetch_bearer_token(code: str, redirect: str, *, refresh: bool) -> dict
1716 data = {
1817 "client_id" : constants .OAUTH2_CLIENT_ID ,
1918 "client_secret" : constants .OAUTH2_CLIENT_SECRET ,
20- "redirect_uri" : f"{ redirect } /callback"
19+ "redirect_uri" : f"{ redirect } /callback" ,
2120 }
2221
2322 if refresh :
@@ -27,9 +26,13 @@ async def fetch_bearer_token(code: str, redirect: str, *, refresh: bool) -> dict
2726 data ["grant_type" ] = "authorization_code"
2827 data ["code" ] = code
2928
30- r = await client .post (f"{ constants .DISCORD_API_BASE_URL } /oauth2/token" , headers = {
31- "Content-Type" : "application/x-www-form-urlencoded"
32- }, data = data )
29+ r = await client .post (
30+ f"{ constants .DISCORD_API_BASE_URL } /oauth2/token" ,
31+ headers = {
32+ "Content-Type" : "application/x-www-form-urlencoded" ,
33+ },
34+ data = data ,
35+ )
3336
3437 r .raise_for_status ()
3538
@@ -38,9 +41,12 @@ async def fetch_bearer_token(code: str, redirect: str, *, refresh: bool) -> dict
3841
3942async def fetch_user_details (bearer_token : str ) -> dict :
4043 async with httpx .AsyncClient () as client :
41- r = await client .get (f"{ constants .DISCORD_API_BASE_URL } /users/@me" , headers = {
42- "Authorization" : f"Bearer { bearer_token } "
43- })
44+ r = await client .get (
45+ f"{ constants .DISCORD_API_BASE_URL } /users/@me" ,
46+ headers = {
47+ "Authorization" : f"Bearer { bearer_token } " ,
48+ },
49+ )
4450
4551 r .raise_for_status ()
4652
@@ -52,15 +58,17 @@ async def _get_role_info() -> list[models.DiscordRole]:
5258 async with httpx .AsyncClient () as client :
5359 r = await client .get (
5460 f"{ constants .DISCORD_API_BASE_URL } /guilds/{ constants .DISCORD_GUILD } /roles" ,
55- headers = {"Authorization" : f"Bot { constants .DISCORD_BOT_TOKEN } " }
61+ headers = {"Authorization" : f"Bot { constants .DISCORD_BOT_TOKEN } " },
5662 )
5763
5864 r .raise_for_status ()
5965 return [models .DiscordRole (** role ) for role in r .json ()]
6066
6167
6268async def get_roles (
63- database : Database , * , force_refresh : bool = False
69+ database : Database ,
70+ * ,
71+ force_refresh : bool = False ,
6472) -> list [models .DiscordRole ]:
6573 """
6674 Get a list of all roles from the cache, or discord API if not available.
@@ -86,23 +94,26 @@ async def get_roles(
8694 if len (roles ) == 0 :
8795 # Fetch roles from the API and insert into the database
8896 roles = await _get_role_info ()
89- await collection .insert_many ({
90- "name" : role .name ,
91- "id" : role .id ,
92- "data" : role .json (),
93- "inserted_at" : datetime .datetime .now (tz = datetime .timezone .utc ),
94- } for role in roles )
97+ await collection .insert_many (
98+ {
99+ "name" : role .name ,
100+ "id" : role .id ,
101+ "data" : role .json (),
102+ "inserted_at" : datetime .datetime .now (tz = datetime .UTC ),
103+ }
104+ for role in roles
105+ )
95106
96107 return roles
97108
98109
99- async def _fetch_member_api (member_id : str ) -> typing . Optional [ models .DiscordMember ] :
110+ async def _fetch_member_api (member_id : str ) -> models .DiscordMember | None :
100111 """Get a member by ID from the configured guild using the discord API."""
101112 async with httpx .AsyncClient () as client :
102113 r = await client .get (
103114 f"{ constants .DISCORD_API_BASE_URL } /guilds/{ constants .DISCORD_GUILD } "
104115 f"/members/{ member_id } " ,
105- headers = {"Authorization" : f"Bot { constants .DISCORD_BOT_TOKEN } " }
116+ headers = {"Authorization" : f"Bot { constants .DISCORD_BOT_TOKEN } " },
106117 )
107118
108119 if r .status_code == 404 :
@@ -113,8 +124,11 @@ async def _fetch_member_api(member_id: str) -> typing.Optional[models.DiscordMem
113124
114125
115126async def get_member (
116- database : Database , user_id : str , * , force_refresh : bool = False
117- ) -> typing .Optional [models .DiscordMember ]:
127+ database : Database ,
128+ user_id : str ,
129+ * ,
130+ force_refresh : bool = False ,
131+ ) -> models .DiscordMember | None :
118132 """
119133 Get a member from the cache, or from the discord API.
120134
@@ -147,7 +161,7 @@ async def get_member(
147161 await collection .insert_one ({
148162 "user" : user_id ,
149163 "data" : member .json (),
150- "inserted_at" : datetime .datetime .now (tz = datetime .timezone . utc ),
164+ "inserted_at" : datetime .datetime .now (tz = datetime .UTC ),
151165 })
152166 return member
153167
@@ -161,7 +175,9 @@ class UnauthorizedError(exceptions.HTTPException):
161175
162176
163177async def _verify_access_helper (
164- form_id : str , request : starlette .requests .Request , attribute : str
178+ form_id : str ,
179+ request : starlette .requests .Request ,
180+ attribute : str ,
165181) -> None :
166182 """A low level helper to validate access to a form resource based on the user's scopes."""
167183 form = await request .state .db .forms .find_one ({"_id" : form_id })
0 commit comments