Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

pyRest part 2: Hooking the API

DZone's Guide to

pyRest part 2: Hooking the API

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

In part 1, a very unexciting base CherryPy implementation was all we had, but now it's time to hook up something real! Instead of creating a mock API to work against as example code, I've decided to use hgapi to access the pyrest repo itself as example implementation - very meta!

I've decided to hook the API in before I refactor the code to separate the web framework from pyRest, since I firmly belive in getting things working first and cleaning up after. I did create a namedtuple to hold basic response values so that the requesthandler function can be extracted later.

The 'interesting' part looks like this
Response = namedtuple('response', 'status content')
def requesthandler(method, *pathargs, **kwargs):
    """Main dispatch for calls to PyRest; no framework specific
    code to be present after this point"""
    if not method == 'get':
        return Response('500 Server Error', 'Not implemented')
    repo = hgapi.Repo('.')
    return Response('200 Ok', repo.hg_id())

..not much yet, but it responds to any GET request with the current Mercurial node id.

My intention is that the final result will be three separate parts - a routing/domain specific part that uses hgapi, pyRest proper which handles requests and autohooks up the routing, a CherryPy part which integrates with CherryPy and will need to be reimplemented for every web framework supported.

That will be at at least one update before that though, because next will be "autowiring" the routing logic. Code for the project is available at Bitbucket

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}