Resource

This module provide a common interface for all CouchDB request. This module make HTTP request using httplib2 module.

Example:

>>> resource = CouchdbResource()
>>> info = resource.get()
>>> info['couchdb']
u'Welcome'

CouchdbResource class

class simplecouchdb.resource.CouchdbResource(uri='http://127.0.0.1:5984', transport=None)
__init__(uri='http://127.0.0.1:5984', transport=None)

Constructor for a CouchdbResource object.

CouchdbResource represent an HTTP resource to CouchDB.

Parameters:
  • uri – str, full uri to the server.
  • transport – any http instance of object based on restclient.transport.HTTPTransportBase. By default it will use a client based on pycurl if installed or restclient.transport.HTTPLib2Transport, a client based on Httplib2 or make yourown depending of the option you need to access to the serve (authentification, proxy, ....).
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()
request(method, path=None, payload=None, headers=None, **params)

Perform HTTP call to the couchdb server and manage JSON conversions, support GET, POST, PUT and DELETE.

Usage example, get infos of a couchdb server on http://127.0.0.1:5984 :

import simplecouchdb.CouchdbResource
resource = simplecouchdb.CouchdbResource()
infos = resource.request('GET'))
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. Parameterss are for example the parameters for a view. See CouchDB View API reference for example.
Returns:

tuple (data, resp), where resp is an httplib2.Response object and data a python object (often a dict).

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.

Exceptions

exception restclient.rest.ResourceNotFound
Exception raised when no resource was found at the given url.
exception simplecouchdb.resource.ResourceConflict
Exception raised when there is conflict while updating
exception restclient.rest.RequestFailed

Exception raised when an unexpected HTTP error is received in response to a request.

The request failed, meaning the remote HTTP server returned a code other than success, unauthorized, or NotFound.

The exception message attempts to extract the error

You can get the status code by e.http_code, or see anything about the response via e.response. For example, the entire result body (which is probably an HTML error page) is e.response.body.

exception simplecouchdb.resource.PreconditionFailed
Exception raised when 412 HTTP error is received in response to a request

See also

Module restclient
Documentation of the restclient module, check py-restclient for more information