Package apiclient :: Module http :: Class BatchHttpRequest |
[hide private]
[frames] | no frames]

# Class BatchHttpRequest

source code

    Batches multiple HttpRequest objects into a single HTTP request.

    Example:
      from apiclient.http import BatchHttpRequest

      def list_animals(request_id, response, exception):
        """Do something with the animals list response."""
        if exception is not None:
          # Do something with the exception.
          pass
        else:
          # Do something with the response.
          pass

      def list_farmers(request_id, response, exception):
        """Do something with the farmers list response."""
        if exception is not None:
          # Do something with the exception.
          pass
        else:
          # Do something with the response.
          pass

      service = build('farm', 'v2')

      batch = BatchHttpRequest()

      batch.add(service.animals().list(), list_animals)
      batch.add(service.farmers().list(), list_farmers)
      batch.execute(http=http)

Instance Methods | [hide private]
__init__(self, callback=None, batch_uri=None) Constructor for a BatchHttpRequest. | source code
_refresh_and_apply_credentials(self, request, http) Refresh the credentials and apply to the request. | source code
_id_to_header(self, id_) Convert an id to a Content-ID header value. | source code
_header_to_id(self, header) Convert a Content-ID header value to an id. | source code
_serialize_request(self, request) Convert an HttpRequest object into a string. | source code
_deserialize_response(self, payload) Convert string into httplib2 response and content. | source code
_new_id(self) Create a new id. | source code
add(self, request, callback=None, request_id=None) Add a new request. | source code
_execute(self, http, order, requests) Serialize batch request, send to server, process response. | source code
execute(self, http=None) Execute all the requests as a single batched HTTP request. | source code
Inherited from `object`: `__delattr__`, `__format__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`, `__sizeof__`, `__str__`, `__subclasshook__`

Properties | [hide private]
Inherited from `object`: `__class__`

Method Details | [hide private]

### __init__(self, callback=None, batch_uri=None) (Constructor) | source code
    Constructor for a BatchHttpRequest.

    Args:
      callback: callable, A callback to be called for each response, of the
        form callback(id, response, exception). The first parameter is the
        request id, and the second is the deserialized response object. The
        third is an apiclient.errors.HttpError exception object if an HTTP error
        occurred while processing the request, or None if no error occurred.
      batch_uri: string, URI to send batch requests to.
Decorators: `@util.positional(1)` Overrides: object.__init__

### _refresh_and_apply_credentials(self, request, http) | source code
    Refresh the credentials and apply to the request.

    Args:
      request: HttpRequest, the request.
      http: httplib2.Http, the global http object for the batch.

### _id_to_header(self, id_) | source code
    Convert an id to a Content-ID header value.

    Args:
      id_: string, identifier of individual request.

    Returns:
      A Content-ID header with the id_ encoded into it. A UUID is prepended to
      the value because Content-ID headers are supposed to be universally
      unique.

### _header_to_id(self, header) | source code
    Convert a Content-ID header value to an id.

    Presumes the Content-ID header conforms to the format that _id_to_header()
    returns.

    Args:
      header: string, Content-ID header value.

    Returns:
      The extracted id value.

    Raises:
      BatchError if the header is not in the expected format.

### _serialize_request(self, request) | source code
    Convert an HttpRequest object into a string.

    Args:
      request: HttpRequest, the request to serialize.

    Returns:
      The request as a string in application/http format.

### _deserialize_response(self, payload) | source code
    Convert string into httplib2 response and content.

    Args:
      payload: string, headers and body as a string.

    Returns:
      A pair (resp, content), such as would be returned from httplib2.request.

### _new_id(self) | source code
    Create a new id.

    Auto incrementing number that avoids conflicts with ids already used.

    Returns:
       string, a new unique id.

### add(self, request, callback=None, request_id=None) | source code
    Add a new request.

    Every callback added will be paired with a unique id, the request_id. That
    unique id will be passed back to the callback when the response comes back
    from the server. The default behavior is to have the library generate it's
    own unique id. If the caller passes in a request_id then they must ensure
    uniqueness for each request_id, and if they are not an exception is
    raised. Callers should either supply all request_ids or nevery supply a
    request id, to avoid such an error.

    Args:
      request: HttpRequest, Request to add to the batch.
      callback: callable, A callback to be called for this response, of the
        form callback(id, response, exception). The first parameter is the
        request id, and the second is the deserialized response object. The
        third is an apiclient.errors.HttpError exception object if an HTTP error
        occurred while processing the request, or None if no errors occurred.
      request_id: string, A unique id for the request. The id will be passed to
        the callback with the response.

    Returns:
      None

    Raises:
      BatchError if a media request is added to a batch.
      KeyError is the request_id is not unique.
Decorators: `@util.positional(2)`

### _execute(self, http, order, requests) | source code
    Serialize batch request, send to server, process response.

    Args:
      http: httplib2.Http, an http object to be used to make the request with.
      order: list, list of request ids in the order they were added to the
        batch.
      request: list, list of request objects to send.

    Raises:
      httplib2.HttpLib2Error if a transport error has occured.
      apiclient.errors.BatchError if the response is the wrong format.

### execute(self, http=None) | source code
    Execute all the requests as a single batched HTTP request.

    Args:
      http: httplib2.Http, an http object to be used in place of the one the
        HttpRequest request object was constructed with. If one isn't supplied
        then use a http object from the requests in this batch.

    Returns:
      None

    Raises:
      httplib2.HttpLib2Error if a transport error has occured.
      apiclient.errors.BatchError if the response is the wrong format.
Decorators: `@util.positional(1)`