WSGI学习系列WSME
2015-08-12 16:32
357 查看
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()
相关文章推荐
- [转载] Kafka+Storm+HDFS整合实践
- 作为软件测试人员,如何描述缺陷(Defect)?
- 实战体验几种MySQL Cluster方案
- Introduce to algorithm--------pseudo code to C/C++ code (chapter 16)
- HDU 5371 Hotaru's problem
- Linux - 设置Centos控制台模式的分辨率
- AFNetworking2.0源码解析
- Android工程的编译过程
- caffe layer
- Linux - 设置Centos控制台模式的分辨率
- 记录帖:Textview在android代码中可以动态的设置字号单位
- MySQL数据库游标
- [POJ 1944]Fiber Communications
- 【数据结构与算法】八皇后问题之递归
- codeforces 374D
- poj 1273 Drainage Ditches(网络流模版题)
- 利用Nagios + CloudWatch API 监控 Amazon 实例
- HDU 1711 Number Sequence
- Maven3.0.4基础原理分析
- 如何在Oracle官网下载历史版本的JDK?