|
| 1 | +"""JSON API router class.""" |
| 2 | + |
| 3 | +from typing import ( |
| 4 | + Any, |
| 5 | + Dict, |
| 6 | + List, |
| 7 | + Optional, |
| 8 | + Type, |
| 9 | + Union, |
| 10 | +) |
| 11 | + |
| 12 | +from fastapi import APIRouter |
| 13 | +from pydantic import BaseModel |
| 14 | + |
| 15 | +from fastapi_rest_jsonapi.exceptions import ExceptionResponseSchema |
| 16 | +from fastapi_rest_jsonapi.methods import ( |
| 17 | + delete_detail_jsonapi, |
| 18 | + get_detail_jsonapi, |
| 19 | + get_list_jsonapi, |
| 20 | + patch_detail_jsonapi, |
| 21 | + post_list_jsonapi, |
| 22 | +) |
| 23 | + |
| 24 | +JSON_API_RESPONSE_TYPE = Optional[Dict[Union[int, str], Dict[str, Any]]] |
| 25 | + |
| 26 | + |
| 27 | +class RoutersJSONAPI(object): |
| 28 | + """API Router interface for JSON API endpoints in web-services.""" |
| 29 | + |
| 30 | + def __init__( # noqa: WPS211 |
| 31 | + self, |
| 32 | + routers: APIRouter, |
| 33 | + path: Union[str, List[str]], |
| 34 | + tags: List[str], |
| 35 | + class_detail: Any, |
| 36 | + class_list: Any, |
| 37 | + schema: Type[BaseModel], |
| 38 | + type_resource: str, |
| 39 | + schema_in_patch: Type[BaseModel], |
| 40 | + schema_in_post: Type[BaseModel], |
| 41 | + resp_schema_detail: Type[BaseModel], |
| 42 | + resp_schema_list: Type[BaseModel], |
| 43 | + ) -> None: |
| 44 | + """Initialize router items.""" |
| 45 | + self._routers: APIRouter = routers |
| 46 | + self._path: Union[str, List[str]] = path |
| 47 | + self._tags: List[str] = tags |
| 48 | + self.class_detail: Any = class_detail |
| 49 | + self.class_list: Any = class_list |
| 50 | + self._type: str = type_resource |
| 51 | + self._schema: Type[BaseModel] = schema |
| 52 | + self._schema_in_patch: Type[BaseModel] = schema_in_patch |
| 53 | + self._schema_in_post: Type[BaseModel] = schema_in_post |
| 54 | + self._resp_schema_detail: Type[BaseModel] = resp_schema_detail |
| 55 | + self._resp_schema_list: Type[BaseModel] = resp_schema_list |
| 56 | + |
| 57 | + if isinstance(self._path, list): |
| 58 | + for i_path in self._path: |
| 59 | + self._add_routers(i_path) |
| 60 | + else: |
| 61 | + self._add_routers(self._path) |
| 62 | + |
| 63 | + def _add_routers(self, path: str): |
| 64 | + """Add new router.""" |
| 65 | + error_responses: Optional[JSON_API_RESPONSE_TYPE] = { |
| 66 | + 400: {"model": ExceptionResponseSchema}, |
| 67 | + 401: {"model": ExceptionResponseSchema}, |
| 68 | + 404: {"model": ExceptionResponseSchema}, |
| 69 | + 500: {"model": ExceptionResponseSchema}, |
| 70 | + } |
| 71 | + if hasattr(self.class_list, "get"): |
| 72 | + self._routers.get(path, tags=self._tags, response_model=self._resp_schema_list, responses=error_responses,)( |
| 73 | + get_list_jsonapi(schema=self._schema, type_=self._type, schema_resp=self._resp_schema_list)( |
| 74 | + self.class_list.get |
| 75 | + ) |
| 76 | + ) |
| 77 | + |
| 78 | + if hasattr(self.class_list, "post"): |
| 79 | + self._routers.post( |
| 80 | + path, |
| 81 | + tags=self._tags, |
| 82 | + response_model=self._resp_schema_detail, |
| 83 | + responses=error_responses, |
| 84 | + )( |
| 85 | + post_list_jsonapi( |
| 86 | + schema=self._schema, |
| 87 | + schema_in=self._schema_in_post, |
| 88 | + type_=self._type, |
| 89 | + schema_resp=self._resp_schema_detail, |
| 90 | + )(self.class_list.post) |
| 91 | + ) |
| 92 | + |
| 93 | + if hasattr(self.class_detail, "get"): |
| 94 | + self._routers.get( |
| 95 | + path + "/{obj_id}", |
| 96 | + tags=self._tags, |
| 97 | + response_model=self._resp_schema_detail, |
| 98 | + responses=error_responses, |
| 99 | + )( |
| 100 | + get_detail_jsonapi(schema=self._schema, type_=self._type, schema_resp=self._resp_schema_detail)( |
| 101 | + self.class_detail.get |
| 102 | + ) |
| 103 | + ) |
| 104 | + |
| 105 | + if hasattr(self.class_detail, "patch"): |
| 106 | + self._routers.patch( |
| 107 | + path + "/{obj_id}", |
| 108 | + tags=self._tags, |
| 109 | + response_model=self._resp_schema_detail, |
| 110 | + responses=error_responses, |
| 111 | + )( |
| 112 | + patch_detail_jsonapi( |
| 113 | + schema=self._schema, |
| 114 | + schema_in=self._schema_in_patch, |
| 115 | + type_=self._type, |
| 116 | + schema_resp=self._resp_schema_detail, |
| 117 | + )(self.class_detail.patch) |
| 118 | + ) |
| 119 | + |
| 120 | + if hasattr(self.class_detail, "delete"): |
| 121 | + self._routers.delete( |
| 122 | + path + "/{obj_id}", |
| 123 | + tags=self._tags, |
| 124 | + )(delete_detail_jsonapi(schema=self._schema)(self.class_detail.delete)) |
0 commit comments