Skip to content

Commit

Permalink
fixed lifespan in example code and ran isort and black
Browse files Browse the repository at this point in the history
  • Loading branch information
Tert0 committed Nov 29, 2024
1 parent bed621c commit 2922de1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 48 deletions.
33 changes: 9 additions & 24 deletions examples/basic.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
from typing import List
from contextlib import asynccontextmanager
from typing import List

from fastapi import Depends, FastAPI
from fastapi.responses import JSONResponse

from fastapi_discord import DiscordOAuthClient, RateLimited, Unauthorized, User
from fastapi_discord.exceptions import ClientSessionNotInitialized
from fastapi_discord.models import GuildPreview


discord = DiscordOAuthClient(
"<client-id>", "<client-secret>", "<redirect-url>", ("identify", "guilds", "email")
) # scopes


# startup is now deprecated https://fastapi.tiangolo.com/advanced/events/#alternative-events-deprecated
# use lifespan https://fastapi.tiangolo.com/advanced/events/


@asynccontextmanager
async def lifespan(_app: FastAPI):
async def lifespan(_: FastAPI):
await discord.init()
yield


app = FastAPI()

# @app.on_event("startup")
# async def on_startup():
# await discord.init()
app = FastAPI(lifespan=lifespan)


@app.get("/login")
Expand All @@ -39,11 +35,7 @@ async def callback(code: str):
return {"access_token": token, "refresh_token": refresh_token}


@app.get(
"/authenticated",
dependencies=[Depends(discord.requires_authorization)],
response_model=bool,
)
@app.get("/authenticated", dependencies=[Depends(discord.requires_authorization)], response_model=bool)
async def isAuthenticated(token: str = Depends(discord.get_token)):
try:
auth = await discord.isAuthenticated(token)
Expand All @@ -59,10 +51,7 @@ async def unauthorized_error_handler(_, __):

@app.exception_handler(RateLimited)
async def rate_limit_error_handler(_, e: RateLimited):
return JSONResponse(
{"error": "RateLimited", "retry": e.retry_after, "message": e.message},
status_code=429,
)
return JSONResponse({"error": "RateLimited", "retry": e.retry_after, "message": e.message}, status_code=429)


@app.exception_handler(ClientSessionNotInitialized)
Expand All @@ -76,10 +65,6 @@ async def get_user(user: User = Depends(discord.user)):
return user


@app.get(
"/guilds",
dependencies=[Depends(discord.requires_authorization)],
response_model=List[GuildPreview],
)
@app.get("/guilds", dependencies=[Depends(discord.requires_authorization)], response_model=List[GuildPreview])
async def get_guilds(guilds: List = Depends(discord.guilds)):
return guilds
30 changes: 8 additions & 22 deletions examples/basic_with_custom_state.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import List
from contextlib import asynccontextmanager
from typing import List

from fastapi import Depends, FastAPI
from fastapi.responses import JSONResponse

from fastapi_discord import DiscordOAuthClient, RateLimited, Unauthorized, User
from fastapi_discord.exceptions import ClientSessionNotInitialized
from fastapi_discord.models import GuildPreview


discord = DiscordOAuthClient(
"<client-id>", "<client-secret>", "<redirect-url>", ("identify", "guilds", "email")
) # scopes
Expand All @@ -16,16 +17,12 @@


@asynccontextmanager
async def lifespan(_app: FastAPI):
async def lifespan(_: FastAPI):
await discord.init()
yield


app = FastAPI()

# @app.on_event("startup")
# async def on_startup():
# await discord.init()
app = FastAPI(lifespan=lifespan)


@app.get("/login")
Expand All @@ -40,11 +37,7 @@ async def callback(code: str, state: str):
return {"access_token": token, "refresh_token": refresh_token}


@app.get(
"/authenticated",
dependencies=[Depends(discord.requires_authorization)],
response_model=bool,
)
@app.get("/authenticated", dependencies=[Depends(discord.requires_authorization)], response_model=bool)
async def isAuthenticated(token: str = Depends(discord.get_token)):
try:
auth = await discord.isAuthenticated(token)
Expand All @@ -60,10 +53,7 @@ async def unauthorized_error_handler(_, __):

@app.exception_handler(RateLimited)
async def rate_limit_error_handler(_, e: RateLimited):
return JSONResponse(
{"error": "RateLimited", "retry": e.retry_after, "message": e.message},
status_code=429,
)
return JSONResponse({"error": "RateLimited", "retry": e.retry_after, "message": e.message}, status_code=429)


@app.exception_handler(ClientSessionNotInitialized)
Expand All @@ -77,10 +67,6 @@ async def get_user(user: User = Depends(discord.user)):
return user


@app.get(
"/guilds",
dependencies=[Depends(discord.requires_authorization)],
response_model=List[GuildPreview],
)
@app.get("/guilds", dependencies=[Depends(discord.requires_authorization)], response_model=List[GuildPreview])
async def get_guilds(guilds: List = Depends(discord.guilds)):
return guilds
4 changes: 2 additions & 2 deletions fastapi_discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from aiocache import cached
from fastapi import Depends, Request
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from typing_extensions import TypedDict, Literal
from typing_extensions import Literal, TypedDict

from .config import DISCORD_API_URL, DISCORD_OAUTH_AUTHENTICATION_URL, DISCORD_TOKEN_URL
from .exceptions import RateLimited, ScopeMissing, Unauthorized, InvalidToken, ClientSessionNotInitialized
from .exceptions import ClientSessionNotInitialized, InvalidToken, RateLimited, ScopeMissing, Unauthorized
from .models import Guild, GuildPreview, User


Expand Down

0 comments on commit 2922de1

Please sign in to comment.