by tnhashmi QuickStartQuick-start Guide for Webservice LoaderPython Webservice Loader
2. wsloader Design and Architecture
- 2.1 Implementation Format
- 2.2 Application State Management
- 2.3 URL Mapping
- 2.4 HTTP Request Method Restriction
- 2.5 Request Parameter and Response Handling
3. Installation Steps 4. Service Configuration 5. Future Improvements 1. Introduction
It is developed by Lulu India for providing user data management services to weRead.
1.1 Objectives
The objectives of wsloader are:
- Minimal/Zero extra code and configuration to expose Python interfaces as a webservice
- Clean separation of webservice delivery mechanism and service implementation
- Full encapsulation of webservice protocols and mechanisms for authentication, authorisation, serialisation, end-point discovery and loading, logging and load balancing
1.2 Features
- loading any Python module/class as a service
- handling of service independent request and response marshalling via JSON
- automatic URL creation based on the module path (can be overridden)
- automatic enforcing of GET and POST methods for read and write endpoints
- proper HTTP error responses for service invocation failures
- high-performance application loading and state management
- apache error log access to module code via sys.stderr
1.3 Why Not Django?
- Django is a Web Application framework, not a Web Service framework
- Django does a good job of mapping coarse-grained URLs to application logic but it does a poor job of simplifying fine-grained API invocation
- Django does a great job of creating dynamic, data rich web pages with styling and interaction elements which are not needed for APIs consumed by programs instead of humans
- Django requires programming in a specific style which makes it easier to do the heavy-lifting for web application development but is too complex for web service API delivery
- Django applications can not be deployed in non-Django environments. They are written specifically for Django
wsloader itself is not a framework built from scratch, though. It is built upon Werkzeug micro-framework. Werkzeug was chosen based on a comparison by principles of over a dozen Python web application frameworks and web micro-frameworks, followed by an in-depth evaluation of 3 short-listed frameworks -- Django, Pylons and Wekzeug.
2. wsloader Design and Architecture
2.1 Implementation Format
2.2 Application State Management
Services that are implemented as classes get instantiated once on each request.
2.3 URL Mapping
http://example.com/datetime/datetime/nowhttp://example.com/foo/bar/baz/hello/say_hellohttp://example.com/greeter/say_hello2.4 HTTP Request Method Restriction
add, post, submit, set, update, deleteFor all other methods, only GET method is allowed.
2.5 Request Parameter and Response Handling
2.5.1 Positional Parameters
http://example.com/greeter/say_hello?args=["World"]greeter.say_hello("World")2.5.2 Named Parameters
http://example.com/greeter/say_hello?addressee="mes+amis"&greeting="Bon+jour"is equivalent to the following Python code:
>>> greeter.say_hello(addressee="mes amis", greeting="Bon jour")3. Installation Steps
- sudo apt-get install werkzeug libapache2-mod-wsgi on Ubuntu
- sudo yum install werkzeug mod_wsgi on Fedora
- Try sudo easy_install werkzeug if it's not available in your OS's repository
Check out wsloader-wsgi from the mercurial repository:
- hg clone https://wsloader.googlecode.com/hg/ wsloader && cd wsloader/wsloader-wsgi
Install src/wsloader.py in /var/www/wsgi/ directory Install config/wsloader.conf in apache's conf.d directory, wherever it lives on your system
- sudo install -m 644 config/wsloader.conf /etc/httpd/conf.d/ on RHEL
- sudo install -m 644 config/wsloader.conf /etc/apache2/conf.d on Ubuntu
Restart httpd. WSGIScriptAlias / /var/www/wsgi/wsloader.pyWSGIScriptAlias /examplepath /var/www/wsgi/wsloader.pyPlease consult apache2 and mod_wsgi documentation for more advanced configurations
4. Service Configuration
4.1 Default Configuration
E.g. to expose foo.bar.baz.hello module, do:
$ sudo touch /etc/wsloader-wsgi/foo.bar.baz.hello4.2 Custom base URL for module/class
E.g. to remap foo.bar.baz.hello to service.greeter, do:
$ echo 'alias_to=foo.bar.baz.hello' > service.greeter$ sudo cp service.greeter /etc/wsloader-wsgi/Now the service can be called as
http://example.com/service/greeter/<endpoint>http://example.com/foo/bar/baz/hello/<endpoint>4.3 Exposing a class
$ cat /etc/wsloader-wsgi/pgreetertype=classalias_to=foo.bar.baz.hello.pGreeterinit_param=greetinghttp://example.com/pgreeter/say_hello?greeting="Hola"&args=["amigos"]This is equivalent to the following Python code:
>>> foo.bar.baz.hello.pGreeter("Hola").say_hello("amigos")5. Future Improvements
- Online documentation browsing via pydoc or other suitable tools
- Request authentication and authorisation
Comments [0]
Comments [0]