11"""Generation of things"""
22
33import uuid
4+ from random import randint
45from typing import Literal
56
67from fastapi import APIRouter
78
89# pylint: disable-next=no-name-in-module
9- from pydantic import BaseModel , Field
10+ from pydantic import BaseModel , Field , validator
1011
1112router_generators = APIRouter (prefix = "/generators" , tags = ["generators" ])
1213
1314
14- class UUIDs (BaseModel ):
15- """Model to hold a list of UUIDs"""
16-
17- uuids : list [str ]
18-
19-
2015class UUIDConfig (BaseModel ):
2116 """Model to hold configuration for UUID generation"""
2217
2318 uuid_type : Literal [1 , 4 ]
2419 quantity : int = Field (gt = 0 , default = 1 )
2520
2621
22+ class UUIDs (BaseModel ):
23+ """Model to hold a list of UUIDs"""
24+
25+ uuids : list [str ]
26+
27+
2728@router_generators .post ("/uuids/" )
2829async def bulk_uuids (config : UUIDConfig ) -> UUIDs :
2930 """Generate bulk UUIDs"""
@@ -35,3 +36,32 @@ async def bulk_uuids(config: UUIDConfig) -> UUIDs:
3536 raise ValueError (f"unsupported UUID type: { config .uuid_type } " )
3637 uuids = [str (function ()) for _ in range (config .quantity )]
3738 return UUIDs (uuids = uuids )
39+
40+
41+ class NumbersConfig (BaseModel ):
42+ """Model to hold configuration for number generation"""
43+
44+ lower_bound : int | None = Field (default = 1 )
45+ upper_bound : int | None = Field (default = 1 )
46+ quantity : int | None = Field (gt = 0 , default = 1 )
47+
48+ @validator ("upper_bound" )
49+ def upper_bound_must_be_greater_than_lower_bound (cls , v , values ):
50+ """Confirm upper bound is greater than lower bound"""
51+ if "lower_bound" in values and v < values ["lower_bound" ]:
52+ raise ValueError ("upper bound must be greater than lower bound" )
53+ return v
54+
55+
56+ class Numbers (BaseModel ):
57+ """Model to hold a list of numbers"""
58+
59+ numbers : list [int ]
60+ total : int
61+
62+
63+ @router_generators .post ("/random-numbers/" )
64+ async def random_numbers (config : NumbersConfig ) -> Numbers :
65+ """Generate bulk random numbers"""
66+ numbers = [randint (config .lower_bound , config .upper_bound ) for _ in range (config .quantity )]
67+ return Numbers (numbers = numbers , total = sum (numbers ))
0 commit comments