Class: Google::Apis::Core::HttpClientAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/google/apis/core/http_client_adapter.rb

Overview

HTTPClient adapter for Hurley.

Instance Method Summary collapse

Instance Method Details

#call(request) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/google/apis/core/http_client_adapter.rb', line 11

def call(request)
  client = ::HTTPClient.new
  configure_client(client, request)

  begin
    ::Hurley::Response.new(request) do |res|
      http_res = client.request(request.verb.to_s.upcase, request.url.to_s, nil, request.body_io, request.header.to_hash, false) do |http_res, chunk|
        copy_response(http_res, res)
        res.receive_body(chunk)
      end
      copy_response(http_res, res)
    end
  rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
    raise ::Hurley::Timeout, $!
  rescue ::HTTPClient::BadResponseError => err
    if err.message.include?('status 407')
      raise ::Hurley::ConnectionFailed, %{407 "Proxy Authentication Required "}
    else
      raise Hurley::ClientError, $!
    end
  rescue Errno::ECONNREFUSED, EOFError
    raise ::Hurley::ConnectionFailed, $!
  rescue => err
    if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
      raise Hurley::SSLError, err
    else
      raise
    end
  end
end

#configure_client(client, request) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/google/apis/core/http_client_adapter.rb', line 51

def configure_client(client, request)
  client.transparent_gzip_decompression = true
  if request.options.proxy
    proxy = request.options.proxy
    client.proxy = sprintf('%s:%d', proxy.host, proxy.port)
    if proxy.user && proxy.password
      client.set_proxy_auth proxy.user, proxy.password
    end
  end
  if request.options.timeout
    client.connect_timeout = request.options.timeout
    client.receive_timeout = request.options.timeout
    client.send_timeout = request.options.timeout
  end
  if request.options.open_timeout
    client.connect_timeout = request.options.open_timeout
    client.send_timeout = request.options.open_timeout
  end
  ssl_config = client.ssl_config
  ssl_opts = request.ssl_options
  ssl_config.verify_mode = ssl_opts.openssl_verify_mode
  ssl_config.cert_store = ssl_opts.openssl_cert_store
  ssl_config.add_trust_ca ssl_opts.ca_file if ssl_opts.ca_file
  ssl_config.add_trust_ca ssl_opts.ca_path if ssl_opts.ca_path
  ssl_config.client_cert = ssl_opts.openssl_client_cert if ssl_opts.openssl_client_cert
  ssl_config.client_key = ssl_opts.openssl_client_key if ssl_opts.openssl_client_key
  ssl_config.verify_depth = ssl_opts.verify_depth if ssl_opts.verify_depth
end

#copy_response(http_res, res) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/google/apis/core/http_client_adapter.rb', line 42

def copy_response(http_res, res)
  unless res.status_code
    res.status_code = http_res.status.to_i
    http_res.header.all.each do |(k,v)|
      res.header[k] = v
    end
  end
end