实用程序库用于处理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 Struts2从服务器重定向到外部URL   java无法捕获从一个方法到另一个方法的返回值   无法在Java中使用HtmlUnit从URL获取id为的元素   java如何从函数中添加片段?   java在一个页面中连接过多?   java如何在javafx中基于时间更改场景   java刷新活动,活动从片段开始   Android中的java Flandmark   初始化和运行springboot应用程序时出现java问题   java如何使用SD卡的直接访问权限   java无法将void转换为“classname”   java从外部向存在于EventDispath线程中的JFrame添加额外的面板?   带有servlet和线程的java Wildfly连接池   java在子类中获取WriteTo文件的内容   多线程从java中ScheduledThreadPoolExecutor>ExecutorService向下发送中断(kill 15或9)信号(Ctrl+C)   java为什么我可以使用类引用在类内部交换字段值,但不能交换引用本身   java PowerMock禁止构造函数,但设置私有最终字段   Javai使用gson解析嵌套的json数据,但返回null   访问者Java私有修饰符混淆