Noob Question: REST App Server ...

27 views
Skip to first unread message

Andrew

unread,
Feb 10, 2011, 2:40:10 PM2/10/11
to pylons-discuss
Gentlefolk,

I've searched your archive and I cannot find a discussion of how to
build just a REST app server in Pylons.

A little background: I am an iOS developer and I specialize in
developing apps that interface via REST to both my client's and public
app servers. When pressed, I will also write a simple REST app server.
For good reasons, which I support, my client prefers to write their
app server in Python. My experience is with Ruby. They, nonetheless,
still want me to write the app server in Python. Hence, my asking the
below questions.

I want a server to implement the following sample routes:

https://api.example.com/1/routeA/route1.json
https://api.example.com/1/routeB/route2.json
https://api.example.com/1/routeB/route3.json

I will potentially use all 4 REST verbs: GET, PUT, POST and DELETE.
Due to the mobile nature of my apps, most interactions will use the
idempotent methods GET and PUT. I only wish to have JSON responses --
no HTML, no text.

If I was doing this in Ruby, I would start with a nice little
framework called Sinatra. It is, in essence, just a routes server. If
I need to, I can bring in a Rails subsystem, such as ActiveRecord.

I want to have small stack on my Python based REST app server. I have
looked at both django-piston and django-tastypie. While I haven't
ruled them out, they are tied to Django. Hence, if I used them, I
would likely have a pretty heavyweight app server.

From my initial skim of your feature description, you support a routes
system. Because the Pylons community touts the modularity of Pylons, I
was hoping that I could run a system with just Python and the Pylons
routing engine. If I need access to a database model, I suspect I can
always bring in SQLAlchemy.



Hence, should Pylons be my development home? Does it really support a
modular style where I eject most of the framework from my runtime? Or
is it "yet another monolithic MVC framework"? In addition to the
Pylons book, is there another book I should be reading to grok the
Pythonic (Pylonic?) path? Has someone else implemented a thin REST app
server in Pylons?


Thank you in advance for any insight you may care to share,
Andrew

Wyatt Baldwin

unread,
Feb 11, 2011, 1:50:00 PM2/11/11
to pylons-...@googlegroups.com
> Has someone else implemented a thin REST app server in Pylons?

The short answer is: Yes. Basically, just trim the middleware to suit your needs, just leaving Routes and whatever else you might need. I can post up an example later.

Wyatt Baldwin

unread,
Feb 11, 2011, 3:23:26 PM2/11/11
to pylons-...@googlegroups.com
Here's an untested minimal setup for Pylons minus a few details (like imports). Throw in a config file, and this about all you need. I'd be interested in seeing an equivalent Pyramid setup.


# config/middleware.py

def make_app(global_conf, full_stack=True, **app_conf):
    config = load_environment(global_conf, app_conf)
    app = PylonsApp(config=config)
    app = RoutesMiddleware(app, config['routes.map'])
    app = RegistryManager(app)
    # auth middleware here if you need it
    return app


# config/environment.py
def load_environment(global_conf, app_conf):
    config = PylonsConfig()
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root, controllers=os.path.join(root, 'controllers'))
    config.init_app(global_conf, app_conf, package='mypkg', paths=paths)
    config['routes.map'] = make_map(config)
    config['pylons.app_globals'] = app_globals.Globals(config)
    return config


# config/routing.py
def make_map(config):
    map = Mapper(directory=config['pylons.paths']['controllers'],
                 always_scan=config['debug'])
    map.minimization = False
    map.explicit = False
    map.resource('thing', 'things')
    return map


# controller/things.py
class ThingsController(SomeRESTfulBaseController):
    """Implement/override index, show, create, delete, etc to suit..."""

Daniel Holth

unread,
Feb 11, 2011, 4:42:08 PM2/11/11
to pylons-...@googlegroups.com
Gentlenoob,

You can be confident that Pyramid will not run code that you don't need and have not configured. I would write your example a bit like the following. Pyramid makes the matched parameters from the route available in request.matchdict and the presumably json request body as request.body; you get to call simplejson.loads(request.body) and int(request.matchdict['foo']) yourself. The 'json' renderer serializes the return value to json for you.

Some people prefer to send all requests for a particular route to a class-based view that can delegate the different request methods to its own methods, but I just like using functions and view predicates such as request_method. The following doesn't seem too far away from what I know about sinatra from reading its landing page.

@view_config(route_name='routeA', renderer='json', request_method='POST')
def handle_POST(request):
    return {'post':'posted'}

@view_config(route_name='routeA', renderer='json', request_method='POST')
def handle_GET(request):
    return {'get':'got'}

... later, in the 'create WSGI app' function:

config.add_route('routeA', '/{foo:\d+}/routeA/routeA.json')
config.scan()

Andrew W. Donoho

unread,
Feb 11, 2011, 5:17:24 PM2/11/11
to pylons-...@googlegroups.com

On Feb 11, 2011, at 14:23 , Wyatt Baldwin wrote:

Here's an untested minimal setup for Pylons minus a few details (like imports). Throw in a config file, and this about all you need. I'd be interested in seeing an equivalent Pyramid setup.



On Feb 11, 2011, at 15:42 , Daniel Holth wrote:

I would write your example a bit like the following. 



Wyatt and Daniel,

WoW, thank you both for the code samples. Tomorrow, there is a Python Code Sprint in town. They are encouraging me to attend and I am.

Python folks are nice. 

I'll keep you folks posted as my decision process continues.


Anon,
Andrew
____________________________________
Andrew W. Donoho
Donoho Design Group, L.L.C.
a...@DDG.com, +1 (512) 750-7596

Knowing is not enough; we must apply. 
    Willing is not enough; we must do.
        -- Johann Wolfgang von Goethe

Patrice Neff

unread,
Feb 11, 2011, 7:16:52 AM2/11/11
to pylons-...@googlegroups.com
On Thu, Feb 10, 2011 at 8:40 PM, Andrew <andrew...@gmail.com> wrote:

> I've searched your archive and I cannot find a discussion of how to
> build just a REST app server in Pylons.

I hope I'll be forgiven for a shameless plug here. Personally I prefer
creating REST services using my WsgiService framework
<https://github.com/pneff/wsgiservice> rather than Pylons. Though I do
use Pylons extensively for frontend work.

If you would like to do this with Pylons, the following links might help you:
- http://pylonshq.com/docs/en/0.9.7/controllers/#using-the-rest-controller-with-a-restful-api
- http://pylonshq.com/docs/en/0.9.7/modules/decorators_rest/
- http://pylonshq.com/docs/en/0.9.7/modules/decorators/#pylons.decorators.jsonify

But so far I have no idea about how to port those approaches to Pyramid.

Patrice

Reply all
Reply to author
Forward
0 new messages