-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from letsbuilda/gh24
Add generation of UUIDs
- Loading branch information
Showing
6 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Modules of the API""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |