Skip to content

Commit e9d9f9b

Browse files
authored
Merge pull request #25 from letsbuilda/gh24
Add generation of UUIDs
2 parents 50f6b4a + 54b16b4 commit e9d9f9b

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

docs/source/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
Changelog
55
=========
66

7+
- :release:`1.7.0 <11th March 2023>`
8+
- :feature:`24` Add random number generation
9+
710
- :release:`1.6.0 <09th March 2023>`
811
- :feature:`21` Add random number generation
912

src/api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Public API for our projects"""
22

3-
__version__ = "1.6.0"
3+
__version__ = "1.7.0"

src/api/modules/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Modules of the API"""

src/api/modules/generators.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Generation of things"""
2+
3+
import uuid
4+
from typing import Literal
5+
6+
from fastapi import APIRouter
7+
8+
# pylint: disable-next=no-name-in-module
9+
from pydantic import BaseModel, Field
10+
11+
router_generators = APIRouter(prefix="/generators", tags=["generators"])
12+
13+
14+
class UUIDs(BaseModel):
15+
"""Model to hold a list of UUIDs"""
16+
17+
uuids: list[str]
18+
19+
20+
class UUIDConfig(BaseModel):
21+
"""Model to hold configuration for UUID generation"""
22+
23+
uuid_type: Literal[1, 4]
24+
quantity: int = Field(gt=0, default=1)
25+
26+
27+
@router_generators.post("/uuids/")
28+
async def bulk_uuids(config: UUIDConfig) -> UUIDs:
29+
"""Generate bulk UUIDs"""
30+
if config.uuid_type == 1:
31+
function = uuid.uuid1
32+
elif config.uuid_type == 4:
33+
function = uuid.uuid4
34+
else:
35+
raise ValueError(f"unsupported UUID type: {config.uuid_type}")
36+
uuids = [str(function()) for _ in range(config.quantity)]
37+
return UUIDs(uuids=uuids)

src/api/server.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pydantic import BaseModel
1212

1313
from . import __version__
14+
from .modules.generators import router_generators
1415

1516
release_prefix = getenv("API_SENTRY_RELEASE_PREFIX", "api")
1617
git_sha = getenv("GIT_SHA", "development")
@@ -76,3 +77,5 @@ async def random_numbers(quantity: int, range_high: int) -> Numbers:
7677

7778

7879
app.include_router(router_fun)
80+
81+
app.include_router(router_generators)

tests/test_generators.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Test the generators module"""
2+
3+
import uuid
4+
5+
from fastapi.testclient import TestClient
6+
7+
from api.server import app
8+
9+
client = TestClient(app)
10+
11+
12+
def test_read_random_numbers():
13+
response = client.post("/generators/uuids/", json={"uuid_type": 1, "quantity": 1})
14+
assert response.status_code == 200
15+
first = response.json()["uuids"][0]
16+
assert isinstance(uuid.UUID(first), uuid.UUID)

0 commit comments

Comments
 (0)