Skip to content

Commit

Permalink
Merge pull request #25 from letsbuilda/gh24
Browse files Browse the repository at this point in the history
Add generation of UUIDs
  • Loading branch information
shenanigansd authored Mar 11, 2023
2 parents 50f6b4a + 54b16b4 commit e9d9f9b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
Changelog
=========

- :release:`1.7.0 <11th March 2023>`
- :feature:`24` Add random number generation

- :release:`1.6.0 <09th March 2023>`
- :feature:`21` Add random number generation

Expand Down
2 changes: 1 addition & 1 deletion src/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Public API for our projects"""

__version__ = "1.6.0"
__version__ = "1.7.0"
1 change: 1 addition & 0 deletions src/api/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Modules of the API"""
37 changes: 37 additions & 0 deletions src/api/modules/generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Generation of things"""

import uuid
from typing import Literal

from fastapi import APIRouter

# pylint: disable-next=no-name-in-module
from pydantic import BaseModel, Field

router_generators = APIRouter(prefix="/generators", tags=["generators"])


class UUIDs(BaseModel):
"""Model to hold a list of UUIDs"""

uuids: list[str]


class UUIDConfig(BaseModel):
"""Model to hold configuration for UUID generation"""

uuid_type: Literal[1, 4]
quantity: int = Field(gt=0, default=1)


@router_generators.post("/uuids/")
async def bulk_uuids(config: UUIDConfig) -> UUIDs:
"""Generate bulk UUIDs"""
if config.uuid_type == 1:
function = uuid.uuid1
elif config.uuid_type == 4:
function = uuid.uuid4
else:
raise ValueError(f"unsupported UUID type: {config.uuid_type}")
uuids = [str(function()) for _ in range(config.quantity)]
return UUIDs(uuids=uuids)
3 changes: 3 additions & 0 deletions src/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pydantic import BaseModel

from . import __version__
from .modules.generators import router_generators

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


app.include_router(router_fun)

app.include_router(router_generators)
16 changes: 16 additions & 0 deletions tests/test_generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Test the generators module"""

import uuid

from fastapi.testclient import TestClient

from api.server import app

client = TestClient(app)


def test_read_random_numbers():
response = client.post("/generators/uuids/", json={"uuid_type": 1, "quantity": 1})
assert response.status_code == 200
first = response.json()["uuids"][0]
assert isinstance(uuid.UUID(first), uuid.UUID)

0 comments on commit e9d9f9b

Please sign in to comment.