diff --git a/README.md b/README.md index b0eddcc..d1e6725 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,10 @@ discord = DiscordOAuthClient( "", "", "", ("identify", "guilds", "email") ) # scopes +@app.on_event("startup") +async def on_startup(): + await discord.init() + @app.get("/login") async def login(): @@ -72,6 +76,7 @@ async def get_user(user: User = Depends(discord.user)): async def get_guilds(guilds: List = Depends(discord.guilds)): return guilds ``` + # Inspired by [Starlette-Discord](https://github.com/nwunderly/starlette-discord) diff --git a/examples/basic.py b/examples/basic.py index 7dd714c..47879d3 100644 --- a/examples/basic.py +++ b/examples/basic.py @@ -12,6 +12,11 @@ ) # scopes +@app.on_event("startup") +async def on_startup(): + await discord.init() + + @app.get("/login") async def login(): return {"url": discord.oauth_login_url} diff --git a/examples/basic_with_custom_state.py b/examples/basic_with_custom_state.py index ce5abac..1eb8db2 100644 --- a/examples/basic_with_custom_state.py +++ b/examples/basic_with_custom_state.py @@ -12,6 +12,11 @@ ) # scopes +@app.on_event("startup") +async def on_startup(): + await discord.init() + + @app.get("/login") async def login(): return {"url": discord.get_oauth_login_url(state="my state")} diff --git a/fastapi_discord/client.py b/fastapi_discord/client.py index 6fd0223..1f65b4b 100644 --- a/fastapi_discord/client.py +++ b/fastapi_discord/client.py @@ -1,8 +1,6 @@ -import asyncio from typing import Dict, List, Optional, Tuple, Union import aiohttp -import nest_asyncio from aiocache import cached from fastapi import Depends, Request from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer @@ -72,7 +70,7 @@ class DiscordOAuthClient: scopes: str proxy: Optional[str] proxy_auth: Optional[aiohttp.BasicAuth] - client_session: aiohttp.ClientSession + client_session: aiohttp.ClientSession = None """Client for Discord Oauth2. @@ -93,13 +91,13 @@ class DiscordOAuthClient: """ def __init__( - self, - client_id, - client_secret, - redirect_uri, - scopes=("identify",), - proxy=None, - proxy_auth: aiohttp.BasicAuth = None, + 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 @@ -107,10 +105,13 @@ def __init__( self.scopes = "%20".join(scope for scope in scopes) self.proxy = proxy self.proxy_auth = proxy_auth - nest_asyncio.apply() - asyncio.get_running_loop().run_until_complete(asyncio.create_task(self.initialize())) - async def initialize(self): + async def init(self): + """ + Initialized the connection to the discord api + """ + if self.client_session is not None: + return self.client_session = aiohttp.ClientSession() 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 headers = {"Authorization": f"Bearer {token}"} if method == "GET": async with self.client_session.get( - f"{DISCORD_API_URL}{route}", - headers=headers, - proxy=self.proxy, - proxy_auth=self.proxy_auth, + 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, - proxy=self.proxy, - proxy_auth=self.proxy_auth, + f"{DISCORD_API_URL}{route}", + headers=headers, + proxy=self.proxy, + proxy_auth=self.proxy_auth, ) as resp: data = await resp.json() else: @@ -159,10 +160,10 @@ async def request(self, route: str, token: str = None, method: Literal["GET", "P async def get_token_response(self, payload: PAYLOAD) -> TokenResponse: async with self.client_session.post( - DISCORD_TOKEN_URL, - data=payload, - proxy=self.proxy, - proxy_auth=self.proxy_auth, + DISCORD_TOKEN_URL, + data=payload, + proxy=self.proxy, + proxy_auth=self.proxy_auth, ) as resp: return await resp.json() diff --git a/requirements.txt b/requirements.txt index f4a77c9..d4d975c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ fastapi==0.85.0 aiohttp==3.8.3 aiocache==0.11.1 -uvicorn==0.18.3 -nest_asyncio==1.5.6 \ No newline at end of file +uvicorn==0.18.3 \ No newline at end of file diff --git a/setup.py b/setup.py index b775729..cb55406 100644 --- a/setup.py +++ b/setup.py @@ -5,13 +5,13 @@ setup( name="fastapi_discord", packages=find_packages(), - version="0.2.3", + version="0.2.4", description="Discord OAuth FastAPI extension for APIs", long_description=readme, long_description_content_type="text/markdown", author="Tert0", license="MIT", - install_requires=["fastapi==0.85.0", "aiohttp==3.8.3", "aiocache==0.11.1", "nest_asyncio==1.5.6"], + install_requires=["fastapi==0.85.0", "aiohttp==3.8.3", "aiocache==0.11.1"], python_requires=">=3.5", url="https://github.com/Tert0/fastapi-discord", )