zoukankan      html  css  js  c++  java
  • WSGI学习系列WSME

    Introduction

      Web Services Made Easy (WSME) simplifies the writing of REST web services by providing simple yet powerful typing,

      removing the need to directly manipulate the request and the response objects.

    Protocols

      WSEM support lots of protocols.

      'restjson':   Implements a REST+Json protocol.

      'restxml':   Implements a REST+Xml protocol.

      'soap':    Implements the SOAP protocol.

    Conception

    WSRoot:   Root controller for webservices

    @expose:   The return type of web service interface

    @validate:  Validate the incoming paramters type

    @signature:  Set the return type and the incoming paramters type

    Example

      WSME provide details implements for Web Service.

      WSME need a Web Framework to provide service.

      In the OpenStack, Horizon use Django web framework.

      The following example use bottle web framework.

    from wsme import WSRoot, expose, validate, signature
    from wsme.types import File
    
    import bottle
    from six import uclass Person(object):
        id = int
        firstname = unicode
        lastname = unicode
    
        hobbies = [unicode]
    
        def __repr__(self):
            return "Person(%s, %s %s, %s)" % (
                self.id,
                self.firstname, self.lastname,
                self.hobbies
            )
    
    class DemoRoot(WSRoot):
        @expose(int)
        @validate(int, int)
        def multiply(self, a, b):
            return a * b
    
        @expose(File)
        @validate(File)
        def echofile(self, afile):
            return afile
    
        @expose(unicode)
        def helloworld(self):
            return u"Здраво, свете (<- Hello World in Serbian !)"
    
        @expose(Person)
        def getperson(self):
            p = Person()
            p.id = 12
            p.firstname = u'Ross'
            p.lastname = u'Geler'
            p.hobbies = []
            print p
            return p
    
        @expose([Person])
        def listpersons(self):
            p = Person()
            p.id = 12
            p.firstname = u('Ross')
            p.lastname = u('Geler')
            r = [p]
            p = Person()
            p.id = 13
            p.firstname = u('Rachel')
            p.lastname = u('Green')
            r.append(p)
            print r
            return r
    
        @expose(Person)
        @validate(Person)
        def setperson(self, person):
            return person
    
        @expose([Person])
        @validate([Person])
        def setpersons(self, persons):
            print persons
            return persons
    
        @signature(int, int, int)
        def increment(self, value, delta=1):
            return value + delta
    
        @signature(Person, body=Person)
        def updateauthor(self, person):
            return person
    
    # Set Web Root Path root
    = DemoRoot(webpath='/ws')
    # Add Soap protocol root.addprotocol(
    'soap', tns='http://example.com/demo', typenamespace='http://example.com/demo/types', baseURL='http://127.0.0.1:8080/ws/', ) # Add Rest Json protocal root.addprotocol('restjson') # Create a wsgi Application bottle.mount('/ws/', root.wsgiapp()) # Run Applicaton in bottle bottle.run()
  • 相关阅读:
    阶段3 3.SpringMVC·_03.SpringMVC常用注解_1 RequestParam注解
    阶段3 3.SpringMVC·_02.参数绑定及自定义类型转换_7 获取Servlet原生的API
    函数传参
    利用 操作符特性 代替if判断语句
    for(;;)和 while(1) 有什么区别吗?for()和while()的使用情景。
    一个简单的mfc单页界面文件读写程序(MFC 程序入口和执行流程)
    ARM异常---一个DataAbort的触发过程:
    C语言,单链表操作(增删改查)(version 0.1)
    Cpu实验
    H-JATG:NAND_FLASH的参数设置
  • 原文地址:https://www.cnblogs.com/edisonxiang/p/4724769.html
Copyright © 2011-2022 走看看