-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
262 lines (236 loc) · 7.52 KB
/
api.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import base64
import pathlib
from typing import Literal
from fastapi import FastAPI, Path, Query
from pydantic import BaseModel, Field
from datetime import date
from fastapi.openapi.utils import get_openapi
class Item(BaseModel):
id: str | None = Field(description="")
name: str = Field(
description='Optional unique identifier for the article, such as a GUID. Must not contain any "." or "/" characters. Different articles with the same price on different days should still have different IDs. If not set, a unique ID will be generated internally.'
)
price: float = Field(
description="The default price for the articles. Default prices are shown to guests before authentication. Yet, the final price is based on priceLookup if present."
)
priceLookup: str = Field(
description="The price lookup code for the article. Whatever identifier is used in the cash register to identify the price group of articles, such as an article ID."
)
category: Literal[
"MAIN", "SIDE", "BOTTLE", "DESSERT", "DRINK", "SALAD", "SOUP", "OTHER"
] = Field(description="Category of the item.")
class Menu(BaseModel):
date: str = Field(description="ISO date for which the menu is valid.")
items: list[Item] = Field(description="List of items on the menu.")
class Config:
schema_extra = {
"example": {
"date": "2022-10-07",
"items": [
{
"id": "abc",
"name": "Burger",
"price": 3.0,
"priceLookup": "123",
"category": "MAIN",
},
{
"id": "def",
"name": "Salad",
"price": 1.5,
"priceLookup": "456",
"category": "SALAD",
},
],
}
}
class Menus(BaseModel):
menus: list[Menu] = Field(description="List of menus on separate dates.")
class Config:
schema_extra = {
"example": {
"menus": [
{
"date": "2023-01-01",
"items": [
{
"name": "Burger",
"price": 3.0,
"priceLookup": "123",
"category": "MAIN",
},
{
"name": "Salad",
"price": 1.5,
"priceLookup": "456",
"category": "SALAD",
},
],
},
{
"date": "2023-01-02",
"items": [
{
"name": "Pasta",
"price": 3.0,
"priceLookup": "123",
"category": "MAIN",
},
{
"name": "Salad",
"price": 1.5,
"priceLookup": "546",
"category": "SALAD",
},
],
},
]
}
}
class Message(BaseModel):
"""Message for additional information."""
message: str
app = FastAPI(
title="VisioLab Menu Import API",
description="API for importing menus into the VisioLab backend.",
)
@app.get(
"/menus/{date}",
summary="Get menu for date",
responses={
200: {"model": Menu, "description": "The menu."},
404: {
"model": Message,
"description": "No menu available for the requested date.",
},
},
)
async def get_menu(
date: str = Path(
description="Date for which the menu is requested.",
example="2023-01-01",
)
):
return {
"date": "2023-01-01",
"items": [
{
"name": "Burger",
"price": 3.0,
"priceLookup": "123",
"category": "MAIN",
},
{
"name": "Salad",
"price": 1.5,
"priceLookup": "456",
"category": "SALAD",
},
],
}
@app.get(
"/menus/today",
summary="Get menu for today",
responses={
200: {"model": Menu, "description": "The menu."},
404: {
"model": Message,
"description": "No menu available for today.",
},
},
)
async def get_todays_menu():
return {
"date": "2023-01-01",
"items": [
{
"name": "Burger",
"price": 3.0,
"priceLookup": "123",
"category": "MAIN",
},
{
"name": "Salad",
"price": 1.5,
"priceLookup": "456",
"category": "SALAD",
},
],
}
@app.get(
"/menus/",
summary="Get menus for date range",
responses={
200: {"model": Menus, "description": "The menus."},
404: {
"model": Message,
"description": "No menus available for the requested date range.",
},
},
)
async def get_menus(
start: str = Query(
description="Start date for which the menus are requested.",
example="2023-01-01",
),
end: str = Query(
description="End date for which the menus are requested (inclusive).",
example="2023-01-02",
),
):
"""Get items available in cash register to synchronize prices."""
return {
"menus": [
{
"date": "2023-01-01",
"items": [
{
"name": "Burger",
"price": 3.0,
"priceLookup": "123",
"category": "MAIN",
},
{
"name": "Salad",
"price": 1.5,
"priceLookup": "456",
"category": "SALAD",
},
],
},
{
"date": "2023-01-02",
"items": [
{
"name": "Pasta",
"price": 3.0,
"priceLookup": "123",
"category": "MAIN",
},
{
"name": "Salad",
"price": 1.5,
"priceLookup": "546",
"category": "SALAD",
},
],
},
]
}
def as_base64_url(path):
return "data:image/png;base64, " + base64.b64encode(
pathlib.Path(path).read_bytes()
).decode("utf-8")
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title=app.title,
version=app.version,
description=app.description,
routes=app.routes,
)
openapi_schema["info"]["x-logo"] = {"url": as_base64_url("./logo.png")}
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi