Resource

This module provide a common interface for all HTTP requests. This module make HTTP requests using Pycurl by default if installed or Urllib2 if not. You could also use 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"}'

Resource class

class restclient.rest.Resource(uri, httpclient=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.
httpclient
Any http instance of object based on restclient.http.HTTPClient. By default it will use a client based on pycurl if installed or urllib2. You could also use restclient.http.HTTPLib2HTTPClient,a client 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").request("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

Payload:

string passed to the body of the request

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
put(path=None, payload=None, headers=None, **params)

HTTP PUT

see POST for params description.

get_status_code()
get status code of the last request
update_uri(path)
to set a new uri absolute path

RestClient class

RestClient represent a simple HTTP client.

class restclient.rest.RestClient(httpclient=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
'{"snippet": "testing API.", "title": "", "id": "3XDqQ8G83LlzVWgCeWdwru", "language": "text", "revision": "363934613139"}'

Properties

httpclient
Any http instance of object based on restclient.http.HTTPClient. By default it will use a client based on pycurl if installed or urllib2. You could also use restclient.http.HTTPLib2HTTPClient,a client based on Httplib2 or make your own depending of the option you need to access to the serve (authentification, proxy, ....).

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
  • 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
Body:

string passed to the body of the request

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

HTTP PUT

see POST for params description.

make_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.make_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 – str or string or any object that could be converted to JSON.
  • headers – dict, optionnal headers that will be added to HTTP request.
  • params – Optionnal parameterss added to the request.
Returns:

str.

Exceptions

exception restclient.rest.ResourceNotFound
Exception raised when a 404 HTTP error is received in response to a request.
exception restclient.rest.Unauthorized
Exception raised when a 401 HTTP error is received in response to a request.
exception restclient.rest.RequestFailed
Exception raised when an unexpected HTTP error is received in response to a request.