forked from MarcoMuellner/openapi-python-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
154 lines (117 loc) · 3.72 KB
/
api.py
File metadata and controls
154 lines (117 loc) · 3.72 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from datetime import datetime
from typing import List
from typing import Optional
import uvicorn as uvicorn
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from pydantic import BaseModel
app = FastAPI()
class RootResponse(BaseModel):
message: str
class User(BaseModel):
id: int
username: str
email: str
password: str
is_active: Optional[bool]
class Team(BaseModel):
id: int
name: str
description: str
is_active: Optional[bool]
created_at: Optional[datetime]
updated_at: Optional[datetime]
users: Optional[List[User]]
@app.get("/", response_model=RootResponse, tags=["general"])
async def root():
return {"message": "Hello World"}
@app.get("/users", response_model=List[User], tags=["general"])
async def get_users():
return [
User(
id=1, username="user1", email="x@y.com", password="123456", is_active=True
),
User(
id=2, username="user2", email="x@y.com", password="123456", is_active=True
),
]
@app.get("/users/{user_id}", response_model=User, tags=["general"])
async def get_user(user_id: int):
return User(
id=user_id, username="user1", email="x@y.com", password="123456", is_active=True
)
@app.post("/users", response_model=User, tags=["general"], status_code=201)
async def create_user(user: User):
return user
@app.patch("/users/{user_id}", response_model=User, tags=["general"], status_code=200)
async def update_user(user_id: int, user: User):
return user
@app.delete("/users/{user_id}", tags=["general"], status_code=204)
async def delete_user(user_id: int):
return None
@app.get("/teams", response_model=List[Team], tags=["general"])
async def get_teams():
return [
Team(
id=1,
name="team1",
description="team1",
is_active=True,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
),
Team(
id=2,
name="team2",
description="team2",
is_active=True,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
),
]
@app.get("/teams/{team_id}", response_model=Team, tags=["general"])
async def get_team(team_id: int):
return Team(
id=team_id,
name="team1",
description="team1",
is_active=True,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow(),
users=[
User(
id=1,
username="user1",
email="x@y.com",
password="123456",
is_active=True,
)
],
)
@app.post("/teams", response_model=Team, tags=["general"], status_code=201)
async def create_team(team: Team):
return team
@app.patch("/teams/{team_id}", response_model=Team, tags=["general"], status_code=200)
async def update_team(team_id: int, team: Team):
return team
@app.delete("/teams/{team_id}", tags=["general"], status_code=204)
async def delete_team(team_id: int):
return None
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="openapi-python-generator test api",
version="1.0.0",
description="API Schema for openapi-python-generator test api",
routes=app.routes,
)
openapi_schema["info"]["x-logo"] = {
"url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
}
openapi_schema["servers"] = [{"url": "http://localhost:8080"}]
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi # type: ignore
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8080)