client

Restful json clients.

Use Resource for a single host. Use Resources for multiple hosts with simple partitioning or redundancy. Use Shards for horizontally partitioning hosts by different keys.

Resources optionally reuse connections, handling request timeouts. Broadcasting to multiple resources is parallelized with asynchronous requests and responses.

The load balancing strategy is randomized, biased by the number of cached connections available. This inherently provides limited failover support, but applications must still handle exceptions as desired.

Response

class client.Response(sock, debuglevel=0, strict=0, method=None)

Bases: httplib.HTTPResponse

A completed response which handles json and caches its body.

status
HTTP status code
reason
HTTP status message
body
string of entire response body
__nonzero__()
Return whether status is successful.
__call__()
Return evaluated response body or raise exception.

Resource

class client.Resource(host, port=None, strict=None, timeout=<object object at 0x1001da410>)

Bases: httplib.HTTPConnection

Synchronous connection which handles json responses.

__call__(method, path, body=None)
Send request and return evaluated response body.
getresponse()
Get the response from the server.
delete(path, **params)
get(path, **params)
post(path, **body)
put(path, **body)
request(method, path, body=None)
Send request after handling body and headers.
response_class
alias of Response

Resources

class client.Resources(hosts, limit=0)

Thread-safe mapping of hosts to optionally persistent resources.

Parameters:
  • hosts – host[:port] strings
  • limit – maximum number of cached connections per host
class Manager
Queue of prioritized resources.
Resources.broadcast(method, path, body=None, hosts=())
Send requests and return responses from all hosts, optionally from given subset.
Resources.choice(hosts)
Return chosen host according to priority.
Resources.getresponse(host, resource)
Return response and release resource.
Resources.priority(host)
Return priority for host. None may be used to eliminate from consideration.
Resources.request(host, method, path, body=None)
Send request to given host and return exclusive resource.
Resources.unicast(method, path, body=None, hosts=())
Send request and return response from any host, optionally from given subset.

Shards

class client.Shards(items=(), limit=0, **multimap)

Mapping of keys to host clusters, with associated resources.

Parameters:
  • items – host, key pairs
  • limit – maximum number of cached connections per host
  • multimap – mapping of hosts to multiple keys
resources
Resources mapping.
broadcast(key, method, path, body=None)
Send requests and return responses from all hosts for corresponding key.
choice(hosts)
Return chosen host according to priority.
multicast(keys, method, path, body=None)
Send requests and return responses from a minimal subset of hosts which cover all corresponding keys. Response overlap is possible depending on partitioning.
priority(hosts)
Return combined priority for hosts.
unicast(key, method, path, body=None)
Send request and return response from any host for corresponding key.

Table Of Contents

Previous topic

server

Next topic

examples

This Page