-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
158 additions
and
21 deletions.
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
Empty file.
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,21 @@ | ||
if __name__ == "__main__": | ||
from datetime import date | ||
from pathlib import Path | ||
|
||
import uvicorn | ||
from fastapi import FastAPI | ||
|
||
from tests.test_tutorial.test_users_example003.schemas import latest | ||
from tests.test_tutorial.test_users_example003.users import router, versions | ||
from tests.test_tutorial.utils import clean_versions | ||
from universi import api_version_var, regenerate_dir_to_all_versions | ||
|
||
try: | ||
regenerate_dir_to_all_versions(latest, versions) | ||
router_versions = router.create_versioned_copies(versions, latest_schemas_module=latest) | ||
app = FastAPI() | ||
api_version_var.set(date(2000, 1, 1)) | ||
app.include_router(router_versions[date(2000, 1, 1)]) | ||
uvicorn.run(app) | ||
finally: | ||
clean_versions(Path(__file__).parent / "schemas") |
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,23 @@ | ||
from .schemas.unions.users import UserCreateRequestUnion | ||
|
||
|
||
class UserScenario: | ||
async def create_user(self, payload: UserCreateRequestUnion): | ||
return { | ||
"id": 83, | ||
"_prefetched_addresses": [{"id": 100, "value": payload.default_address}], | ||
} | ||
|
||
async def get_user(self, user_id: int): | ||
return { | ||
"id": user_id, | ||
"_prefetched_addresses": (await self.get_user_addresses(user_id))["data"], | ||
} | ||
|
||
async def get_user_addresses(self, user_id: int): | ||
return { | ||
"data": [ | ||
{"id": 83, "value": "123 Example St"}, | ||
{"id": 91, "value": "456 Main St"}, | ||
], | ||
} |
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions
18
tests/test_tutorial/test_users_example003/schemas/latest/users.py
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,18 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class UserCreateRequest(BaseModel): | ||
default_address: str | ||
|
||
|
||
class UserResource(BaseModel): | ||
id: int | ||
|
||
|
||
class UserAddressResource(BaseModel): | ||
id: int | ||
value: str | ||
|
||
|
||
class UserAddressResourceList(BaseModel): | ||
data: list[UserAddressResource] |
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,79 @@ | ||
from datetime import date | ||
from typing import Any | ||
|
||
from universi import Field, VersionedAPIRouter | ||
from universi.structure import ( | ||
Version, | ||
VersionChange, | ||
Versions, | ||
convert_response_to_previous_version_for, | ||
endpoint, | ||
schema, | ||
) | ||
|
||
from .schemas.latest.users import ( | ||
UserAddressResourceList, | ||
UserCreateRequest, | ||
UserResource, | ||
) | ||
from .scenario import UserScenario | ||
|
||
router = VersionedAPIRouter() | ||
|
||
|
||
@router.post("/users", response_model=UserResource) | ||
async def create_user(user: UserCreateRequest): | ||
return await UserScenario().create_user(user) | ||
|
||
|
||
@router.get("/users/{user_id}", response_model=UserResource) | ||
async def get_user(user_id: int): | ||
return await UserScenario().get_user(user_id) | ||
|
||
|
||
@router.get("/users/{user_id}/addresses", response_model=UserAddressResourceList) | ||
async def get_user_addresses(user_id: int): | ||
return await UserScenario().get_user_addresses(user_id) | ||
|
||
|
||
class ChangeAddressToList(VersionChange): | ||
description = "Change vat id to list" | ||
instructions_to_migrate_to_previous_version = ( | ||
schema(UserCreateRequest).field("addresses").didnt_exist, | ||
schema(UserCreateRequest).field("address").existed_with(type=str, info=Field()), | ||
schema(UserResource).field("addresses").didnt_exist, | ||
schema(UserResource).field("address").existed_with(type=str, info=Field()), | ||
) | ||
|
||
@convert_response_to_previous_version_for(get_user, create_user) | ||
def change_addresses_to_single_item(cls, data: dict[str, Any]) -> None: | ||
data["address"] = data.pop("addresses")[0] | ||
|
||
@schema(UserCreateRequest).had_property("addresses") | ||
def addresses_property(parsed_schema): | ||
return [parsed_schema.address] # pragma: no cover | ||
|
||
|
||
class ChangeAddressesToSubresource(VersionChange): | ||
description = "Change vat ids to subresource" | ||
instructions_to_migrate_to_previous_version = ( | ||
schema(UserCreateRequest).field("addresses").existed_with(type=list[str], info=Field()), | ||
schema(UserCreateRequest).field("default_address").didnt_exist, | ||
schema(UserResource).field("addresses").existed_with(type=list[str], info=Field()), | ||
endpoint(get_user_addresses).didnt_exist, | ||
) | ||
|
||
@convert_response_to_previous_version_for(get_user, create_user) | ||
def change_addresses_to_list(cls, data: dict[str, Any]) -> None: | ||
data["addresses"] = [id["value"] for id in data.pop("_prefetched_addresses")] | ||
|
||
@schema(UserCreateRequest).had_property("default_address") | ||
def default_address_property(parsed_schema): | ||
return parsed_schema.addresses[0] # pragma: no cover | ||
|
||
|
||
versions = Versions( | ||
Version(date(2002, 1, 1), ChangeAddressesToSubresource), | ||
Version(date(2001, 1, 1), ChangeAddressToList), | ||
Version(date(2000, 1, 1)), | ||
) |
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
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