My favorites | Sign in
Project Hosting will be READ-ONLY Thursday at 3:00pm UTC for up to 3 hours for network maintenance.
Project Home Downloads Wiki Issues Source
Search
for
QuickStart  

Featured
Updated Jun 3, 2010 by tnhashmi@gmail.com

Creating a Service with wsloader

Here's an example based on what I wrote on-the-fly with my laptop projected on screen for a tech-talk about wsloader. It's not that I don't have stage-fright, it's just that wsloader is incredibly easy. So let's dip our toes in, shall we?

1. Getting Started

Before getting started with this example, you should have wsloader installed correctly. For this example we'll use a Python module, greeter.py, from the tests directory in wsloader source distribution. It should be copied to a standard directory where you would install python modules. For RHEL, this is the "site-packages" directory for Python in your system. The following command could help you find it:

locate site-packages | grep `python -V 2>&1 | awk '{print $2}' | cut -d . -f -2`

For Ubuntu, this directory is dist-packages.

2. Simplest Service

The simplest of these services is the hello_world function in greeter. To expose this in wsloader, you only have to create a file called "examples.ini" in /etc/wsloader-wsgi/. The file name actually doesn't matter. In its contents, just mention the name of the module to be loaded, in square brackets. It should look something like this:

$ cat /etc/wsloader-wsgi/examples.ini
[greeter]
$

When you restart httpd after creating this config file, you can use the URL http://localhost/services/greeter/say_hello for the following Python equivalent:

>>> import greeter
>>> greeter.say_hello()
'Hello World!'
>>> 

3. Exposing Classes, Aliasing and Parameters

greeter.py module contains a class, Greeter. By default, it would be referred by wsloader as localhost/services/greeter.Greeter. The class takes an init parameter, "greeting" and its say_hello() method takes an argument for "to_whom".

Normally, to expose this class, you'd add a line that reads [greeter.Greeter] in the config file. The resulting service URL would be:

http://localhost/services/greeter/Greeter/

How about making it slightly more pretty? We shall change greeter.Greeter to helloworld. To do that, replace [greeter.Greeter] with [helloworld] instead, and under it we add the following directive:

    alias_to=greeter.Greeter 

So the config file now looks like this:

$ cat /etc/wsloader-wsgi/examples.ini
[greeter]

[helloworld]
alias_to=greeter.Greeter
$

Since Greeter is a class that takes "greeting" as an __init__ parameter, we also need to specify the following:

    type=class
    init_param=greeting

With these three directives placed, the configuration finally looks like this:

$ cat /etc/wsloader-wsgi/examples.ini
[greeter]

[helloworld]
alias_to=greeter.Greeter
type=class
init_param=greeting
$

To call the web-service equivalent of the following invocation:

>>> from greeter import Greeter
>>> Greeter("Hello").say_hello("World")
'Hello World'
>>> 

you can use the following URL:

http://localhost/services/helloworld/say_hello?greeting="Hello"&to_whom="World"

That's it! We've covered the concepts that you require to expose your existing Python APIs as JSON-HTTP services. The complete config file is provided in tests/conf/examples.ini in the source distribution.


Sign in to add a comment
Powered by Google Project Hosting