@@ -21,19 +21,17 @@ def get(path)
2121 private
2222
2323 def execute ( method , path , body : nil )
24- uri = URI ( config . base_url )
25- http = setup_http_connection ( uri )
26- request = method . new ( uri . path + path )
24+ request = method . new ( base_uri . path + path )
2725 request . body = body
2826
2927 request [ 'Content-Type' ] = 'application/json'
3028 request [ 'Authorization' ] = 'Bearer ' + config . api_key . to_s
3129 request [ 'User-Agent' ] = config . user_agent
3230
33- response = http . request ( request )
31+ response = connection . request ( request )
3432
3533 case response
36- when Net ::HTTPSuccess , Net ::HTTPUnauthorized then
34+ when Net ::HTTPSuccess , Net ::HTTPUnauthorized
3735 parse_response ( response . body )
3836 else
3937 raise ( Error , "Failure: #{ response . class } " )
@@ -50,21 +48,29 @@ def parse_response(response_body)
5048 end
5149 end
5250
53- def setup_http_connection ( uri )
54- http = Net ::HTTP ::Proxy (
55- config . proxy_host , config . proxy_port , config . proxy_user , config . proxy_pass
56- ) . new ( uri . host , uri . port )
51+ def base_uri
52+ @base_uri ||= URI ( config . base_url )
53+ end
5754
58- http . read_timeout = config . http_read_timeout
59- http . open_timeout = config . http_open_timeout
55+ def connection
56+ @connection ||= setup_connection
57+ end
6058
61- if uri . scheme == 'https'
62- http . use_ssl = true
63- http . verify_mode = OpenSSL ::SSL ::VERIFY_PEER
59+ def setup_connection
60+ http = if config . proxy
61+ proxy = URI ( config . proxy )
62+ proxy_use_ssl = proxy . scheme == 'https'
63+ Net ::HTTP . new ( base_uri . hostname , base_uri . port ,
64+ proxy . hostname , proxy . port , proxy . user , proxy . password , nil , proxy_use_ssl )
6465 else
65- http . use_ssl = false
66+ Net :: HTTP . new ( base_uri . hostname , base_uri . port )
6667 end
6768
69+ http . use_ssl = base_uri . scheme == 'https'
70+ http . verify_mode = OpenSSL ::SSL ::VERIFY_PEER if http . use_ssl?
71+ http . read_timeout = config . http_read_timeout
72+ http . open_timeout = config . http_open_timeout
73+
6874 http
6975 end
7076 end
0 commit comments