b53ef86ef8
git-subtree-dir: utils_v2 git-subtree-split: 7f273565196085feb05ee3328aa2e80d3d721fc3
134 lines
5.5 KiB
Python
134 lines
5.5 KiB
Python
"""
|
|
|
|
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 ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# For data-modelling:
|
|
from pydantic import BaseModel
|
|
from typing import Any, Optional, List
|
|
|
|
# My utils:
|
|
from utils_v2.api.codes import StatusCodes, HttpCodes
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MACROS / ONE-TIME INIT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** VARIABLES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** CLASSES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
class ResponseModel(BaseModel):
|
|
|
|
"""
|
|
A model for how the response should be when developing API endpoints.
|
|
"""
|
|
|
|
# The fields that you want in your response:
|
|
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):
|
|
|
|
"""
|
|
Call this when you are using either Flask or Quart as your framework.
|
|
:return: The output as expected by Flask and Quart.
|
|
"""
|
|
|
|
# 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]
|
|
|
|
# Construct the basic structure:
|
|
response_dict = {
|
|
"status": 1 if self.status_code.value[0] else 0,
|
|
"code": response_http_code,
|
|
"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
|
|
|
|
# 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())
|