One of our favorite HTTP clients is Kenneth Reitz's Requests library for Python. Requests bills itself as "HTTP for Humans" and compared to the aging urllib2 in the Python standard library, it's a breath of fresh air.
Starting with Requests 1.0, a new extensibility point was added called Transport Adapters. By registering an adapter you can modify how Requests processes each request without changing the high-level API. Cory Benfield's httpcache adapter is a great example of an adapter at work, seamlessly adding proper HTTP caching support.
A Runscope Transport Adapter
Creating Runscope URLs is easy, but we wanted to make it even easier and a Transport Adapter was the perfect way to do it. Here's what it looks like in action:
session = requests.Session() session.mount('http://', RunscopeAdapter("bucket_key")) session.mount('https://', RunscopeAdapter("bucket_key")) resp = session.get("https://api.github.com") print resp.content
Two lines to register the adapter with your bucket key and the rest of your code doesn't need to change at all. You can even scope it to specific APIs by changing the prefix in the
mount() call. Once registered, all requests to the given prefix will automatically be converted to use Runscope URLs for the bucket you specify.
But wait, there's more!
Another benefit of using the adapter is that it automatically converts a URL with a port number specified into the correct
Runscope-Request-Port header. Also, since the URL transformation happens later in the lifcycle, it doesn't interfere with hostname-based signatures found in things like OAuth 1.0a.
To get started, run
pip install requests-runscope to include it in your project. All of the source code is available on the GitHub project page. If you run into any issues, submit an issue or send us an email and we'll be happy to help.