Esempio n. 1
0
def request_from_context(context):
    """
    Do http requests from context.

    :param context: request context.
    """
    new_context = copy.deepcopy(context)
    assert new_context.method in ALLOWED_METHODS

    new_context.url_path = generate_url_path(
        new_context.url_path,
        prefix=new_context.prefix_url_path,
        format_suffix=new_context.url_path_format,
        **new_context.url_path_params)

    if new_context.body_params or new_context.files:
        body, content_type = new_context.renderer.encode_params(
            new_context.body_params, files=new_context.files)
        if new_context.update_content_type and HttpSdk.CONTENT_TYPE_HEADER_NAME not in new_context.headers:
            new_context.headers[
                HttpSdk.CONTENT_TYPE_HEADER_NAME] = content_type
    else:
        body = None

    authentication_instances = new_context.authentication_instances
    for auth_obj in authentication_instances:
        new_context = auth_obj.apply_authentication(new_context)

    if HttpSdk.COOKIE_HEADER_NAME not in new_context.headers and not new_context.cookie.is_empty(
    ):
        new_context.headers[
            HttpSdk.
            COOKIE_HEADER_NAME] = new_context.cookie.as_cookie_header_value()

    url = "%s%s" % (new_context.host, new_context.url_path)
    if new_context.query_params:
        url += "?%s" % (urlencode(new_context.query_params))

    log_print_request(new_context.method, url, new_context.query_params,
                      new_context.headers, body)
    # ensure method and url are native str
    r = HttpSdk.get_pool_manager(
        new_context.proxy, ssl_verify=new_context.ssl_verify).request(
            convert_unicode_to_native_str(new_context.method),
            convert_unicode_to_native_str(url),
            body=body,
            headers=HttpSdk.convert_headers_to_native_str(new_context.headers),
            redirect=new_context.redirect,
            timeout=new_context.timeout)
    log_print_response(r.status, r.data, r.headers)
    r = new_context.response_class(r)
    return r
Esempio n. 2
0
 def convert_headers_to_native_str(headers):
     return {
         convert_unicode_to_native_str(name):
         convert_unicode_to_native_str(value)
         for name, value in headers.items()
     }