Package: httpproblem
python-httpproblemUtility library to work withRFC7807 Problem Details for HTTP
APIs.This library is very light-weight, with no external dependencies,
fully-tested and works with both Python 2 and Python 3. It has special
support forAWS lambda proxy integration output
formatbut it should be easy to map to any other format or framework. Currently
only JSON serialization is supported.Installationpip install httpproblemUsageBuild a Problem dictTheproblem()function that can be used to build a dict with the
problem fields.>>>pprint(problem(httplib.BAD_REQUEST,'You do not have enough credit.','Your current balance is 30, but that costs 50.','/account/12345/msgs/abc')){'detail':'Your current balance is 30, but that costs 50.','status':400,'title':'You do not have enough credit.','type':'/account/12345/msgs/abc'}You can also use problem extensions>>>pprint(problem(httplib.BAD_REQUEST,'You do not have enough credit.','Your current balance is 30, but that costs 50.','/account/12345/msgs/abc',balance=30,accounts=['/account/12345','/account/67890'])){'accounts':['/account/12345','/account/67890'],'balance':30,'detail':'Your current balance is 30, but that costs 50.','status':400,'title':'You do not have enough credit.','type':'/account/12345/msgs/abc'}As specified byPredefined Problem
Types:The “about:blank” URI, when used as a problem type, indicates that
the problem has no additional semantics beyond that of the HTTP
status code.When “about:blank” is used, the title SHOULD be the same as the
recommended HTTP status phrase for that code (e.g., “Not Found” for
404, and so on), although it MAY be localized to suit client
preferences (expressed with the Accept-Language request header).So if this library will automatically fill the title field if the type
is not present orabout:blank.>>>problem(404){'status':404,'title':'Not Found'}>>>problem(httplib.BAD_REQUEST,type='about:blank'){'status':400,'type':'about:blank','title':'Bad Request'}Build a Problem HTTP responseTheproblem_http_response()function helps to build HTTP responses
using the format used by the AWS lambda proxy integration. The method
will automatically fill theContent-Typeheader withapplication/problem+jsonand the HTTP response code with the status.>>>pprint(problem_http_response(httplib.BAD_REQUEST)){'body':'{"status": 400, "type": "about:blank", "title": "Bad Request"}','headers':{'Content-Type':'application/problem+json'},'statusCode':400}You can map this to other frameworks. For instance for Flask (or
Werkzeug):>>>problem=problem_http_response(400)>>>print(flask.Response(problem['body'],status=problem['statusCode'],headers=problem['headers']))<Response39bytes[400BADREQUEST]>By default,json.dumpsis used to serialize into JSON. This can be
changed by using theset_serialize_function>>> httpproblem.set_serialize_method(lambda data: json.dumps(data, indent=4))
>>> print(problem_http_response(400)['body'])
{
    "status": 400,
    "title": "Bad Request"
}Raise a Problem exceptionTheProblemexception class can be used to simplify the error
management with try/except. The class has methods to convert it to a
Problem dict or HTTP response.>>>try:...raiseProblem(httplib.BAD_REQUEST)...exceptProblemase:...print(e.to_dict())...{'status':400,'title':'Bad Request'}Theto_dictandto_http_responsetake awith_tracebackargument that can be used to include the traceback. You can also set it
globally with theactivate_traceback()function. For security
reasons, the default is to not include the traceback and it is
recommended to not activate it in production.>>># Add traceback by call argument>>>try:...raiseProblem(httplib.BAD_REQUEST)...exceptProblemase:...pprint(e.to_dict(with_traceback=True))...{'status':400,'title':'Bad Request','traceback':'Traceback (most recent call last):\nFile "<stdin>", line 2, in <module>\nProblem: {\'status\': 400,\'title\':\'Bad Request\'}\n'}>>>>>># Add traceback globally>>>httpproblem.activate_traceback()>>>try:...raiseProblem(httplib.BAD_REQUEST)...exceptProblemase:...pprint(e.to_dict())...{'status':400,'title':'Bad Request','traceback':'Traceback (most recent call last):\nFile "<stdin>", line 2, in <module>\nProblem: {\'status\': 400,\'title\':\'Bad Request\'}\n'}