Resource

This module provide a common interface for all HTTP requests. This module make HTTP requests using Pycurl by default if installed or Httplib2.

Example:

>>> from restclient import Resource
>>> res = Resource('http://friendpaste.com')
>>> res.get('/5rOqE9XTz7lccLgZoQS4IP',headers={'Accept': 'application/json'})
'{"snippet": "hi!", "title": "", "id": "5rOqE9XTz7lccLgZoQS4IP", "language": "text", "revision": "386233396230"}'
>>> res.get('/5rOqE9XTz7lccLgZoQS4IP',headers={'Accept': 'application/json'}).http_code
200

Resource class

class restclient.rest.Resource(uri, transport=None, headers=None)

A class that can be instantiated for access to a RESTful resource, including authentication.

It can use pycurl, urllib2, httplib2 or any interface over restclient.http.HTTPClient.

Properties

uri
Str, full uri to the server.
transport
Any instance of object based on restclient.transport.HTTPTransportBase. By default it will use a client based on pycurl if installed or restclient.http.HTTPLib2Transport,a transport based on Httplib2 or make your own depending of the option you need to access to the serve (authentification, proxy, ....).

Methods

clone()

if you want to add a path to resource uri, you can do:

resr2 = res.clone()
__call__(path)

if you want to add a path to resource uri, you can do:

Resource("/path").get()
get(path=None, headers=None, **params)

HTTP GET

Parameters:
  • path – string additionnal path to the uri
  • headers – dict, optionnal headers that will be added to HTTP request.
  • params – Optionnal parameterss added to the request.
head(path=None, headers=None, **params)

HTTP HEAD

see GET for params description.

delete(path=None, headers=None, **params)

HTTP DELETE

see GET for params description.

post(path=None, payload=None, headers=None, **params)

HTTP POST

Parameters:
  • payload – string passed to the body of the request
  • path – string additionnal path to the uri
  • headers – dict, optionnal headers that will be added to HTTP request.
  • params – Optionnal parameterss added to the request
put(path=None, payload=None, headers=None, **params)

HTTP PUT

see POST for params description.

request(method, path=None, payload=None, headers=None, **params)

HTTP request

This method may be the only one you want to override when subclassing restclient.rest.Resource.

Parameters:
  • payload – string or File object passed to the body of the request
  • path – string additionnal path to the uri
  • headers – dict, optionnal headers that will be added to HTTP request.
  • params – Optionnal parameterss added to the request
update_uri(path)
to set a new uri absolute path

RestClient class

RestClient represent a simple HTTP client. New in 1.1 By default all urls are utf8 encoded. But you could change default charset, safe characters and decide that keys don’t need to be encoded.

class restclient.rest.RestClient(transport=None, headers=None)

Basic rest client

>>> res = RestClient()
>>> xml = res.get('http://pypaste.com/about')
>>> json = res.get('http://pypaste.com/3XDqQ8G83LlzVWgCeWdwru', headers={'accept': 'application/json'})
>>> json
u'{"snippet": "testing API.", "title": "", "id": "3XDqQ8G83LlzVWgCeWdwru", "language": "text", "revision": "363934613139"}'

Properties

transport
Any http instance of object based on restclient.http.HTTPTransportBase. By default it will use a client based on pycurl if installed or restclient.http.HTTPLib2Transport,a transport based on Httplib2 or make your own depending of the option you need to access to the serve (authentification, proxy, ....).
charset
By default it’s ‘utf-8’, it define charset of urls.
encode_keys
By default True. Encode params keys
safe
By default is “:/”, define which charaters are safe in url and don’t need to be encoded.

Methods

get(uri, path=None, headers=None, **params)

HTTP GET

Parameters:
  • uri – str, uri on which you make the request
  • path – string additionnal path to the uri
  • headers – dict, optionnal headers that will be added to HTTP request.
  • params – Optionnal parameterss added to the request.
head(uri, path=None, headers=None, **params)

HTTP HEAD

see GET for params description.

delete(uri, path=None, headers=None, **params)

HTTP DELETE

see GET for params description.

post(uri, path=None, body=None, headers=None, **params)

HTTP POST

Parameters:
  • uri – str, uri on which you make the request
  • body – string or File object passed to the body of the request
  • path – string additionnal path to the uri
  • headers – dict, optionnal headers that will be added to HTTP request.
  • params – Optionnal parameterss added to the request
put(uri, path=None, body=None, headers=None, **params)

HTTP PUT

see POST for params description.

request(method, uri, path=None, body=None, headers=None, **params)

Perform HTTP call support GET, HEAD, POST, PUT and DELETE.

Usage example, get friendpaste page :

from restclient import RestClient
client = RestClient()
page = resource.request('GET', 'http://friendpaste.com')

Or get a paste in JSON :

from restclient import RestClient
client = RestClient()
client.request('GET', 'http://friendpaste.com/5rOqE9XTz7lccLgZoQS4IP'),
    headers={'Accept': 'application/json'})
Parameters:
  • method – str, the HTTP action to be performed: ‘GET’, ‘HEAD’, ‘POST’, ‘PUT’, or ‘DELETE’
  • path – str or list, path to add to the uri
  • data – tring or File object.
  • headers – dict, optionnal headers that will be added to HTTP request.
  • params – Optionnal parameterss added to the request.
Returns:

str.