-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path02_function_api.py
More file actions
60 lines (42 loc) · 1.47 KB
/
Copy path02_function_api.py
File metadata and controls
60 lines (42 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from pydantic import BaseModel, Field
from panther import Panther, status
from panther.app import API
from panther.request import Request
from panther.response import Response
class CreateBookInput(BaseModel):
title: str = Field(min_length=1)
author: str = Field(min_length=1)
pages_count: int = Field(ge=1)
class BookOutput(BaseModel):
title: str
author: str
BOOKS: list[dict] = [
{'id': 1, 'title': 'Panther Guide', 'author': 'Panther Team', 'pages_count': 120},
]
@API(methods=['GET'])
async def list_books(request: Request):
author = request.query_params.get('author')
if author:
return [book for book in BOOKS if book['author'] == author]
return BOOKS
@API(methods=['GET'], output_model=BookOutput)
async def first_book():
return BOOKS[0]
@API(methods=['GET'])
async def get_book(book_id: int):
for book in BOOKS:
if book['id'] == book_id:
return book
return Response(data={'detail': 'Book not found'}, status_code=status.HTTP_404_NOT_FOUND)
@API(methods=['POST'], input_model=CreateBookInput)
async def create_book(request: Request):
book = {'id': len(BOOKS) + 1, **request.validated_data.model_dump()}
BOOKS.append(book)
return Response(data=book, status_code=status.HTTP_201_CREATED)
url_routing = {
'books/': list_books,
'books/create/': create_book,
'books/first/': first_book,
'books/<book_id>/': get_book,
}
app = Panther(__name__, configs=__name__, urls=url_routing)