Fork me on GitHub

Nudge by Evite

A python services publisher that works with the wsgi interface

If you don't already love Nudge, this might push you in the right direction: The Pitch

Contact

Evite Inc. (github@evitelabs.com)

Download

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/evite/nudge

Reference Videos

Nudge

Documentation

  1. Args
  2. Endpoints
  3. Service Publisher

Args

Any arg your service would need to work is set up as an Arg. There are a few such as String, Boolean, Date, Integer and Json set up for you. These should really just sanitize your inputs before they ever get to the real service. So, any problems will error out before, and your service code can stay clean!

For instance, if you needed to know the authorized user's id for YOUR app, you would make a new arg that would look something like the following:


    class UserId(CustomArg):
        
        def __init__(self):
            def func(req, inargs):
                auth_header = req.headers.get('x_your_auth_header')
                if not auth_header:
                    return None
                return your_auth_class.authorize_user(auth_header) 
            self.argspec = func
    

Endpoints

This is exacly what you think it is. It's a descriptor class for a service endpoint. It publishes a specific service call to a uri, an http method (GET, POST, PUT etc.), and allows for a renderer on the way back. This first example is the simplest you can really do. It is from our hello world example. You have an index endpoint that you GET that ends up returning rendered html.


    Endpoint(name='Index',
        method='GET',
        uri='/$',
        function=hws.index,
        renderer=HTML(),
    )
    

This next example is a little more complicated. It is from our twitter example. This is also an index Endpoint, but it has Args and exceptions that *could* be thrown.


    Endpoint(
        'index',
        'GET',
        '/$',
        index,
        args=Args(
            UserTwitterClient(),
            nudge.arg.String('type', optional=True),
        ),
        renderer=Identity('text/html'),
        exceptions={
            TwitterAuthException: handle_exception,
        },
    )
    

Service Publisher

Ok, so now you have Endpoints. What do you do now? Ah, you make a simple list and ask nudge to serve them. Hmmm, really? Nooo, it can't be that easy. Yup! Really! It is that easy. Ask nudge to serve your endpoints and all of the sudden you have a full-fledged service running.


    from nudge import serve, Endpoint, Args
    service_description = [endpoint1, endpoint2, endpoint3, ..., endpointN]
    serve(service_description)
    

Because the thing being served is just a list, you can use standard list operations to push two+ services together to run many on one port all you want!


    from nudge import serve, Endpoint, Args

    #Some user service for example
    service1 = [endpoint1, endpoint2, endpoint3, ..., endpointN]
    #Some content service for example
    service2 = [endpointa, endpointb, endpointc, ..., endpointX]

    service1.extend(service2)

    serve(service1)