一个扭曲的web-rest微框架
CorePost的Python项目详细描述
基于flaskapi,计划集成多处理支持,以充分利用所有cpu。 在coretwisted.webapi之上提供了一个更具flask/sinatra风格的api。 集成formencode以进行路径、窗体和查询参数验证。
多模块twisted.web corepost rest应用程序示例 它公开了两个独立的rest服务(针对客户和客户地址实体):
class CustomerRESTService(): path = "/customer" @route("/") def getAll(self,request): return DB.getAllCustomers() @route("/<customerId>") def get(self,request,customerId): return DB.getCustomer(customerId) @route("/",Http.POST) def post(self,request,customerId,firstName,lastName): customer = Customer(customerId, firstName, lastName) DB.saveCustomer(customer) return Response(201) @route("/<customerId>",Http.PUT) def put(self,request,customerId,firstName,lastName): c = DB.getCustomer(customerId) (c.firstName,c.lastName) = (firstName,lastName) return Response(200) @route("/<customerId>",Http.DELETE) def delete(self,request,customerId): DB.deleteCustomer(customerId) return Response(200) @route("/",Http.DELETE) def deleteAll(self,request): DB.deleteAllCustomers() return Response(200) class CustomerAddressRESTService(): path = "/customer/<customerId>/address" @route("/") def getAll(self,request,customerId): return DB.getCustomer(customerId).addresses @route("/<addressId>") def get(self,request,customerId,addressId): return DB.getCustomerAddress(customerId, addressId) @route("/",Http.POST) def post(self,request,customerId,addressId,streetNumber,streetName,stateCode,countryCode): c = DB.getCustomer(customerId) address = CustomerAddress(streetNumber,streetName,stateCode,countryCode) c.addresses[addressId] = address return Response(201) @route("/<addressId>",Http.PUT) def put(self,request,customerId,addressId,streetNumber,streetName,stateCode,countryCode): address = DB.getCustomerAddress(customerId, addressId) (address.streetNumber,address.streetName,address.stateCode,address.countryCode) = (streetNumber,streetName,stateCode,countryCode) return Response(200) @route("/<addressId>",Http.DELETE) def delete(self,request,customerId,addressId): DB.getCustomerAddress(customerId, addressId) #validate address exists del(DB.getCustomer(customerId).addresses[addressId]) return Response(200) @route("/",Http.DELETE) def deleteAll(self,request,customerId): c = DB.getCustomer(customerId) c.addresses = {} return Response(200) def run_rest_app(): app = RESTResource((CustomerRESTService(),CustomerAddressRESTService())) app.run(8080) if __name__ == "__main__": run_rest_app()
bdd展示了它的不同功能
https://github.com/jacek99/corepost/blob/master/corepost/test/feature/rest_app.feature
更改日志
- 0.0.16:
- 针对问题4(将对象图序列化为xml)的小错误修复,删除了jinja2作为依赖项:
- https://github.com/jacek99/corepost/issues/4
- 0.0.15:
- 在自动将响应转换为json和解析带有意外字符的参数/路径时修复了小错误
- 0.0.14:
- 自动解析查询、表单、json、yaml和xml参数: http://jacek99.github.com/corepost/argument_parsing.html
- 0.0.13:
- perf fix在进行url路由时避免不必要的字符串连接,在代码检查之后(感谢gerald tremblay)
- 0.0.12:
- 向后不兼容的更改:为嵌套的rest服务添加了高级url路由。 corepost对象不见了,rest服务现在只是标准类。 它们在暴露时被包裹在restresource对象中(见上面的示例)。
- 0.0.11:
- 添加了对请求/响应筛选器的支持
- 0.0.10:
- 删除了对txzmq的依赖性,而此时还不需要它
- 0.0.9:
- 修复问题3(错误的类作为“self”传递到路由器方法):
- https://github.com/jacek99/corepost/issues/3
- 0.0.8:
- 支持基于调用方的accept头将类序列化为json、xml、yaml
- 从CorePost资源对象中分离路由功能,为将来的多核支持做好准备
- 0.0.7:
- 自动解析传入内容(json、yaml、xml)
- 按传入内容类型路由
- 基于调用方接受头的自动响应转换(json/yaml
- 在@inlinecallbacks route方法中支持defer.returnValue()
- 0.0.6-围绕类和方法而不是函数和全局对象重新设计的api(在twisted dev的反馈之后)
- 0.0.5-添加了参数的formencode验证
- 0.0.4-路径参数提取,强制参数错误检查