RestConsumer is a generic python wrapper for consuming JSON REST APIs
Pull request Compare This branch is 2 commits behind becomingGuru:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
README.rst Plyground now has a simple example to try such an API Jan 31, 2012


RestConsumer: Generic REST API consumer.

RestConsumer is a generic python wrapper for consuming JSON REST APIs. It is built with python-requests, some magic and good intentions. This is the first alpha release; pull requests are welcome.


All modern REST API services already have an hierarchical meaningful url structure and return the response content in JSON format. Custom API wrappers for each service would necessitate the developer consuming the API, to refer to the documentation of the wrapper as well as the service itself. (Do I call the twitter.public_timeline() or twitter.get_public_timeline() of this particular library; what about it's pagination option?)

This package is an attempt to exploit the meaningful url structure and use some python magic to make it developer friendly to create a thin wrapper, on using which the developer would be enabled to consume REST API by looking at the service documentation alone.

Examples below highlight it's purpose and use.

For Github API v3: From the documentation, /users/:user/repos/ returns all the user's repos.

# Instantiate the consumer with the api end point
>>> github = RestConsumer(base_url='')

# use the consumer to query for the repos.
>>> github.users.kennethreitz.repos() -- Returns all the repos of the user kennethreitz.

Internally, requests.get(url='') is called and the content is returned in a python dict.

For Stackoverflow: returns unanswered questions for user with id 55562

>>> stack = RestConsumer(base_url='')

# When there are special characters, that can't be used as python methods, You can access it's item, like a dict.
>>> s.users['55562'].questions.unanswered()

# All the top answerers of the tag python can be obtained from:

For Twitter:

# Initialize twitter from it's end point. All twitter api calls need '.json' appended.
t = RestConsumer(base_url='',append_json=True)

# From the twitter documentation, /statuses/public_timeline provides all public timeline data

# Similarly, /user_timeline?screen_name=becomingGuru provides the timeline of the given user

# Since get is the default, you can skip that and just do:

# Since twitter also allows (undocumented) /user_timeline/becomingGuru, you can do:
t.statuses.user_timeline.becomingGuru() -- Which is equivallent to t.statuses.user_timeline.becomingGuru.get()

This is all you need to know about this package. That's the whole point. Spend your time reading through the documentation of the REST API that you are trying to consume rather than another class wrapper built around it. You get a dictionary of the received JSON. Wrapping those in classes is hardly pythonic or transparent, nor does it allow you to store it in a database without enough changes. Might as well deal with the received data directly.

Credits: API has been inspired by, about 2 years ago: While that one is twitter only, this is intended as a generic wrapper. With requests in the scene doing all the hard work, this should be simpler.


  • Returning mutated self is not a good idea. Return new objects. - Done.
  • Enable oAuth
  • Enable all parameters taken by requests. Lazy content fetch, with header only, ...
  • Provide, that provides various classes, that document end points of various services, so developer can import bing and get going.
  • Enable bash auto completion of different services by including various uri's
  • Include headers etc as params in the response given.
  • Explore wrapping response into ServiceClasses that behave like dict, list and other
  • Add more services, freebase, wikipedia