Sunday, October 13, 2013

Python HTTP Client API

The modern web services expose public API to the world and JSON is de-facto standard in this communication. Here is a simple use case that integrates with buildbot public API.

Lets setup a virtual environment and install wheezy.core package:
virtualenv env
env/bin/easy_install wheezy.core
Launch python from virtual environment (env/bin/python) and try this:
>>> from wheezy.core.httpclient import HTTPClient
>>> c = HTTPClient('')
>>> c.get('project')
>>> project = c.json
>>>> str(project.title)
Here is another example that demonstarates etag handling (the second time we request events the server responds with HTTP status code 304, not modified):
>>> base = ''
>>> c = HTTPClient(base)
>>> c.get('events')
>>> c.headers['content-encoding']
>>> c.get('events')
The HTTPClient supports HTTP(S) GET/HEAD/POST verbs, follows redirects, handles cookies and etags between requests, gzip content decoding.