def request(method, url, env, params=None, headers=None):
    if not env:
        raise Exception('No environment configured.')
    if headers is None:
        headers = {}

    url = env.api_url(url)
    if method.lower() in ('get', 'head', 'delete'):
        url = '%s?%s' % (url, compat.urlencode(params))
        payload = None
    else:
        payload = compat.urlencode(params)
        headers['Content-type'] = 'application/x-www-form-urlencoded'

    headers.update({
        'User-Agent': 'ChargeBee-Python-Client v%s' % VERSION,
        'Accept': 'application/json',
        'Authorization': _basic_auth_str(env.api_key),
        'Lang-Version': str(compat.py_major_v) + "." + str(compat.py_minor_v),
        'OS-Version': platform.platform()
    })

    meta = compat.urlparse(url)
    request_args = {
        'method': method.upper(),
        'data': payload,
        'headers': headers,
    }

    uri = meta.netloc + meta.path + '?' + meta.query

    if ChargeBee.verify_ca_certs:
        request_args.update({
            'verify': ChargeBee.ca_cert_path,
            'url': 'https://' + uri,
        })
    else:
        if Environment.protocol == "https":
            request_args.update({
                'url': 'https://' + uri,
            })
        else:
            request_args.update({
                'url': 'http://' + uri,
            })

    _logger.debug('{method} Request: {url}'.format(**request_args))
    _logger.debug('HEADERS: {0}'.format({k: v for k, v in headers.items() if k.lower() != "authorization"}))
    if payload:
        _logger.debug('PAYLOAD: {data}'.format(**request_args))

    response = requests.request(**request_args)

    _logger.debug('{method} Response: {status_code} - {text}'.format(
        method=request_args['method'], status_code=response.status_code, text=response.text
    ))

    return process_response(url, response.text, response.status_code)
def request(method, url, env, params=None, headers=None):
    if not env:
        raise Exception('No environment configured.')
    if headers is None:
        headers = {}

    url = env.api_url(url)
    if method.lower() in ('get', 'head', 'delete'):
        url = '%s?%s' % (url, compat.urlencode(params))
        payload = None
    else:
        payload = compat.urlencode(params)
        headers['Content-type'] = 'application/x-www-form-urlencoded'

    if isinstance(env, MockEnvironment):
        # we're running in a testing environment. It will provide responses
        data, resp_status = env.request(method.upper(), url, payload)
        return process_response(url, data, resp_status)

    headers.update({
        'User-Agent': 'ChargeBee-Python-Client v%s' % VERSION,
        'Accept': 'application/json',
        'Authorization': _basic_auth_str(env.api_key),
        'Lang-Version': str(compat.py_major_v) + "." + str(compat.py_minor_v),
        'OS-Version': platform.platform()
    })

    meta = compat.urlparse(url)
    if ChargeBee.verify_ca_certs:
        connection = compat.VerifiedHTTPSConnection(meta.netloc)
        connection.set_cert(ca_certs=ChargeBee.ca_cert_path)
    else:
        if Environment.protocol == "https":
            connection = compat.HTTPSConnection(meta.netloc)
        else:
            connection = compat.HTTPConnection(meta.netloc)

    connection.request(method.upper(), meta.path + '?' + meta.query, payload, headers)
    try:
        response = connection.getresponse()
        data = response.read()
        if compat.py_major_v >= 3:
            data = data.decode('utf-8')

        return process_response(url,data, response.status)
    finally:
        connection.close()
Beispiel #3
0
def request(method, url, env, params=None):
    if not env:
        raise APIError('No environment configured.')

    headers = {}

    url = env.api_url(url)
    params = utf8_encode_dict(params)
    if method.lower() in ('get', 'head', 'delete'):
        url = '%s?%s' % (url, compat.urlencode(params))
        payload = None
    else:
        payload = compat.urlencode(params)
        headers['Content-type'] = 'application/x-www-form-urlencoded'

    headers.update({
        'User-Agent': 'ChargeBee-Python-Client v%s' % VERSION,
        'Accept': 'application/json',
        'Authorization': _basic_auth_str(env.api_key),
    })

    meta = compat.urlparse(url)
    if ChargeBee.verify_ca_certs:
        connection = compat.VerifiedHTTPSConnection(meta.netloc)
        connection.set_cert(ca_certs=ChargeBee.ca_cert_path)
    else:
        if Environment.chargebee_domain is None:
            connection = compat.HTTPSConnection(meta.netloc)
        else:
            connection = compat.HTTPConnection(meta.netloc)    
        
    connection.request(method.upper(), meta.path + '?' + meta.query, payload, headers)

    try:
        response = connection.getresponse()
        data = response.read()
        if compat.is_py3:
            data = data.decode('utf-8')

        return process_response(data, response.status)
    except compat.HTTPException:
        raise APIError('Error while connecting to chargebee. If you see this repeatedly, contact us at [email protected]')
    finally:
        connection.close()
def request(method, url, env, params=None, headers=None):
    if not env:
        raise Exception('No environment configured.')
    if headers is None:
        headers = {}

    url = env.api_url(url)
    if method.lower() in ('get', 'head', 'delete'):
        url = '%s?%s' % (url, compat.urlencode(params))
        payload = None
    else:
        payload = compat.urlencode(params)
        headers['Content-type'] = 'application/x-www-form-urlencoded'

    headers.update({
        'User-Agent': 'ChargeBee-Python-Client v%s' % VERSION,
        'Accept': 'application/json',
        'Authorization': _basic_auth_str(env.api_key),
    })

    meta = compat.urlparse(url)
    if ChargeBee.verify_ca_certs:
        connection = compat.VerifiedHTTPSConnection(meta.netloc)
        connection.set_cert(ca_certs=ChargeBee.ca_cert_path)
    else:
        connect_impl = compat.HTTPSConnection if Environment.protocol == "https" else compat.HTTPConnection
        if env.proxy:
            connection = connect_impl(env.proxy, env.proxy_port)
            connection.set_tunnel(meta.netloc, meta.port)
        else:
            connection = connect_impl(meta.netloc)

    connection.request(method.upper(), meta.path + '?' + meta.query, payload,
                       headers)
    try:
        response = connection.getresponse()
        data = response.read()
        if compat.py_major_v >= 3:
            data = data.decode('utf-8')

        return process_response(url, data, response.status)
    finally:
        connection.close()
def request(method, url, env, params=None):
    if not env:
        raise APIError('No environment configured.')

    headers = {}

    url = env.api_url(url)
    if method.lower() in ('get', 'head', 'delete'):
        url = '%s?%s' % (url, compat.urlencode(params))
        payload = None
    else:
        payload = compat.urlencode(params)
        headers['Content-type'] = 'application/x-www-form-urlencoded'

    headers.update({
        'User-Agent': 'ChargeBee-Python-Client/%s' % ChargeBee.VERSION,
        'Accept': 'application/json',
        'Authorization': _basic_auth_str(env.api_key),
    })

    meta = compat.urlparse(url)
    if ChargeBee.verify_ca_certs:
        connection = compat.VerifiedHTTPSConnection(meta.netloc)
        connection.set_cert(ca_certs=ChargeBee.ca_cert_path)
    else:
        connection = compat.HTTPSConnection(meta.netloc)
        
    connection.request(method.upper(), meta.path + '?' + meta.query, payload, headers)

    try:
        response = connection.getresponse()
        data = response.read()
        if compat.is_py3:
            data = data.decode('utf-8')

        return process_response(data, response.status)
    except compat.HTTPException:
        raise APIError('Error while connecting to chargebee. If you see this repeatedly, contact us at [email protected]')
    finally:
        connection.close()