一个扭曲的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:
  • 0.0.15:
    • 在自动将响应转换为json和解析带有意外字符的参数/路径时修复了小错误
  • 0.0.14:
  • 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:
  • 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-路径参数提取,强制参数错误检查

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

推荐PyPI第三方库


热门话题
java如何在Swing JText标签中显示集合<String>。   java IE10向Spring控制器发送双post请求   java Websphere MQ:限制分布式环境中消耗的最大消息数   java获取Android中选定画廊图像的文件路径和文件名   Java抽象泛型方法,使用具体类型实现通配符   java强制tomcat只使用HTTP 1.0或忽略除头之外的其他内容   如果每个子类型都有唯一的属性,java是获得独立子类型进行协作的正确方法   java如何在特定片段中隐藏动作栏   在构建Android应用程序时,java在特定清单条目上没有签名保护   java广播接收器触发两次   java将Spring升级到最新补丁版本安全吗?   java AlertDialog无法解析或不是字段   java如何实现TableDecorator?   java如何在Android日记应用程序中显示条目并进行编辑   java如何将文件从一个文件夹移动到另一个文件夹