Pattens and tips for writing better code
The last comment block of each slide will be treated as slide notes. It will be visible and editable in Presenter Mode along with the slide. Read more in the docs
Pattens and tips for writing better code
An API design-first approach is about describing every API design in an iterative way that both humans and computers can understand—before you write any code. Where API-first is about putting APIs front and center, API design-first is about the process of creating the API itself.
An API design-first approach is about describing every API design in an iterative way that both humans and computers can understand—before you write any code. Where API-first is about putting APIs front and center, API design-first is about the process of creating the API itself.
There are three principles of API First Design:
Your API is the first user interface of your application
Your API comes first, then the implementation
Your API is described (and maybe even self-descriptive)
Here are the steps:
schemas.py
endpoint
and method
in router.py
We’ll build an apis here and skip the database design part, GET /countrypedia/countries
: Returns list of countries supported by CountryPedia For each countries
schema.py
, and write some sample responserouter.py
create single data model in schema.py
, and write some sample response
class CountryResponse(BaseModel): alpha_code: str = Field(name="alpha_code",description="The alpha_code code for the country",max_length=300) name: str = Field(title="name",description="The English name for the country") name_cn: str = Field(title="name_cn", description="The Chinese name for the country") flag: str = Field(title="flag", description="The flag for the country") class Config: schema_extra = { "example": { "alpha_code": "ALA", "name": "Aland lslands", "name_cn": "奥兰群岛", "flag": "", } }
class CountryResponse(BaseModel): alpha_code: str = Field(name="alpha_code",description="The alpha_code code for the country",max_length=300) name: str = Field(title="name",description="The English name for the country") name_cn: str = Field(title="name_cn", description="The Chinese name for the country") flag: str = Field(title="flag", description="The flag for the country") class Config: schema_extra = { "example": { "alpha_code": "ALA", "name": "Aland lslands", "name_cn": "奥兰群岛", "flag": "", } }
create list data model in schema.py
, and write some sample response
class CountriesResponse(BaseModel): __root__: list[CountryResponse] class Config: schema_extra = { "example": { "total": 2, "page_number": 0, "page_size": 10, "rows": [ { "alpha_code": "ALA", "name": "Aland lslands", "name_cn": "奥兰群岛", "flag": "", }, { "alpha_code": "ATG", "name": "Antigua and Barbuda", "name_cn": "安提瓜与巴布达", "flag": "", }, ], } }
class CountriesResponse(BaseModel): __root__: list[CountryResponse] class Config: schema_extra = { "example": { "total": 2, "page_number": 0, "page_size": 10, "rows": [ { "alpha_code": "ALA", "name": "Aland lslands", "name_cn": "奥兰群岛", "flag": "", }, { "alpha_code": "ATG", "name": "Antigua and Barbuda", "name_cn": "安提瓜与巴布达", "flag": "", }, ], } }
create endpoint in the router
router = APIRouter( prefix="/countrypedia", tags=["countrypedia"], ) @router.get( "/countries", summary="Returns list of countries supported by CountryPedia", description=""" Returns list of countries supported by CountryPedia For each countries: Fields: alpha code, name, name_cn,flag, list of supported regions (others tbd) """, response_model=Response[PaginationData[CountriesResponse]], ) async def get_countries(user_id: int = Depends(get_user_id)): pass
router = APIRouter( prefix="/countrypedia", tags=["countrypedia"], ) @router.get( "/countries", summary="Returns list of countries supported by CountryPedia", description=""" Returns list of countries supported by CountryPedia For each countries: Fields: alpha code, name, name_cn,flag, list of supported regions (others tbd) """, response_model=Response[PaginationData[CountriesResponse]], ) async def get_countries(user_id: int = Depends(get_user_id)): pass