实用程序库用于处理http api的rfc7807问题详细信息
httpproblem的Python项目详细描述
python httpproblem
使用RFC7807 Problem Details for HTTP APIs的实用程序库。
这个库很轻,没有外部依赖关系, 经过全面测试,可与Python2和Python3一起使用。它有特别的 支持AWS lambda proxy integration output format 但它应该很容易映射到任何其他格式或框架。目前 只支持json序列化。
安装
pip install httpproblem
用法
建立一个问题库
problem()函数,可用于使用 问题字段。
>>>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'}
您还可以使用问题扩展
>>>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'}指定
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).
所以如果这个库将自动填充标题字段 不存在或about:blank。
>>>problem(404){'status':404,'title':'Not Found'}>>>problem(httplib.BAD_REQUEST,type='about:blank'){'status':400,'type':'about:blank','title':'Bad Request'}
生成有问题的http响应
problem_http_response()函数帮助构建http响应 使用aws lambda代理集成使用的格式。方法 将自动用Content-Type头填充 application/problem+json和状态为的http响应代码。
>>>pprint(problem_http_response(httplib.BAD_REQUEST)){'body':'{"status": 400, "type": "about:blank", "title": "Bad Request"}','headers':{'Content-Type':'application/problem+json'},'statusCode':400}
您可以将其映射到其他框架。例如烧瓶(或 韦克泽格):
>>>problem=problem_http_response(400)>>>print(flask.Response(problem['body'],status=problem['statusCode'],headers=problem['headers']))<Response39bytes[400BADREQUEST]>
默认情况下,json.dumps用于序列化为json。这可能是 使用set_serialize_function
更改>>> httpproblem.set_serialize_method(lambda data: json.dumps(data, indent=4)) >>> print(problem_http_response(400)['body']) { "status": 400, "title": "Bad Request" }
引发问题异常
可以使用Problem异常类来简化错误 使用try/except进行管理。类有方法将其转换为 问题dict或http响应。
>>>try:...raiseProblem(httplib.BAD_REQUEST)...exceptProblemase:...print(e.to_dict())...{'status':400,'title':'Bad Request'}
to_dict和to_http_response接受with_traceback 可用于包含回溯的参数。你也可以设置它 全局使用activate_traceback()函数。为了安全 原因是,默认情况下不包括回溯 建议在生产中不要激活它。
>>># 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):\n File "<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):\n File "<stdin>", line 2, in <module>\nProblem: {\'status\': 400, \'title\': \'Bad Request\'}\n'}