Use Pydantic models

This commit is contained in:
Kafonek, Matt
2021-04-07 18:17:25 +00:00
parent 42c5030b0e
commit a55ccce64e

View File

@@ -6,6 +6,9 @@ from fastapi import Form
from fastapi import Request from fastapi import Request
from .client import get_client from .client import get_client
from .models import AuthorizationError
from .models import HubApiError
from .models import User
from .security import get_current_user from .security import get_current_user
# APIRouter prefix cannot end in / # APIRouter prefix cannot end in /
@@ -20,7 +23,7 @@ async def get_token(code: str = Form(...)):
# Everything else we can hardcode / pull from env # Everything else we can hardcode / pull from env
async with get_client() as client: async with get_client() as client:
redirect_uri = ( redirect_uri = (
os.getenv("PUBLIC_HOST", "") + os.environ["JUPYTERHUB_OAUTH_CALLBACK_URL"], os.environ["PUBLIC_HOST"] + os.environ["JUPYTERHUB_OAUTH_CALLBACK_URL"],
) )
data = { data = {
"client_id": os.environ["JUPYTERHUB_CLIENT_ID"], "client_id": os.environ["JUPYTERHUB_CLIENT_ID"],
@@ -30,7 +33,7 @@ async def get_token(code: str = Form(...)):
"redirect_uri": redirect_uri, "redirect_uri": redirect_uri,
} }
resp = await client.post("/oauth2/token", data=data) resp = await client.post("/oauth2/token", data=data)
### response is {'access_token': <token>, 'token_type': 'Bearer'} ### resp.json() is {'access_token': <token>, 'token_type': 'Bearer'}
return resp.json() return resp.json()
@@ -40,15 +43,20 @@ async def index():
return {"Hello": "World"} return {"Hello": "World"}
# See security.py comment, consider a User model instead of dict? # response_model and responses dict translate to OpenAPI (Swagger) hints
@router.get("/me") # compare and contrast what the /me endpoint looks like in Swagger vs /debug
async def me(user: dict = Depends(get_current_user)): @router.get(
"/me",
response_model=User,
responses={401: {'model': AuthorizationError}, 400: {'model': HubApiError}},
)
async def me(user: User = Depends(get_current_user)):
"Authenticated function that returns the User model" "Authenticated function that returns the User model"
return user return user
@router.get("/debug") @router.get("/debug")
async def index(request: Request, user: dict = Depends(get_current_user)): async def index(request: Request, user: User = Depends(get_current_user)):
""" """
Authenticated function that returns a few pieces of debug Authenticated function that returns a few pieces of debug
* Environ of the service process * Environ of the service process