I'm trying to standardize my handling of HTTP status codes returned from various APIs in an effort to reduce the amount of code I copy across files.
For my current application, I have a set of files that each contain one class that all derive inheritance from one master class. I'm using the Python requests module to consume the APIs.
So far, I've written custom status code handling in each function to continue with a 200, log the request I sent with a 400, log the url for a 404, retry for 5xx, but it's a hassle to keep copying this code across functions and classes.
I'm thinking of the following (note that I've simplified my code here to just use GETs, but in reality, I'm mostly POSTing and receiving back a json response):
apiMaster.py
class ApiMaster(object):
def _handle_response(self, resp):
if resp.status_code == 200: # or requests.code.ok
return resp.json()
if resp.status_code == 400:
err_msg = "400 Error - Bad Request\n" + resp.request.url + "\n" + resp.request.data
raise HTTPError(err_msg)
...
apiA.py
class Api_A(ApiMaster):
def query_json_a(self):
resp = requests.get(self.url + '/a.json')
try:
resp_json = self._handle_response(resp)
except HTTPError as e:
logger.error(str(e))
apiB.py
class Api_B(ApiMaster):
def query_json_b(self):
# same as API A but with different endpoints and purpose
However, this still seems iffy. I don't know if it's reasonable to try to account for every status code returned by an API. And this would also require me to copy this code to any new project I start working on.
Is there a better way to do this?