Skip to content

Commit

Permalink
Updated examples to use lifespan instead of startup which is now depr…
Browse files Browse the repository at this point in the history
…ecated. (#188)

* Update README.md

* Update basic.py

* Update basic_with_custom_state.py

* fixed typo

* fixed typo

* fixed typo

* Complying black code style

* Complying black code style

* Update basic.py

* Update basic_with_custom_state.py

---------

Co-authored-by: Tert0 <[email protected]>
  • Loading branch information
arfathyahiya and Tert0 authored Nov 21, 2024
1 parent 89e95ec commit 6bce293
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@ PIP Package `fastapi-discord`
You can find the Example in [examples/](https://github.com/Tert0/fastapi-discord/tree/master/examples)
```py
from typing import List

from contextlib import asynccontextmanager
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

app = FastAPI()

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/

@app.on_event("startup")
async def on_startup():
@asynccontextmanager
async def lifespan(_app: FastAPI):
await discord.init()
yield

app = FastAPI()

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


@app.get("/login")
Expand Down
18 changes: 14 additions & 4 deletions examples/basic.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
from typing import List

from contextlib import asynccontextmanager
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

app = FastAPI()

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/

@app.on_event("startup")
async def on_startup():

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


app = FastAPI()

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


@app.get("/login")
Expand Down
18 changes: 14 additions & 4 deletions examples/basic_with_custom_state.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
from typing import List

from contextlib import asynccontextmanager
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

app = FastAPI()

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/

@app.on_event("startup")
async def on_startup():

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


app = FastAPI()

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


@app.get("/login")
Expand Down

0 comments on commit 6bce293

Please sign in to comment.