实用程序库用于处理http api的rfc7807问题详细信息

httpproblem的Python项目详细描述


python httpproblem

使用RFC7807 Problem Details for HTTP APIs的实用程序库。

Build Statussonar-quality-gatesonar-coveragesonar-bugssonar-vulnerabilities

这个库很轻,没有外部依赖关系, 经过全面测试,可与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'}

Predefined 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).

所以如果这个库将自动填充标题字段 不存在或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_dictto_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'}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java岩石、布、剪刀程序,带文件输入   java迭代Hashmap中的值列表   数组不能正确循环   JavaHibernate:对于一个遥远的惰性依赖,有一个渴望的获取者吗?   交通阻塞下的java车辆路径选择   如何在java中创建布尔方法来检查添加到具有特定索引的arraylist的对象   java Tomcat配置文件/上下文xml似乎已经崩溃了。请确保它是可分析和有效的。有关详细信息,请参阅服务器日志   为什么坐标有时相等,有时不相等?   java libgdx CameraInputController过于敏感,平移开始得太早   将C++排序函数移植到java   java应该是十进制的   swing Java JFrame中的所有内容都不可见   java使用数组中除一个元素以外的所有元素   从子类运行构造函数的Java