diff --git a/README.md b/README.md index af91e99..3aea9fc 100644 --- a/README.md +++ b/README.md @@ -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') @@ -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): @@ -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: diff --git a/examples/fastapi_.py b/examples/fastapi_.py index 3e8867d..9bc61de 100644 --- a/examples/fastapi_.py +++ b/examples/fastapi_.py @@ -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__) @@ -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() @@ -86,7 +95,7 @@ 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, @@ -94,14 +103,14 @@ async def unauthorized_exception_handler(r: Request, e: Unauthorized): ) +@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})