@dryv/fastapi-backendv0.1.0
FastAPI routers and Pydantic models.
dryv pack add @dryv/fastapi-backend{%- macro value_type(value) -%}
{%- if value.is.schema -%}{{ value.schema.name.pascal }}
{%- elif value.is.unknown -%}object
{%- elif value.is.dynamic -%}Any
{%- elif value.is.primitive and value.property.primitive.is.boolean -%}bool
{%- elif value.is.primitive and value.property.primitive.is.number and value.property.primitive.is.integer -%}int
{%- elif value.is.primitive and value.property.primitive.is.number -%}float
{%- elif value.is.primitive and value.property.primitive.is.uuid -%}UUID
{%- elif value.is.primitive -%}str
{%- elif value.is.enum -%}{{ value.property.enum.name.pascal }}
{%- elif value.is.generic -%}{{ value.property.generic.name.pascal }}
{%- else -%}Any
{%- endif -%}
{%- endmacro -%}
{%- macro result_type(operation) -%}
{%- if operation.output %}{{ value_type(operation.output.value) }}{% else %}None{% endif -%}
{%- endmacro -%}
{#- The service receives the input by name: the model for a schema input, keyword-only fields otherwise. -#}
{%- macro service_params(operation) -%}
{%- if operation.input and operation.input.kind == 'schema' %}, input: {{ value_type(operation.input.value) }}
{%- elif operation.input %}, *{% for field in operation.input.fields %}, {{ field.name.snake }}: {{ value_type(field.value) }}{% if field.optional %} | None = None{% endif %}{% endfor %}
{%- endif -%}
{%- endmacro -%}
{%- macro binds_whole(http) -%}
{%- for binding in http.inputs %}{% if binding.port.field is none %}x{% endif %}{% endfor -%}
{%- endmacro -%}
{%- macro param_name(binding) -%}
{%- if binding.is.path %}{{ binding.name }}{% else %}{{ binding.port.name.snake }}{% endif -%}
{%- endmacro -%}
"""Generated by Dryv for {{ feature.name.title }}. Do not edit; implement {{ file.symbols.service }}."""
from abc import ABC, abstractmethod
from typing import Any
from uuid import UUID
from fastapi import APIRouter, Query
{% for dependency in file.dependencies %}{% if dependency.symbol %}
from {{ dependency.path.relative | replace('../', '..') | replace('/', '.') | replace('.py', '') }} import {{ dependency.symbol }}
{%- endif %}{% endfor %}
__all__ = ["{{ file.symbols.service }}", "{{ file.symbols.router }}"]
class {{ file.symbols.service }}(ABC):
"""What {{ feature.name.title }} needs from handwritten code.
Raise ``fastapi.HTTPException`` for failures such as "not found".
"""
{% for operation in operations %}
@abstractmethod
async def {{ operation.name.snake }}(self{{ service_params(operation) }}) -> {{ result_type(operation) }}:
"""{{ operation.description or operation.name.title }}{% if operation.role %} ({{ operation.role }}){% endif %}."""
{% endfor %}
def {{ file.symbols.router }}(service: {{ file.symbols.service }}) -> APIRouter:
"""Routes for {{ feature.name.title }}, delegating to ``service``."""
router = APIRouter(tags=["{{ feature.name.title }}"])
{% for operation in operations %}
{%- set http = operation.facets.http %}
@router.{{ http.method | lower }}(
"{{ http.path.local }}"{% if operation.output %},
response_model={{ value_type(operation.output.value) }}{% endif %}{% for binding in http.outputs %},
status_code={{ binding.status }}{% endfor %},
)
async def {{ operation.name.snake }}(
{%- for binding in http.inputs %}
{% if binding.is.path %}{{ binding.name }}: {{ value_type(binding.port.value) }}{% elif binding.is.query %}{{ binding.port.name.snake }}: {{ value_type(binding.port.value) }} = Query(alias="{{ binding.name }}"){% else %}{{ binding.port.name.snake }}: {{ value_type(binding.port.value) }}{% endif %},
{%- endfor %}
) -> {{ result_type(operation) }}:
return await service.{{ operation.name.snake }}(
{%- if binds_whole(http) %}{% for binding in http.inputs %}{{ param_name(binding) }}{% endfor %}
{%- else %}{% for binding in http.inputs %}{{ binding.port.name.snake }}={{ param_name(binding) }}{% if not loop.last %}, {% endif %}{% endfor %}
{%- endif %})
{% endfor %}
return router