class documentation

class BatchHttpRequest(object):

Constructor: BatchHttpRequest(callback, batch_uri)

View In Hierarchy

Batches multiple HttpRequest objects into a single HTTP request.

Example

from googleapiclient.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)

Method __init__ Constructor for a BatchHttpRequest.
Method add Add a new request.
Method execute Execute all the requests as a single batched HTTP request.
Method _deserialize_response Convert string into httplib2 response and content.
Method _execute Serialize batch request, send to server, process response.
Method _header_to_id Convert a Content-ID header value to an id.
Method _id_to_header Convert an id to a Content-ID header value.
Method _new_id Create a new id.
Method _refresh_and_apply_credentials Refresh the credentials and apply to the request.
Method _serialize_request Convert an HttpRequest object into a string.
Instance Variable _base_id Undocumented
Instance Variable _batch_uri Undocumented
Instance Variable _callback Undocumented
Instance Variable _callbacks Undocumented
Instance Variable _last_auto_id Undocumented
Instance Variable _order Undocumented
Instance Variable _refreshed_credentials Undocumented
Instance Variable _requests Undocumented
Instance Variable _responses Undocumented
@util.positional(1)
def __init__(self, callback=None, batch_uri=None):

Constructor for a BatchHttpRequest.

Parameters
callbackcallable, 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 googleapiclient.errors.HttpError exception object if an HTTP error occurred while processing the request, or None if no error occurred.
batch_uristring, URI to send batch requests to.
@util.positional(2)
def add(self, request, callback=None, request_id=None):

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 never supply a request id, to avoid such an error.

Parameters
requestHttpRequest, Request to add to the batch.
callbackcallable, 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 googleapiclient.errors.HttpError exception object if an HTTP error occurred while processing the request, or None if no errors occurred.
request_idstring, 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.
@util.positional(1)
def execute(self, http=None):

Execute all the requests as a single batched HTTP request.

Parameters
httphttplib2.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 occurred.
googleapiclient.errors.BatchError if the response is the wrong format.
def _deserialize_response(self, payload):

Convert string into httplib2 response and content.

Parameters
payloadstring, headers and body as a string.
Returns
A pair (resp, content), such as would be returned from httplib2.request.
def _execute(self, http, order, requests):

Serialize batch request, send to server, process response.

Parameters
httphttplib2.Http, an http object to be used to make the request with.
orderlist, list of request ids in the order they were added to the batch.
requestslist, list of request objects to send.
Raises
httplib2.HttpLib2Error if a transport error has occurred.
googleapiclient.errors.BatchError if the response is the wrong format.
def _header_to_id(self, header):

Convert a Content-ID header value to an id.

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

Parameters
headerstring, Content-ID header value.
Returns
The extracted id value.
Raises
BatchError if the header is not in the expected format.
def _id_to_header(self, id_):

Convert an id to a Content-ID header value.

Parameters
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.
def _new_id(self):

Create a new id.

Auto incrementing number that avoids conflicts with ids already used.

Returns
string, a new unique id.
def _refresh_and_apply_credentials(self, request, http):

Refresh the credentials and apply to the request.

Parameters
requestHttpRequest, the request.
httphttplib2.Http, the global http object for the batch.
def _serialize_request(self, request):

Convert an HttpRequest object into a string.

Parameters
requestHttpRequest, the request to serialize.
Returns
The request as a string in application/http format.
_base_id =

Undocumented

_batch_uri =

Undocumented

_callback =

Undocumented

_callbacks: dict =

Undocumented

_last_auto_id: int =

Undocumented

_order: list =

Undocumented

_refreshed_credentials: dict =

Undocumented

_requests: dict =

Undocumented

_responses: dict =

Undocumented