Exemplo n.º 1
0
  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 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). The first parameter is the request id, and
        the second is the deserialized response object.
      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 resumable request is added to a batch.
      KeyError is the request_id is not unique.
    """
    if request_id is None:
      request_id = self._new_id()
    if request.resumable is not None:
      raise BatchError("Resumable requests cannot be used in a batch request.")
    if request_id in self._requests:
      raise KeyError("A request with this ID already exists: %s" % request_id)
    self._requests[request_id] = (request, callback)
    self._order.append(request_id)
Exemplo n.º 2
0
  def _execute(self, http, order, requests):
    """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.
    """
    message = MIMEMultipart('mixed')
    # Message should not write out it's own headers.
    setattr(message, '_write_headers', lambda self: None)

    # Add all the individual requests.
    for request_id in order:
      request = requests[request_id]

      msg = MIMENonMultipart('application', 'http')
      msg['Content-Transfer-Encoding'] = 'binary'
      msg['Content-ID'] = self._id_to_header(request_id)

      body = self._serialize_request(request)
      msg.set_payload(body)
      message.attach(msg)

    body = message.as_string()

    headers = {}
    headers['content-type'] = ('multipart/mixed; '
                               'boundary="%s"') % message.get_boundary()

    resp, content = http.request(self._batch_uri, 'POST', body=body,
                                 headers=headers)

    if resp.status >= 300:
      raise HttpError(resp, content, uri=self._batch_uri)

    # Now break out the individual responses and store each one.
    boundary, _ = content.split(None, 1)

    # Prepend with a content-type header so FeedParser can handle it.
    header = 'content-type: %s\r\n\r\n' % resp['content-type']
    for_parser = header + content

    parser = FeedParser()
    parser.feed(for_parser)
    mime_response = parser.close()

    if not mime_response.is_multipart():
      raise BatchError("Response not in multipart/mixed format.", resp=resp,
                       content=content)

    for part in mime_response.get_payload():
      request_id = self._header_to_id(part['Content-ID'])
      response, content = self._deserialize_response(part.get_payload())
      self._responses[request_id] = (response, content)
Exemplo n.º 3
0
    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.

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

    Returns:
      The extracted id value.

    Raises:
      BatchError if the header is not in the expected format.
    """
        if header[0] != '<' or header[-1] != '>':
            raise BatchError("Invalid value for Content-ID: %s" % header)
        if '+' not in header:
            raise BatchError("Invalid value for Content-ID: %s" % header)
        base, id_ = header[1:-1].rsplit('+', 1)

        return urllib.unquote(id_)
Exemplo n.º 4
0
    def execute(self, http=None):
        """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:
      apiclient.errors.HttpError if the response was not a 2xx.
      httplib2.Error if a transport error has occured.
    """
        if http is None:
            for request_id in self._order:
                request, callback = self._requests[request_id]
                if request is not None:
                    http = request.http
                    break
        if http is None:
            raise ValueError("Missing a valid http object.")

        msgRoot = MIMEMultipart('mixed')
        # msgRoot should not write out it's own headers
        setattr(msgRoot, '_write_headers', lambda self: None)

        # Add all the individual requests.
        for request_id in self._order:
            request, callback = self._requests[request_id]

            msg = MIMENonMultipart('application', 'http')
            msg['Content-Transfer-Encoding'] = 'binary'
            msg['Content-ID'] = self._id_to_header(request_id)

            body = self._serialize_request(request)
            msg.set_payload(body)
            msgRoot.attach(msg)

        body = msgRoot.as_string()

        headers = {}
        headers['content-type'] = ('multipart/mixed; '
                                   'boundary="%s"') % msgRoot.get_boundary()

        resp, content = http.request(self._batch_uri,
                                     'POST',
                                     body=body,
                                     headers=headers)

        if resp.status >= 300:
            raise HttpError(resp, content, self._batch_uri)

        # Now break up the response and process each one with the correct postproc
        # and trigger the right callbacks.
        boundary, _ = content.split(None, 1)

        # Prepend with a content-type header so FeedParser can handle it.
        header = 'Content-Type: %s\r\n\r\n' % resp['content-type']
        content = header + content

        parser = FeedParser()
        parser.feed(content)
        respRoot = parser.close()

        if not respRoot.is_multipart():
            raise BatchError("Response not in multipart/mixed format.")

        parts = respRoot.get_payload()
        for part in parts:
            request_id = self._header_to_id(part['Content-ID'])

            headers, content = self._deserialize_response(part.get_payload())

            # TODO(jcgregorio) Remove this temporary hack once the server stops
            # gzipping individual response bodies.
            if content[0] != '{':
                gzipped_content = content
                content = gzip.GzipFile(
                    fileobj=StringIO.StringIO(gzipped_content)).read()

            request, cb = self._requests[request_id]
            postproc = request.postproc
            response = postproc(resp, content)
            if cb is not None:
                cb(request_id, response)
            if self._callback is not None:
                self._callback(request_id, response)