11"""Generation of things"""
22
33import uuid
4+ from random import randint
45from typing import Literal
56
67from fastapi import APIRouter
7-
88# pylint: disable-next=no-name-in-module
9- from pydantic import BaseModel , Field
9+ from pydantic import BaseModel , Field , validator
1010
1111router_generators = APIRouter (prefix = "/generators" , tags = ["generators" ])
1212
1313
14- class UUIDs (BaseModel ):
15- """Model to hold a list of UUIDs"""
16-
17- uuids : list [str ]
18-
19-
2014class UUIDConfig (BaseModel ):
2115 """Model to hold configuration for UUID generation"""
2216
2317 uuid_type : Literal [1 , 4 ]
2418 quantity : int = Field (gt = 0 , default = 1 )
2519
2620
21+ class UUIDs (BaseModel ):
22+ """Model to hold a list of UUIDs"""
23+
24+ uuids : list [str ]
25+
26+
2727@router_generators .post ("/uuids/" )
2828async def bulk_uuids (config : UUIDConfig ) -> UUIDs :
2929 """Generate bulk UUIDs"""
@@ -35,3 +35,31 @@ async def bulk_uuids(config: UUIDConfig) -> UUIDs:
3535 raise ValueError (f"unsupported UUID type: { config .uuid_type } " )
3636 uuids = [str (function ()) for _ in range (config .quantity )]
3737 return UUIDs (uuids = uuids )
38+
39+
40+ class NumbersConfig (BaseModel ):
41+ """Model to hold configuration for number generation"""
42+
43+ lower_bound : int | None = Field (default = 1 )
44+ upper_bound : int | None = Field (default = 1 )
45+ quantity : int | None = Field (gt = 0 , default = 1 )
46+
47+ @validator ('upper_bound' )
48+ def upper_bound_must_be_greater_than_lower_bound (cls , v , values , ** kwargs ):
49+ if 'lower_bound' in values and v < values ['lower_bound' ]:
50+ raise ValueError ('upper bound must be greater than lower bound' )
51+ return v
52+
53+
54+ class Numbers (BaseModel ):
55+ """Model to hold a list of numbers"""
56+
57+ numbers : list [int ]
58+ total : int
59+
60+
61+ @router_generators .post ("/random-numbers/" )
62+ async def random_numbers (config : NumbersConfig ) -> Numbers :
63+ """Generate bulk random numbers"""
64+ numbers = [randint (config .lower_bound , config .upper_bound ) for _ in range (config .quantity )]
65+ return Numbers (numbers = numbers , total = sum (numbers ))
0 commit comments