-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_API.py
More file actions
69 lines (50 loc) · 1.27 KB
/
first_API.py
File metadata and controls
69 lines (50 loc) · 1.27 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
61
62
63
64
65
66
67
68
69
from fastapi import FastAPI, HTTPException
import uvicorn
from pydantic import BaseModel
app = FastAPI()
books = [
{
"id": 1,
"title": "богатый кара бедный бойко",
"author": "арс",
},
{
"id": 2,
"title": "как жрать бургеры и кадрить девчонок аурой",
"author": "бойкус",
},
]
@app.get(
"/books",
tags=["Книги"],
summary="Получить все книги"
)
def read_books():
return books
@app.get(
"/books/{id}",
tags=["Книги"],
summary="Получить конкретную книгу"
)
def get_book(id: int):
for book in books:
if book["id"] == id:
return book
raise HTTPException(status_code=404, detail="Книга не найдена")
######
class NewBook(BaseModel):
title: str
author: str
@app.post(
"/books",
tags=["Книги"])
def create_book(new_book: NewBook):
books.append({
"id": len(books) + 1,
"title": new_book.title,
"author": new_book.author
})
return {"success": True, "message": "Книга успешно добавлена"}
#####
if __name__ == "__main__":
uvicorn.run("main:app", reload=True)