-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathmain.py
More file actions
173 lines (136 loc) · 4.05 KB
/
main.py
File metadata and controls
173 lines (136 loc) · 4.05 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import random
from typing import Annotated
from fastapi import FastAPI, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
tags_metadata = [
{
"name": "Random Playground",
"description": "Generate random numbers",
},
{
"name": "Random Items Management",
"description": "Create, shuffle, read, update and delete items",
},
]
app = FastAPI(
title="Randomizer API",
description="Shuffle lists, pick random items, generate random numbers.",
version="1.0.0",
openapi_tags=tags_metadata,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000", "https://example.com"],
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE"],
allow_headers=["*"],
)
items_db = []
class Item(BaseModel):
name: str = Field(
min_length=1, max_length=100, description="The item name"
)
class ItemResponse(BaseModel):
message: str
item: str
class ItemListResponse(BaseModel):
original_order: list[str]
randomized_order: list[str]
count: int
class ItemUpdateResponse(BaseModel):
message: str
old_item: str
new_item: str
class ItemDeleteResponse(BaseModel):
message: str
deleted_item: str
remaining_items_count: int
@app.get("/", tags=["Random Playground"])
def home():
return {"message": "Welcome to the Randomizer API"}
@app.get("/random/{max_value}", tags=["Random Playground"])
def get_random_number(max_value: int):
return {"max": max_value, "random_number": random.randint(1, max_value)}
@app.get("/random-between", tags=["Random Playground"])
def get_random_number_between(
min_value: Annotated[
int,
Query(
title="Minimum Value",
description="The minimum random number",
ge=1,
le=1000,
),
] = 1,
max_value: Annotated[
int,
Query(
title="Maximum Value",
description="The maximum random number",
ge=1,
le=1000,
),
] = 99,
):
if min_value > max_value:
raise HTTPException(
status_code=400, detail="min_value can't be greater than max_value"
)
return {
"min": min_value,
"max": max_value,
"random_number": random.randint(min_value, max_value),
}
@app.post(
"/items", response_model=ItemResponse, tags=["Random Items Management"]
)
def add_item(item: Item):
if item.name in items_db:
raise HTTPException(status_code=400, detail="Item already exists")
items_db.append(item.name)
return ItemResponse(message="Item added successfully", item=item.name)
@app.get(
"/items", response_model=ItemListResponse, tags=["Random Items Management"]
)
def get_randomized_items():
randomized = items_db.copy()
random.shuffle(randomized)
return ItemListResponse(
original_order=items_db,
randomized_order=randomized,
count=len(items_db),
)
@app.put(
"/items/{update_item_name}",
response_model=ItemUpdateResponse,
tags=["Random Items Management"],
)
def update_item(update_item_name: str, item: Item):
if update_item_name not in items_db:
raise HTTPException(status_code=404, detail="Item not found")
if item.name in items_db:
raise HTTPException(
status_code=409, detail="An item with that name already exists"
)
index = items_db.index(update_item_name)
items_db[index] = item.name
return ItemUpdateResponse(
message="Item updated successfully",
old_item=update_item_name,
new_item=item.name,
)
@app.delete(
"/items/{item}",
response_model=ItemDeleteResponse,
tags=["Random Items Management"],
)
def delete_item(item: str):
if item not in items_db:
raise HTTPException(status_code=404, detail="Item not found")
items_db.remove(item)
return ItemDeleteResponse(
message="Item deleted successfully",
deleted_item=item,
remaining_items_count=len(items_db),
)