As I’ve said in my OSDC report, after I presented SPORE I’ve had some positive feedback. In the last ten days, I’ve created a google group to discuss the current specification and implementations, a SPORE account on github to hold the implementation specifications and the API descriptions files, and more importantly, we have some new implementations:
in addition to the already existing implementations:
In this post, I’ll try to show some common usages for SPORE, in order to give a better explanation of why I think it’s needed.
As you can see in the previous example, I do the same request on the GitHub API: fetch informations from the user “mojombo”. In the three languages, the API for the client is the same:
- you create a client using the github.json API description
- you enable some middlewares
- you execute your request: the method name is the same, the argument names are the same!
- you manipulate the result the same way
Easy to switch from a language to another
You can switch from a language to another without any surprises. If you must provide an API client to a third-party, you don’t have to care about what languages they use, you only need to provide a description. Your methods call will be the same between all the languages, so it’s easy to switch between languages, without the need to chose an appropriate client for your API (if one exists), to read the documentation (when there is one), and having the client implementation going in your way.
What annoys me the most when I want to use an API, is that I have to choose between two, three, or more clients that will communicate with this API. I need to read the documentations, the code, and test thoses implementations to decide which one will best fit my needs, and won’t go in my way. And what if I need to do caching before the content is deserialized ? And what if the remote API changes it’s authentication method (like twitter, from basic auth to OAuth) and the maintainer of the client doesn’t update the code ?
Easy to use with APIs that are compatible
If you want to use the Twitter public timeline:
And now on statusnet:
easy, right ? As both APIs are compatible, the only thing you need to do is change the argument base_url when you create your new client.
It’s easy to write a description
It’s really easy to write a description for your API. Let’s take a look at the
one for github:
The important parts are the basic API description (with a name, a base url for the API) and the list of available methods (here I’ve only put the ‘follow’ method).
We also have a schema to validate your descriptions.
By default, your SPORE client will only do a request and return a result. But it’s easy to alter the default behavior with various middlewares. The most obvious one is the deserialization for a response, like the previous example with github and the middleware Format::JSON.
Control your workflow
The use of middlewares allow you to control your workflow as with Plack/Rack/WSGI. You can easily imagine doing this:
- check if the request has already been made and cached
- return the response if the cache is still valid
- perform the request
- send the content to a remote storer in raw format
- cache the raw data locally
- deserialize to json
- remove some data from the response
- give the response to the client
Or to interrogate a site as an API:
- send a request on a web page
- pass the response to a scraper, and put the data in JSON
- return the JSON with scraped data to the client
Creating a repository on Github
In this example, we use a middleware to authenticate on the GitHub API:
The middleware Auth::Basic will add the authorization header to the request, using the given tokens.
SPORE + MooseX::Role::Parameterized
I really like MooseX::Role::Parameterized. This module allows you to build dynamically a Role to apply to your class/object:
This Role will add two new attributes to my class: couchdb and url_solver, reading from a config file a list of middlewares to apply and the options (like base_uri).
Testing my application that uses CouchDB
This is a common case. In your application you use CouchDB to store some information. When you run the tests for this application, you don’t know if there will be a couchdb running on the host, if it will be on the default port, on what database should you do your tests, …
The Perl implementation of SPORE comes with a Mock middleware:
The middleware catches the request, checks if it matches something defined by the user and returns a response.
I really see SPORE as something Perlish: a glue. The various implementations are a nice addition, and I’m happy to see some suggestions and discussions about the specifications.
I’m pretty confident that the current specification for the API description is stable at this point. We still need to write more middlewares to see if we can cover most of the usages easily, so we can decide if the specification for the implementation is valid.
(as always, thanks to bl0b!).