|
| 1 | +""" |
| 2 | +The module which defines error responses that will be returned by the API. |
| 3 | +""" |
| 4 | + |
| 5 | +from collections import OrderedDict |
| 6 | +from typing import Self |
| 7 | + |
| 8 | +from fastapi import status |
| 9 | +from fastapi.responses import PlainTextResponse |
| 10 | + |
| 11 | + |
| 12 | +class FailureResponse: |
| 13 | + descriptor_delimiter = " |OR| " |
| 14 | + defaultResponseClass = PlainTextResponse |
| 15 | + |
| 16 | + def __init__(self, args_dict: dict): |
| 17 | + self.__dict = OrderedDict(args_dict) |
| 18 | + |
| 19 | + def __or__(self, other: Self): |
| 20 | + buff = OrderedDict(self.__dict) |
| 21 | + for key, value in other.__dict.items(): |
| 22 | + buff[key] = FailureResponse.listify(buff.get(key, [])) |
| 23 | + buff[key].extend(FailureResponse.listify(value)) |
| 24 | + return FailureResponse(buff) |
| 25 | + |
| 26 | + def __str__(self): |
| 27 | + return str(self.__dict) |
| 28 | + |
| 29 | + def fastapi_response(self, status_code: int | None = None): |
| 30 | + if status_code is None and len(self.__dict) > 1: |
| 31 | + raise ValueError("In case of multiple response status codes, please provide one.") |
| 32 | + status_code, content = [(k, v) for k, v in self.__dict.items()][0] |
| 33 | + try: |
| 34 | + return FailureResponse.defaultResponseClass( |
| 35 | + content=FailureResponse.stringify(content), |
| 36 | + status_code=status_code) |
| 37 | + except KeyError: |
| 38 | + raise KeyError(f"No default response found for the given status code: {status_code}") |
| 39 | + |
| 40 | + @property |
| 41 | + def fastapi_descriptor(self): |
| 42 | + return {k: {"description": FailureResponse.stringify(v)} for k, v in self.__dict.items()} |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def listify(item: str | list[str]) -> list[str]: |
| 46 | + return item if isinstance(item, list) else [item] |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def stringify(item: str | list[str]) -> str: |
| 50 | + return FailureResponse.descriptor_delimiter.join(FailureResponse.listify(item)) |
| 51 | + |
| 52 | + |
| 53 | +class BaseFailureResponses: |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def fields(cls): |
| 57 | + return {k: v for k, v in cls.__dict__.items() if isinstance(v, FailureResponse)} |
| 58 | + |
| 59 | + @classmethod |
| 60 | + def union(cls): |
| 61 | + buff = FailureResponse({}) |
| 62 | + for k, v in cls.fields().items(): |
| 63 | + buff |= v |
| 64 | + return buff |
| 65 | + |
| 66 | + |
| 67 | +class CollectionFail(BaseFailureResponses): |
| 68 | + NOT_FOUND = FailureResponse({ |
| 69 | + status.HTTP_404_NOT_FOUND: |
| 70 | + "Collection name does not exist." |
| 71 | + }) |
| 72 | + |
| 73 | + WRONG_TYPE = FailureResponse({ |
| 74 | + status.HTTP_422_UNPROCESSABLE_ENTITY: |
| 75 | + "Collection name must be either a string or None; or both database name and collection name must be None." |
| 76 | + }) |
| 77 | + |
| 78 | + |
| 79 | +class DatabaseFail(BaseFailureResponses): |
| 80 | + NOT_FOUND = FailureResponse({ |
| 81 | + status.HTTP_404_NOT_FOUND: |
| 82 | + "Database name does not exist." |
| 83 | + }) |
| 84 | + |
| 85 | + WRONG_TYPE = FailureResponse({ |
| 86 | + status.HTTP_422_UNPROCESSABLE_ENTITY: |
| 87 | + "Database name must be either a string or None." |
| 88 | + }) |
| 89 | + |
| 90 | + |
| 91 | +class DocumentsFail(BaseFailureResponses): |
| 92 | + NOT_FOUND = FailureResponse({ |
| 93 | + status.HTTP_404_NOT_FOUND: |
| 94 | + "Could not find any document with the given object id." |
| 95 | + }) |
| 96 | + |
| 97 | + |
| 98 | +Database_Collection_Fail = DatabaseFail | CollectionFail |
| 99 | +Database_Collection_Document_Fail = DatabaseFail | CollectionFail | DocumentsFail |
| 100 | + |
| 101 | +database_collection_fail_descriptor = ( |
| 102 | + DatabaseFail.union() | CollectionFail.union() |
| 103 | +).fastapi_descriptor |
| 104 | + |
| 105 | +database_collection_document_fail_descriptor = ( |
| 106 | + DatabaseFail.union() | CollectionFail.union() | DocumentsFail.union() |
| 107 | +).fastapi_descriptor |
0 commit comments