Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
staciax committed Jan 24, 2023
1 parent 88f763b commit 5172a0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 48 deletions.
39 changes: 1 addition & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ pip install linked-roles

## FastAPI Examples:
```py
import logging

from fastapi import FastAPI, status
from fastapi.responses import RedirectResponse

import config
from linked_roles import LinkedRolesOAuth2, OAuth2Scopes, RolePlatform, Unauthorized, NotFound

_log = logging.getLogger(__name__)
from linked_roles import LinkedRolesOAuth2, OAuth2Scopes, OAuth2Unauthorized, RolePlatform

app = FastAPI(title='Linked Roles OAuth2')

Expand All @@ -42,25 +38,19 @@ client = LinkedRolesOAuth2(
state=config.COOKIE_SECRET,
)


@app.on_event('startup')
async def startup():
await client.start()
_log.info('Startup complete')


@app.on_event('shutdown')
async def shutdown():
await client.close()
_log.info('Shutdown complete')


@app.get('/linked-role', status_code=status.HTTP_302_FOUND)
async def linked_roles():
url = client.get_oauth_url()
return RedirectResponse(url=url)


@app.get('/verified-role')
async def verified_role(code: str):

Expand All @@ -77,33 +67,6 @@ async def verified_role(code: str):
await user.edit_role_metadata(platform=platform)

return 'Verified role successfully'


@app.post('/update-role-metadata')
async def update_role_metadata(user_id: str):

user = client.get_user(id=user_id)

if user is None:
raise UserNotFound(f'User with ID {user_id} not found')

tokens = user.get_tokens()
if tokens is None:
raise OAuth2Unauthorized('User is not connected to Linked Roles')

if tokens.is_expired():
await tokens.refresh()

platform = user.get_role_platform()
platform.username = 'STACIA#4321'
platform.edit_metadata(key='matches', value=5000)
platform.edit_metadata(key='winrate', value=5000)
platform.edit_metadata(key='combat_score', value=100000)
platform.edit_metadata(key='last_update', value=datetime.datetime.now())
platform.edit_metadata(key='verified', value=True)

await user.edit_role_metadata(platform=platform)

```

# Register Examples:
Expand Down
29 changes: 19 additions & 10 deletions examples/fastapi_.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
from fastapi.responses import JSONResponse, RedirectResponse

import _config
from linked_roles import LinkedRolesOAuth2, NotFound, OAuth2Scopes, RateLimited, RolePlatform, Unauthorized
from linked_roles import (
LinkedRolesOAuth2,
OAuth2Scopes,
OAuth2Unauthorized,
RateLimited,
RolePlatform,
Unauthorized,
User,
UserNotFound,
)

_log = logging.getLogger(__name__)

Expand Down Expand Up @@ -61,16 +70,16 @@ async def verified_role(code: str):


@app.post('/update-role-metadata')
async def update_role_metadata(user_id: int):
async def update_role_metadata(user_id: str):

user = client.get_user(id=user_id)

if user is None:
raise NotFound(f'User with ID {user_id} not found')
raise UserNotFound(f'User with ID {user_id} not found')

tokens = user.get_tokens()
if tokens is None:
raise Unauthorized('User is not connected to Linked Roles')
raise OAuth2Unauthorized('User is not connected to Linked Roles')

if tokens.is_expired():
await tokens.refresh()
Expand All @@ -86,22 +95,22 @@ async def update_role_metadata(user_id: int):
await user.edit_role_metadata(platform=platform)


@app.exception_handler(Unauthorized)
@app.exception_handler(UserNotFound)
async def unauthorized_exception_handler(r: Request, e: Unauthorized):
return JSONResponse(
status_code=status.HTTP_401_UNAUTHORIZED,
content={'error': 'Unauthorized', 'message': e.message},
)


@app.exception_handler(UserNotFound)
async def not_found_exception_handler(r: Request, e: UserNotFound):
return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content={'error': 'Not found', 'message': e.message})


@app.exception_handler(RateLimited)
async def rate_limited_exception_handler(r: Request, e: RateLimited):
return JSONResponse(
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
content={'error': 'Rate limited', 'retry_after': e.retry_after, 'message': e.message},
)


@app.exception_handler(NotFound)
async def not_found_exception_handler(r: Request, e: NotFound):
return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content={'error': 'Not found', 'message': e.message})

0 comments on commit 5172a0e

Please sign in to comment.