-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (34 loc) · 1.31 KB
/
main.py
File metadata and controls
54 lines (34 loc) · 1.31 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
from fastapi import FastAPI, Path, Query, Header, Request
from pydantic import BaseModel
app = FastAPI()
@app.get("/api/healthchecker")
def root():
return {"message": "Welcome to FastAPI!"}
@app.get("/note/new")
async def read_new_notes():
return {"message": "Return new notes"}
@app.get("/notes/{note_id}")
async def read_note(note_id: int = Path(description="The ID of the note to get", gt=0, le=10)):
return {"note": note_id}
@app.get("/notes")
async def read_notes(skip: int = 0, limit: int = Query(default=10, le=100, ge=10)):
return {"message": f"Return all notes: skip: {skip}, limit: {limit}"}
class Note(BaseModel):
name: str
description: str
done: bool
@app.post("/notes")
async def create_note(note: Note):
return {"name": note.name, "description": note.description, "status": note.done}
@app.get("/headers")
async def read_headers(user_agent: str = Header(default=None)):
return {"User-Agent": user_agent}
@app.get("/all-headers")
async def read_all_headers(request: Request):
return {"headers": dict(request.headers)}
@app.get("/custom-header")
async def read_custom_header(x_custom: str = Header(default=None, min_length=3, max_length=50)):
return {"X-Custom": x_custom}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)