def loggable_body(self, value):
        """Update request body for logging purposes

        :type value: str
        :param value: updated body

        :raises: :exc:`RequestError` if the request does not have a body.
        """
        if self.body is None:
            raise RequestError(
                'Cannot set loggable body on request with no body')
        self._loggable_body = value
Example #2
0
def _make_api_request_no_retry(http, http_request, redirections=_REDIRECTIONS):
    """Send an HTTP request via the given http instance.

    This wrapper exists to handle translation between the plain httplib2
    request/response types and the Request and Response types above.

    :type http: :class:`httplib2.Http`
    :param http: an instance which impelements the `Http` API.

    :type http_request: :class:`Request`
    :param http_request: the request to send.

    :type redirections: integer
    :param redirections: Number of redirects to follow.

    :rtype: :class:`Response`
    :returns: an object representing the server's response

    :raises: :exc:`google.cloud.streaming.exceptions.RequestError` if no
             response could be parsed.
    """
    connection_type = None
    # Handle overrides for connection types.  This is used if the caller
    # wants control over the underlying connection for managing callbacks
    # or hash digestion.
    if getattr(http, 'connections', None):
        url_scheme = parse.urlsplit(http_request.url).scheme
        if url_scheme and url_scheme in http.connections:
            connection_type = http.connections[url_scheme]

    # Custom printing only at debuglevel 4
    new_debuglevel = 4 if httplib2.debuglevel == 4 else 0
    with _httplib2_debug_level(http_request, new_debuglevel, http=http):
        info, content = http.request(str(http_request.url),
                                     method=str(http_request.http_method),
                                     body=http_request.body,
                                     headers=http_request.headers,
                                     redirections=redirections,
                                     connection_type=connection_type)

    if info is None:
        raise RequestError()

    response = Response(info, content, http_request.url)
    _check_response(response)
    return response
Example #3
0
def _check_response(response):
    """Validate a response

    :type response: :class:`Response`
    :param response: the response to validate

    :raises: :exc:`google.cloud.streaming.exceptions.RequestError` if response
             is None, :exc:`~.exceptions.BadStatusCodeError` if response status
             code indicates an error, or :exc:`~.exceptions.RetryAfterError`
             if response indicates a retry interval.
    """
    if response is None:
        # Caller shouldn't call us if the response is None, but handle anyway.
        raise RequestError('Request did not return a response.')
    elif (response.status_code >= 500
          or response.status_code == TOO_MANY_REQUESTS):
        raise BadStatusCodeError.from_response(response)
    elif response.retry_after:
        raise RetryAfterError.from_response(response)