|
| 1 | +from collections.abc import Awaitable, Callable, Coroutine |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import uvicorn |
| 6 | +from fastapi import FastAPI |
| 7 | +from pydantic import create_model |
| 8 | + |
| 9 | +from fastcs.attributes import AttrR, AttrRW, AttrW, T |
| 10 | +from fastcs.controller import BaseController |
| 11 | +from fastcs.mapping import Mapping |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class RestServerOptions: |
| 16 | + host: str = "localhost" |
| 17 | + port: int = 8080 |
| 18 | + log_level: str = "info" |
| 19 | + |
| 20 | + |
| 21 | +class RestServer: |
| 22 | + def __init__(self, mapping: Mapping): |
| 23 | + self._mapping = mapping |
| 24 | + self._app = self._create_app() |
| 25 | + |
| 26 | + def _create_app(self): |
| 27 | + app = FastAPI() |
| 28 | + _add_attribute_api_routes(app, self._mapping) |
| 29 | + _add_command_api_routes(app, self._mapping) |
| 30 | + |
| 31 | + return app |
| 32 | + |
| 33 | + def run(self, options: RestServerOptions | None = None) -> None: |
| 34 | + if options is None: |
| 35 | + options = RestServerOptions() |
| 36 | + |
| 37 | + uvicorn.run( |
| 38 | + self._app, |
| 39 | + host=options.host, |
| 40 | + port=options.port, |
| 41 | + log_level=options.log_level, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def _put_request_body(attribute: AttrW[T]): |
| 46 | + """ |
| 47 | + Creates a pydantic model for each datatype which defines the schema |
| 48 | + of the PUT request body |
| 49 | + """ |
| 50 | + type_name = str(attribute.datatype.dtype.__name__).title() |
| 51 | + # key=(type, ...) to declare a field without default value |
| 52 | + return create_model( |
| 53 | + f"Put{type_name}Value", |
| 54 | + value=(attribute.datatype.dtype, ...), |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def _wrap_attr_put( |
| 59 | + attribute: AttrW[T], |
| 60 | +) -> Callable[[T], Coroutine[Any, Any, None]]: |
| 61 | + async def attr_set(request): |
| 62 | + await attribute.process(request.value) |
| 63 | + |
| 64 | + # Fast api uses type annotations for validation, schema, conversions |
| 65 | + attr_set.__annotations__["request"] = _put_request_body(attribute) |
| 66 | + |
| 67 | + return attr_set |
| 68 | + |
| 69 | + |
| 70 | +def _get_response_body(attribute: AttrR[T]): |
| 71 | + """ |
| 72 | + Creates a pydantic model for each datatype which defines the schema |
| 73 | + of the GET request body |
| 74 | + """ |
| 75 | + type_name = str(attribute.datatype.dtype.__name__).title() |
| 76 | + # key=(type, ...) to declare a field without default value |
| 77 | + return create_model( |
| 78 | + f"Get{type_name}Value", |
| 79 | + value=(attribute.datatype.dtype, ...), |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def _wrap_attr_get( |
| 84 | + attribute: AttrR[T], |
| 85 | +) -> Callable[[], Coroutine[Any, Any, Any]]: |
| 86 | + async def attr_get() -> Any: # Must be any as response_model is set |
| 87 | + value = attribute.get() # type: ignore |
| 88 | + return {"value": value} |
| 89 | + |
| 90 | + return attr_get |
| 91 | + |
| 92 | + |
| 93 | +def _add_attribute_api_routes(app: FastAPI, mapping: Mapping) -> None: |
| 94 | + for single_mapping in mapping.get_controller_mappings(): |
| 95 | + path = single_mapping.controller.path |
| 96 | + |
| 97 | + for attr_name, attribute in single_mapping.attributes.items(): |
| 98 | + attr_name = attr_name.replace("_", "-") |
| 99 | + route = f"{'/'.join(path)}/{attr_name}" if path else attr_name |
| 100 | + |
| 101 | + match attribute: |
| 102 | + # https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods |
| 103 | + case AttrRW(): |
| 104 | + app.add_api_route( |
| 105 | + f"/{route}", |
| 106 | + _wrap_attr_get(attribute), |
| 107 | + methods=["GET"], # Idempotent and safe data retrieval, |
| 108 | + status_code=200, # https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET |
| 109 | + response_model=_get_response_body(attribute), |
| 110 | + ) |
| 111 | + app.add_api_route( |
| 112 | + f"/{route}", |
| 113 | + _wrap_attr_put(attribute), |
| 114 | + methods=["PUT"], # Idempotent state change |
| 115 | + status_code=204, # https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT |
| 116 | + ) |
| 117 | + case AttrR(): |
| 118 | + app.add_api_route( |
| 119 | + f"/{route}", |
| 120 | + _wrap_attr_get(attribute), |
| 121 | + methods=["GET"], |
| 122 | + status_code=200, |
| 123 | + response_model=_get_response_body(attribute), |
| 124 | + ) |
| 125 | + case AttrW(): |
| 126 | + app.add_api_route( |
| 127 | + f"/{route}", |
| 128 | + _wrap_attr_put(attribute), |
| 129 | + methods=["PUT"], |
| 130 | + status_code=204, |
| 131 | + ) |
| 132 | + |
| 133 | + |
| 134 | +def _wrap_command( |
| 135 | + method: Callable, controller: BaseController |
| 136 | +) -> Callable[..., Awaitable[None]]: |
| 137 | + async def command() -> None: |
| 138 | + await getattr(controller, method.__name__)() |
| 139 | + |
| 140 | + return command |
| 141 | + |
| 142 | + |
| 143 | +def _add_command_api_routes(app: FastAPI, mapping: Mapping) -> None: |
| 144 | + for single_mapping in mapping.get_controller_mappings(): |
| 145 | + path = single_mapping.controller.path |
| 146 | + |
| 147 | + for name, method in single_mapping.command_methods.items(): |
| 148 | + cmd_name = name.replace("_", "-") |
| 149 | + route = f"/{'/'.join(path)}/{cmd_name}" if path else cmd_name |
| 150 | + app.add_api_route( |
| 151 | + f"/{route}", |
| 152 | + _wrap_command( |
| 153 | + method.fn, |
| 154 | + single_mapping.controller, |
| 155 | + ), |
| 156 | + methods=["PUT"], |
| 157 | + status_code=204, |
| 158 | + ) |
0 commit comments