示例#1
0
def _InternalRequest(connection_policy, request_options, request_body):
    """Makes one http request.

    :Parameters:
        - `connection_policy`: documents.ConnectionPolicy
        - `request_options`: dict
        - `request_body`: str, unicode or Non

    :Returns:
        tuple of (result, headers), where both result and headers
        are dicts.

    """
    is_media = request_options['path'].find('media') > -1
    connection_timeout = (connection_policy.MediaRequestTimeout
                          if is_media
                          else connection_policy.RequestTimeout)

    if connection_policy.ProxyConfiguration and connection_policy.ProxyConfiguration.Host:
        connection = https_connection.HTTPSConnection(connection_policy.ProxyConfiguration.Host,
                                                      port=int(connection_policy.ProxyConfiguration.Port),
                                                      ssl_configuration=connection_policy.SSLConfiguration,
                                                      timeout=connection_timeout / 1000.0)
        connection.set_tunnel(request_options['host'], request_options['port'], None)
    else:
        connection = https_connection.HTTPSConnection(request_options['host'],
                                                      port=request_options['port'],
                                                      ssl_configuration=connection_policy.SSLConfiguration,
                                                      timeout=connection_timeout / 1000.0)

    connection.request(request_options['method'],
                       request_options['path'],
                       request_body,
                       request_options['headers'])
    response = connection.getresponse()
    headers = response.getheaders()

    # In case of media response, return the response to the user and the user
    # will need to handle reading the response.
    if (is_media and
        connection_policy.MediaReadMode == documents.MediaReadMode.Streamed):
        return  (response, dict(headers))

    data = response.read()
    if response.status >= 400:
        raise errors.HTTPFailure(response.status, data, headers)

    result = None
    if is_media:
        result = data
    else:
        if len(data) > 0:
            try:
                result = json.loads(data)
            except:
                raise errors.JSONParseFailure(data)

    return (result, dict(headers))
示例#2
0
def _Request(connection_policy, requests_session, resource_url,
             request_options, request_body):
    """Makes one http request using the requests module.

    :param documents.ConnectionPolicy connection_policy:
    :param requests.Session requests_session:
        Session object in requests module
    :param str resource_url:
        The url for the resource
    :param dict request_options:
    :param str request_body:
        Unicode or None

    :return:
        tuple of (result, headers)
    :rtype:
        tuple of (dict, dict)

    """
    is_media = request_options['path'].find('media') > -1
    is_media_stream = is_media and connection_policy.MediaReadMode == documents.MediaReadMode.Streamed

    connection_timeout = (connection_policy.MediaRequestTimeout
                          if is_media else connection_policy.RequestTimeout)

    parse_result = urlparse(resource_url)

    # The requests library now expects header values to be strings only starting 2.11,
    # and will raise an error on validation if they are not, so casting all header values to strings.
    request_options['headers'] = {
        header: str(value)
        for header, value in request_options['headers'].items()
    }

    # We are disabling the SSL verification for local emulator(localhost/127.0.0.1) or if the user
    # has explicitly specified to disable SSL verification.
    is_ssl_enabled = (parse_result.hostname != 'localhost'
                      and parse_result.hostname != '127.0.0.1'
                      and not connection_policy.DisableSSLVerification)

    if connection_policy.SSLConfiguration:
        ca_certs = connection_policy.SSLConfiguration.SSLCaCerts
        cert_files = (connection_policy.SSLConfiguration.SSLCertFile,
                      connection_policy.SSLConfiguration.SSLKeyFile)

        response = requests_session.request(request_options['method'],
                                            resource_url,
                                            data=request_body,
                                            headers=request_options['headers'],
                                            timeout=connection_timeout /
                                            1000.0,
                                            stream=is_media_stream,
                                            verify=ca_certs,
                                            cert=cert_files)
    else:
        response = requests_session.request(
            request_options['method'],
            resource_url,
            data=request_body,
            headers=request_options['headers'],
            timeout=connection_timeout / 1000.0,
            stream=is_media_stream,
            # If SSL is disabled, verify = false
            verify=is_ssl_enabled)

    headers = dict(response.headers)

    # In case of media stream response, return the response to the user and the user
    # will need to handle reading the response.
    if is_media_stream:
        return (response.raw, headers)

    data = response.content
    if not six.PY2:
        # python 3 compatible: convert data from byte to unicode string
        data = data.decode('utf-8')

    if response.status_code >= 400:
        raise errors.HTTPFailure(response.status_code, data, headers)

    result = None
    if is_media:
        result = data
    else:
        if len(data) > 0:
            try:
                result = json.loads(data)
            except:
                raise errors.JSONParseFailure(data)

    return (result, headers)
示例#3
0
def _Request(connection_policy, requests_session, resource_url,
             request_options, request_body):
    """Makes one http request using the requests module.

    :Parameters:
        - `connection_policy`: documents.ConnectionPolicy
        - `requests_session`: requests.Session, Session object in requests module
        - `resource_url`: str, the url for the resource
        - `request_options`: dict
        - `request_body`: str, unicode or None

    :Returns:
        tuple of (result, headers), where both result and headers
        are dicts.

    """
    is_media = request_options['path'].find('media') > -1
    is_media_stream = is_media and connection_policy.MediaReadMode == documents.MediaReadMode.Streamed

    connection_timeout = (connection_policy.MediaRequestTimeout
                          if is_media else connection_policy.RequestTimeout)

    parse_result = urlparse(resource_url)

    # We are disabling the SSL verification for local emulator(localhost) because in order to enable it we need to provide a path
    # for the emulator certificate and assign it to REQUESTS_CA_BUNDLE environment variable, so for security reasons we wanted to avoid
    # checking in that certificate on the file system.
    is_ssl_enabled = (parse_result.hostname != 'localhost')

    if connection_policy.SSLConfiguration:
        ca_certs = connection_policy.SSLConfiguration.SSLCaCerts
        cert_files = (connection_policy.SSLConfiguration.SSLCertFile,
                      connection_policy.SSLConfiguration.SSLKeyFile)

        response = requests_session.request(request_options['method'],
                                            resource_url,
                                            data=request_body,
                                            headers=request_options['headers'],
                                            timeout=connection_timeout /
                                            1000.0,
                                            stream=is_media_stream,
                                            verify=ca_certs,
                                            cert=cert_files)
    else:
        response = requests_session.request(request_options['method'],
                                            resource_url,
                                            data=request_body,
                                            headers=request_options['headers'],
                                            timeout=connection_timeout /
                                            1000.0,
                                            stream=is_media_stream,
                                            verify=is_ssl_enabled)

    headers = dict(response.headers)

    # In case of media stream response, return the response to the user and the user
    # will need to handle reading the response.
    if is_media_stream:
        return (response.raw, headers)

    data = response.content
    if not six.PY2:
        # python 3 compatible: convert data from byte to unicode string
        data = data.decode('utf-8')

    if response.status_code >= 400:
        raise errors.HTTPFailure(response.status_code, data, headers)

    result = None
    if is_media:
        result = data
    else:
        if len(data) > 0:
            try:
                result = json.loads(data)
            except:
                raise errors.JSONParseFailure(data)

    return (result, headers)