11import random
2- from string import ascii_letters , hexdigits
32from typing import Annotated
4-
5- from jinja2 import Template
6- from pydantic import BaseModel , Field
7-
83from fastapi import FastAPI , HTTPException , Query
94from fastapi .middleware .cors import CORSMiddleware
10- from fastapi . responses import HTMLResponse , PlainTextResponse
5+ from pydantic import BaseModel , Field
116
127tags_metadata = [
138 {
149 "name" : "Random Playground" ,
15- "description" : "Operations for generating random stuff " ,
10+ "description" : "Generate random numbers " ,
1611 },
1712 {
1813 "name" : "Random Items Management" ,
2217
2318app = FastAPI (
2419 title = "Randomizer API" ,
25- description = "Generate random numbers and manage a list of items " ,
20+ description = "Shuffle lists, pick random items, and generate random numbers. " ,
2621 version = "1.0.0" ,
2722 openapi_tags = tags_metadata ,
2823)
@@ -44,19 +39,9 @@ class Item(BaseModel):
4439 )
4540
4641
47- class ItemList (BaseModel ):
48- items : list [str ] = Field (min_items = 1 , description = "List of items" )
49-
50-
51- class ItemUpdate (BaseModel ):
52- old_item : str = Field (min_length = 1 , description = "Item to replace" )
53- new_item : str = Field (min_length = 1 , description = "New item name" )
54-
55-
5642class ItemResponse (BaseModel ):
5743 message : str
5844 item : str
59- total_items : int
6045
6146
6247class ItemListResponse (BaseModel ):
@@ -74,16 +59,16 @@ class ItemUpdateResponse(BaseModel):
7459class ItemDeleteResponse (BaseModel ):
7560 message : str
7661 deleted_item : str
77- remaining_items : int
62+ remaining_items_count : int
7863
7964
8065@app .get ("/" , tags = ["Random Playground" ])
8166async def home ():
8267 return {"message" : "Welcome to the Randomizer API" }
8368
8469
85- @app .get ("/random/{max_value}" , tags = [ "Random Playground" ] )
86- async def get_random_number (max_value : int ):
70+ @app .get ("/random/{max_value}" )
71+ async def get_random_number (max_value : int , tags = [ "Random Playground" ] ):
8772 return {"max" : max_value , "random_number" : random .randint (1 , max_value )}
8873
8974
@@ -95,7 +80,7 @@ def get_random_number_between(
9580 title = "Minimum Value" ,
9681 description = "The minimum random number" ,
9782 ge = 1 ,
98- le = 99 ,
83+ le = 1000 ,
9984 ),
10085 ] = 1 ,
10186 max_value : Annotated [
@@ -109,7 +94,9 @@ def get_random_number_between(
10994 ] = 99 ,
11095):
11196 if min_value > max_value :
112- return {"error" : "min_value cannot be greater than max_value" }
97+ raise HTTPException (
98+ status_code = 400 , detail = "min_value can't be greater than max_value"
99+ )
113100
114101 return {
115102 "min" : min_value ,
@@ -118,48 +105,6 @@ def get_random_number_between(
118105 }
119106
120107
121- @app .get ("/random-string/{length}" , tags = ["Random Playground" ])
122- def generate_random_string (length : int ):
123- return PlainTextResponse ("" .join (random .choices (ascii_letters , k = length )))
124-
125-
126- @app .get (
127- "/random-color" , response_class = HTMLResponse , tags = ["Random Playground" ]
128- )
129- def random_color ():
130- hex_chars = "" .join (random .choice (hexdigits .lower ()) for _ in range (6 ))
131- hex_color = f"#{ hex_chars } "
132- template_string = """
133- <!DOCTYPE html>
134- <html lang="en">
135- <head>
136- <meta charset="UTF-8">
137- <title>Random Color: {{ color }}</title>
138- <style>
139- body {
140- height: 100vh;
141- display: flex;
142- justify-content: center;
143- align-items: center;
144- background-color: {{ color }};
145- color: white;
146- font-size: 120px;
147- font-family: monospace;
148- }
149- </style>
150- </head>
151- <body>
152- <div>{{ color }}</div>
153- </body>
154- </html>
155- """
156-
157- template = Template (template_string )
158- html_content = template .render (color = hex_color )
159-
160- return html_content
161-
162-
163108@app .post (
164109 "/items" , response_model = ItemResponse , tags = ["Random Items Management" ]
165110)
@@ -168,24 +113,13 @@ def add_item(item: Item):
168113 raise HTTPException (status_code = 400 , detail = "Item already exists" )
169114
170115 items_db .append (item .name )
171- return ItemResponse (
172- message = "Item added successfully" ,
173- item = item .name ,
174- total_items = len (items_db ),
175- )
116+ return ItemResponse (message = "Item added successfully" , item = item .name )
176117
177118
178119@app .get (
179- "/items/random" ,
180- response_model = ItemListResponse ,
181- tags = ["Random Items Management" ],
120+ "/items" , response_model = ItemListResponse , tags = ["Random Items Management" ]
182121)
183122def get_randomized_items ():
184- if not items_db :
185- return ItemListResponse (
186- original_order = [], randomized_order = [], count = 0
187- )
188-
189123 randomized = items_db .copy ()
190124 random .shuffle (randomized )
191125
@@ -197,21 +131,26 @@ def get_randomized_items():
197131
198132
199133@app .put (
200- "/items" ,
134+ "/items/{update_item_name} " ,
201135 response_model = ItemUpdateResponse ,
202136 tags = ["Random Items Management" ],
203137)
204- def update_item (item_update : ItemUpdate ):
205- if item_update . old_item not in items_db :
138+ def update_item (update_item_name : str , item : Item ):
139+ if update_item_name not in items_db :
206140 raise HTTPException (status_code = 404 , detail = "Item not found" )
207141
208- index = items_db .index (item_update .old_item )
209- items_db [index ] = item_update .new_item
142+ if item .name in items_db :
143+ raise HTTPException (
144+ status_code = 409 , detail = "An item with that name already exists"
145+ )
146+
147+ index = items_db .index (update_item_name )
148+ items_db [index ] = item .name
210149
211150 return ItemUpdateResponse (
212151 message = "Item updated successfully" ,
213- old_item = item_update . old_item ,
214- new_item = item_update . new_item ,
152+ old_item = update_item_name ,
153+ new_item = item . name ,
215154 )
216155
217156
@@ -229,5 +168,5 @@ def delete_item(item: str):
229168 return ItemDeleteResponse (
230169 message = "Item deleted successfully" ,
231170 deleted_item = item ,
232- remaining_items = len (items_db ),
171+ remaining_items_count = len (items_db ),
233172 )
0 commit comments