Project to use as submodule in FastApi solutions.
Architeture of the project if given below.
├──common_http
└──schemas.py
└──definitions.py
└──responses.py
While in the responses file there are methods defined to be used in the api routes, the other two contains definitions for the OpenAPI document generation.
-
In the schemas.py file it is defined the structure of the possible responses, always thinking in a JSONResponse method;
-
In the definitions.py there are dictionaries describing possible responses codes for each type of request [GET, POST, PUT, DELETE] and its definitions.
The following command will install from the main branch. Be careful.
pip install git+ssh://git@github.com/Ourinvest/common-http.git
You can choose manually the branch too, using:
pip install git+ssh://git@github.com/Ourinvest/common-http.git@development
As this is private repository, correct authentication will be required (a.k.a Token).
Create a token here, or get one from AWS Secret Manager
pip install git+https://${GITHUB_USER}:${GITHUB_TOKEN}@github.com/Ourinvest/common-http.git
Or, for specfic branch:
pip install git+https://${GITHUB_USER}:${GITHUB_TOKEN}@github.com/Ourinvest/common-http.git@${BRANCH_NAME}
As stated, in the responses.py file there is the class CustomResponse, in which there are static methods to be called generating a specific response.
Successes
For success responses [2xx] it is returned a JSONResponse indicating its status code and a message given.
return CustomResponse.success('Request processed successfully')
Or even return customized responses giving its status code.
return CustomResponse.custom_response(222, 'This is fun!')
Errors
While for error responses [4xx or 5xx], it is raised an HTTPException indicating its given detail.
raise CustomResponse.bad_request('That was a bad bad request, sir!')
Or
raise CustomResponse.custom_error(567, 'WOW, that wasn\'t supposed to happen!')