I’m almost done with the parts to make this project PyPI ready – it can now work on your application as long as you implement the actual code to route calls to the right part of your API – this works:
python -m pyrest.launcher --server pyrest.integration.cherry \ --api=pyrest.examples.hg
Configuration can also be done using a config file instead of or together with command-line arguments. This means that if you can describe your API’s usage using modules (representing resources) and post/put/get/delete functions in those modules, you can then pretty much just copy the line above and have a REST interface as long as you return data that the json library can understand how to serialize.
The magic is all in the new launcher.py, which reads configuration, instantiates pyrest, an appserver interface, and hooks up the specified API. Very little other changes needed to be made – none to pyrest.py, and for cherry.py the only changes was to move functionality from the ’if dunder main’ to a new start() function.
Time for a goal check!
- Create a tool that can expose a Python API in a RESTish fashion
- The API itself must not have to know about the tool
- It must handle HTTP errors
- It must run on Python 3.2+
#+BEGIN_QUOTE These are all done, with the new launcher module taking care of reading configuration/arguments and wiring up the correct backend server – although right now, there’s only the CherryPy version available.
- It must run on at least CherryPy and two other webapp frameworks TBD (no, not Django)
Not OK, just CherryPy implemented.
- It must be able to encode data into JSON before returning it
Partially OK, but there’s no clean way of specializing the serialization if you send data that is not handled by the standard library json module.
In addition to those goals, I’ll also have to create a setup.py to make the tool installable before I can call version 1.0.0
I’d also like to take the time to give a huge thanks to the people over at DZone – not only did they ask if they could repost my blogs (which they don’t have to since I allow anyone to do anything with it as long as they don’t blame me for damage done…), but they actually sent me goodies all the way over the Atlantic! They actually sent me nerf guns (and a t-shirt and other stuff) halfway across the globe just because they like my blog. Crazy. If everyone who likes this blog did that I’d probably have, like, three nerf guns!
That being said, code is as always available at Bitbucket.