Python Module — WSME

举报
云物互联 发表于 2022/01/15 00:10:41 2022/01/15
【摘要】 目录 文章目录 目录WSMEWSME 的使用 WSME WSME(Web Service Made Easy)是一个 RESTful API Service Typing 库,用于对 H...

目录

文章目录

WSME

WSME(Web Service Made Easy)是一个 RESTful API Service Typing 库,用于对 HTTP Request Body、Response Body、Response Status Code 进行规范化的校验和约束。

WSME 的设计理念是:在大多数情况下,Web Services 对输入/输出数据类型的要求都是严格的。

  • 官方文档:https://wsme.readthedocs.io/en/latest/index.html

WSME 的使用

WSME 提供了 2 个装饰器:

  1. @signature:用来描述一个方法或函数的输入/输出数据类型。注意,大多数情况下不建议直接使用该装饰器,而是使用为不同的 Web 框架进行封装的 @wsexpose。
class wsme.signature([return_type, [arg0_type, [arg1_type, ..., ]]]body=None, status_code=None)
# return_type – Type of the value returned by the function
# argN – Type of the Nth argument
# body – If the function takes a final argument that is supposed to be the request body by itself, its type.
# status_code – HTTP return status code of the function.
# ignore_extra_args – Allow extra/unknow arguments (default to False)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  1. @wsexpose:包含了 @signature 的功能,也具有相同的形参列表,同时还会把方法或函数的 Route Infors 暴露给 Web 框架,类似于 Pecan 提供的 @expose 装饰器,被装饰的方法或函数可以被 Controller Router 找到。
# wsmeext.pecan.wsexpose(return_type, *arg_types, **options)
@wsexpose(ResponseBodyType,
          Param01Type, Param02Type, ..., Param0NType,
          body=RequestBodyType,
          status_code={{ successfully_status_code_num }},
          ignore_extra_args=True)
def http_method(self, param01, param02, ..., param0N, body):
    ...

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

NOTE:对于 Error 或 Exception 的 Response Context 和 Status Code,通常直接在 raise exceptions 中定义,由 Pecan 框架捕获之后进行响应,而不会经过 WSME 的处理,所以 WSME 也没有这方面的逻辑。

文章来源: is-cloud.blog.csdn.net,作者:云物互联,版权归原作者所有,如需转载,请联系作者。

原文链接:is-cloud.blog.csdn.net/article/details/122493510

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。