示例#1
0
    def MakeRequestAndDecodeJSON(self, url, method, **kwargs):
        """Make a HTTP request and decode the results as JSON.

    Args:
      url (str): URL to make a request to.
      method (str): HTTP method to used to make the request. GET and POST are
          supported.
      kwargs: parameters to the requests .get() or post() methods, depending
          on the value of the method parameter.

    Returns:
      dict[str, object]: body of the HTTP response, decoded from JSON.

    Raises:
      ConnectionError: If it is not possible to connect to the given URL, or it
          the request returns a HTTP error.
      ValueError: If an invalid HTTP method is specified.
    """
        method_upper = method.upper()
        if method_upper not in ('GET', 'POST'):
            raise ValueError('Method {0:s} is not supported')

        if url.lower().startswith('https'):
            self._CheckPythonVersionAndDisableWarnings()

        try:
            if method_upper == 'GET':
                response = requests.get(url, **kwargs)

            elif method_upper == 'POST':
                response = requests.post(url, **kwargs)

            response.raise_for_status()

        except requests.ConnectionError as exception:
            error_string = 'Unable to connect to {0:s}: {1:s}'.format(
                url, exception)
            raise errors.ConnectionError(error_string)

        except requests.HTTPError as exception:
            error_string = '{0:s} returned a HTTP error: {1:s}'.format(
                url, exception)
            raise errors.ConnectionError(error_string)

        return response.json()
示例#2
0
  def MakeRequestAndDecodeJSON(self, url, method, **kwargs):
    """Make a HTTP request and decode the results as JSON.

    Args:
      url: The URL to make a request to.
      method: The HTTP method to use to make the request, as a string. GET and
              POST are supported.
      kwargs: Parameters to the requests .get() or post() methods, depending
              on the value of the method parameter.

    Returns:
      The body of the HTTP response, decoded from JSON.

    Raises:
      ConnectionError: If it is not possible to connect to the given URL, or it
                       the request returns a HTTP error.
      ValueError: If an invalid HTTP method is specified.
    """
    method = method.lower()
    if method not in [u'get', u'post']:
      raise ValueError(u'Method {0:s} is not supported')

    if url.lower().startswith(u'https'):
      self._CheckPythonVersionAndDisableWarnings()

    try:
      if method.lower() == u'get':
        response = requests.get(url, **kwargs)
      if method.lower() == u'post':
        response = requests.post(url, **kwargs)
      response.raise_for_status()
    except requests.ConnectionError as exception:
      error_string = u'Unable to connect to {0:s}: {1:s}'.format(
          url, exception)
      raise errors.ConnectionError(error_string)
    except requests.HTTPError as exception:
      error_string = u'{0:s} returned a HTTP error: {1:s}'.format(
          url, exception)
      raise errors.ConnectionError(error_string)

    return response.json()