|
| 1 | +from base64 import b64decode |
| 2 | +from typing import Annotated |
| 3 | + |
| 4 | +from fastapi import APIRouter |
| 5 | +from fastapi import Body |
| 6 | +from fastapi import Request |
| 7 | +from fastapi import Response |
| 8 | +from fastapi import status |
| 9 | + |
| 10 | +OPENID_LOGO = b64decode( |
| 11 | + """ |
| 12 | +R0lGODlhEAAQAMQAAO3t7eHh4srKyvz8/P5pDP9rENLS0v/28P/17tXV1dHEvPDw8M3Nzfn5+d3d |
| 13 | +3f5jA97Syvnv6MfLzcfHx/1mCPx4Kc/S1Pf189C+tP+xgv/k1N3OxfHy9NLV1/39/f///yH5BAAA |
| 14 | +AAAALAAAAAAQABAAAAVq4CeOZGme6KhlSDoexdO6H0IUR+otwUYRkMDCUwIYJhLFTyGZJACAwQcg |
| 15 | +EAQ4kVuEE2AIGAOPQQAQwXCfS8KQGAwMjIYIUSi03B7iJ+AcnmclHg4TAh0QDzIpCw4WGBUZeikD |
| 16 | +Fzk0lpcjIQA7 |
| 17 | +""" |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +router = APIRouter( |
| 22 | + prefix="/v1/pets", |
| 23 | + tags=["pets"], |
| 24 | + responses={404: {"description": "Not found"}}, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +@router.get("") |
| 29 | +async def list_pets(request: Request, response: Response): |
| 30 | + assert request.scope["openapi"] |
| 31 | + assert not request.scope["openapi"].errors |
| 32 | + assert request.scope["openapi"].parameters.query == { |
| 33 | + "page": 1, |
| 34 | + "limit": 12, |
| 35 | + "search": "", |
| 36 | + } |
| 37 | + data = [ |
| 38 | + { |
| 39 | + "id": 12, |
| 40 | + "name": "Cat", |
| 41 | + "ears": { |
| 42 | + "healthy": True, |
| 43 | + }, |
| 44 | + }, |
| 45 | + ] |
| 46 | + response.headers["X-Rate-Limit"] = "12" |
| 47 | + return {"data": data} |
| 48 | + |
| 49 | + |
| 50 | +@router.post("") |
| 51 | +async def create_pet(request: Request): |
| 52 | + assert request.scope["openapi"].parameters.cookie == { |
| 53 | + "user": 1, |
| 54 | + } |
| 55 | + assert request.scope["openapi"].parameters.header == { |
| 56 | + "api-key": "12345", |
| 57 | + } |
| 58 | + assert request.scope["openapi"].body.__class__.__name__ == "PetCreate" |
| 59 | + assert request.scope["openapi"].body.name in ["Cat", "Bird"] |
| 60 | + if request.scope["openapi"].body.name == "Cat": |
| 61 | + assert request.scope["openapi"].body.ears.__class__.__name__ == "Ears" |
| 62 | + assert request.scope["openapi"].body.ears.healthy is True |
| 63 | + if request.scope["openapi"].body.name == "Bird": |
| 64 | + assert ( |
| 65 | + request.scope["openapi"].body.wings.__class__.__name__ == "Wings" |
| 66 | + ) |
| 67 | + assert request.scope["openapi"].body.wings.healthy is True |
| 68 | + |
| 69 | + headers = { |
| 70 | + "X-Rate-Limit": "12", |
| 71 | + } |
| 72 | + return Response(status_code=status.HTTP_201_CREATED, headers=headers) |
| 73 | + |
| 74 | + |
| 75 | +@router.get("/{petId}") |
| 76 | +async def detail_pet(request: Request, response: Response): |
| 77 | + assert request.scope["openapi"] |
| 78 | + assert not request.scope["openapi"].errors |
| 79 | + assert request.scope["openapi"].parameters.path == { |
| 80 | + "petId": 12, |
| 81 | + } |
| 82 | + data = { |
| 83 | + "id": 12, |
| 84 | + "name": "Cat", |
| 85 | + "ears": { |
| 86 | + "healthy": True, |
| 87 | + }, |
| 88 | + } |
| 89 | + response.headers["X-Rate-Limit"] = "12" |
| 90 | + return { |
| 91 | + "data": data, |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | +@router.get("/{petId}/photo") |
| 96 | +async def download_pet_photo(): |
| 97 | + return Response(content=OPENID_LOGO, media_type="image/gif") |
| 98 | + |
| 99 | + |
| 100 | +@router.post("/{petId}/photo") |
| 101 | +async def upload_pet_photo( |
| 102 | + image: Annotated[bytes, Body(media_type="image/jpg")], |
| 103 | +): |
| 104 | + assert image == OPENID_LOGO |
| 105 | + return Response(status_code=status.HTTP_201_CREATED) |
0 commit comments