""" AUTHOR: Khushal P Soonderji DATE: Thursday, 12th Sept., 2024 OBJECTIVE: To have a structure to the response sent from the API calls. REFERENCES: N/A DOWNLOADS: N/A """ # ***************************************************************************************************************** # ***** **** # *** IMPORT *** # ***** **** # ***************************************************************************************************************** from pydantic import BaseModel from typing import Any, Optional, List from utils_v2.api.codes import StatusCodes, HttpCodes # ***************************************************************************************************************** # ***** **** # *** MACROS / ONE-TIME INIT *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** VARIABLES *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** CLASSES *** # ***** **** # ***************************************************************************************************************** class ResponseModel(BaseModel): status_code: StatusCodes message: Optional[str | List] = None data: Optional[Any] = None seconds: Optional[float | int] = None log_id: Optional[str] = None http_code: Optional[HttpCodes] = None api_version: Optional[str] = None def for_quart(self): # Construct the basic structure: response_dict = { "status": 1 if self.status_code.value[0] else 0, "code": self.status_code.value[1], "message": self.message or self.status_code.name.replace("_", " ").lower(), "data": self.data, "apiVer": self.api_version } # Now add the additional fields: if self.seconds is not None: response_dict["seconds"] = self.seconds if self.log_id is not None: response_dict["logId"] = self.log_id # Figure out the HTTP code: response_http_code = self.http_code.value if self.http_code is not None else self.status_code.value[2] # Done here: return response_dict, response_http_code # ***************************************************************************************************************** # ***** **** # *** FUNCTIONS *** # ***** **** # ***************************************************************************************************************** # --- Nothing Yet # ***************************************************************************************************************** # ***** **** # *** MAIN PROGRAM *** # ***** **** # ***************************************************************************************************************** if __name__ == "__main__": my_response = ResponseModel( status_code = StatusCodes.RATE_LIMIT_EXCEEDED ) my_response.log_id = "abc123" print(my_response.for_quart())