Coverage for src/kwai/api/v1/schemas.py: 100%
15 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module for defining common JSON:API schemas."""
3from typing import Self
5from pydantic import BaseModel
7from kwai.api.v1.resources import CountryResourceIdentifier
8from kwai.core.json_api import Document, ResourceData
9from kwai.modules.club.domain.country import CountryEntity
12class CountryAttributes(BaseModel):
13 """Attributes for the country JSON:API resource."""
15 iso_2: str
16 iso_3: str
17 name: str
20class CountryResource(CountryResourceIdentifier, ResourceData[CountryAttributes, None]):
21 """A JSON:API resource for a country."""
24class CountryDocument(Document[CountryResource, None]):
25 """A JSON:API document for one or more countries."""
27 @classmethod
28 def create(cls, country: CountryEntity) -> Self:
29 """Create a country document from a country value object."""
30 country_resource = CountryResource(
31 id=str(country.id),
32 attributes=CountryAttributes(
33 iso_2=country.iso_2, iso_3=country.iso_3, name=country.name
34 ),
35 )
36 return cls(data=country_resource)