dews 0.3.0
dead simple Web Services stack for Python
Although exposing a Web Service is getting pretty simple these days
when you're using the proper IDE and following the recipes, getting to
known the inner details of a SOAP stack can be a daunting task. For
instance, if you are experimenting with extensions or simply want to
learn how it works.
Inspired by Ryan Kelly's dexml, the dead simple Object-XML mapper, and
by K. Jacobson's simple Python Web Service framework blog entry, dews is
small and simple. In fact, there is a long way to go to comply with the
Web Services standards. Nonetheless, built-in support for Django makes it
straighforward to build SOAP views and to bridge Django models to XML.
Exporting a Web Service without Django is as easy as defining request and
response classes in Python using dexml with corresponding handler functions:
>>> from dexml import Model, fields
>>> from dews import Service
>>> class HelloNS(Model):
... class meta:
... namespace = "http://localhost/hello"
... namespece_prefix = "h"
>>> class Hello(HelloNS):
... name = fields.String()
>>> class HelloResponse(HelloNS):
... greeting = fields.String()
>>> def hello(r):
... return HelloResponse(greeting='Hello '+r.name)
>>> service = Service('Hello', (
... (Hello,HelloResponse,hello)
... ))
And then creating a basic HTTP server:
>>> from BaseHTTPServer import HTTPServer
>>> from dews.http import BaseHTTPWSHandler
>>> HTTPServer(('',8080), BaseHTTPWSHandler(service)).serve_forever()
This will serve the WSDL on HTTP GET. No client is provided
by dews but I recommend Suds (https://fedorahosted.org/suds/).
When using Django, database models can be mapped directly to XML
and a generic SOAP view is provided, reducing the amount of code
even further. Consider the following Django model:
>>> from django.db import models
>>> class Person(models.Model):
... first_name = models.CharField(max_length=100)
... last_name = models.CharField(max_length=100)
An dexml model is obtained as follows:
>>> from dews.django.models import XMLModel
>>> class PersonXML(XMLModel):
... class meta:
... model = Person
These can be converted back and forth as follows (save() is needed to
set the id AutoField):
>>> db = Person(first_name='John', last_name='Doe')
>>> db.save()
>>> xml = PersonXML(instance=db)
>>> print xml.render()
<?xml version="1.0" ?
><PersonXML><id>1</id><first_name>John</first_name><last_name>Doe</last_na
me></PersonXML>
or:
>>> xml = PersonXML(id=1, first_name='John', last_name='Doe')
>>> db = xml.instance()
>>> print db
Person object
PersonXML can also, obviously, be used as an element in other dexml models.
Namely, it can be used within requests/responses to create services that
interact with persistent state. Consider the following service that finds
a person by id:
>>> class Get(dexml.Model):
... id = fields.Integer(tagname='id')
>>> class GetResponse(dexml.Model):
... data = fields.Model(PersonXML)
>>> def get(request):
... try:
... return
GetResponse(data=PersonXML(instance=Test.objects.get(id=request.id)))
... except:
... raise Fault(faultcode="Client", faultstring="invalid id")
>>> service = Service('People', (
... (Get,GetResponse,get)
... ))
And then binding it to an URL in Django patterns:
>>> urlpatterns = patterns('',
... (r'^people$', 'dews.django.views.handler', {'service': service}),
... )
| File | Type | Py Version | Uploaded on | Size | # downloads |
|---|---|---|---|---|---|
| dews-0.3.0.tar.gz (md5) | Source | 2010-04-08 | 5KB | 465 | |
- Author: Jose Orlando Pereira
- Home Page: http://gitorious.org/dews
- Keywords: xml soap web services
- License: MIT
- Requires dexml (>=0.3.2)
- Categories
- Package Index Owner: jop
- DOAP record: dews-0.3.0.xml
