Skip to content

Commit 783d4cc

Browse files
committed
feat(HTTP Configurations): Add support for custom timeouts and proxies
1 parent 48d25a6 commit 783d4cc

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

lib/ibm_watson/watson_service.rb

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ def initialize(vars)
7474

7575
@conn = HTTP::Client.new(
7676
headers: headers
77-
).timeout(
78-
:per_operation,
79-
read: 60,
80-
write: 60,
81-
connect: 60
8277
)
8378
end
8479

@@ -177,4 +172,52 @@ def headers(headers)
177172
@temp_headers = headers
178173
self
179174
end
175+
176+
# @!method http_config(proxy: {}, timeout: {})
177+
# Sets the http client config, currently works with timeout and proxies
178+
# @param proxy [Hash] The hash of proxy configurations
179+
# @option proxy address [String] The address of the proxy
180+
# @option proxy port [Integer] The port of the proxy
181+
# @option proxy username [String] The username of the proxy, if authentication is needed
182+
# @option proxy password [String] The password of the proxy, if authentication is needed
183+
# @option proxy headers [Hash] The headers to be used with the proxy
184+
# @param timeout [Hash] The hash for configuring timeouts. `per_operation` has priority over `global`
185+
# @option timeout per_operation [Hash] Timeouts per operation. Requires `read`, `write`, `connect`
186+
# @option timeout global [Integer] Upper bound on total request time
187+
def http_config(proxy: {}, timeout: {})
188+
raise TypeError("proxy parameter must be a Hash") unless proxy.empty? || proxy.instance_of?(Hash)
189+
raise TypeError("timeout parameter must be a Hash") unless timeout.empty? || timeout.instance_of?(Hash)
190+
add_proxy(proxy) unless proxy.empty? || !proxy.dig(:address).is_a?(String) || !proxy.dig(:port).is_a?(Integer)
191+
add_timeout(timeout) unless timeout.empty? || (!timeout.key?(:per_operation) && !timeout.key?(:global))
192+
end
193+
194+
private
195+
196+
def add_timeout(timeout)
197+
if timeout.key?(:per_operation)
198+
raise TypeError("per_operation in timeout must be a Hash") unless timeout[:per_operation].instance_of?(Hash)
199+
defaults = {
200+
write: 0,
201+
connect: 0,
202+
read: 0
203+
}
204+
time = defaults.merge(timeout[:per_operation])
205+
@conn = @conn.timeout(:per_operation, write: time[:write], connect: time[:connect], read: time[:read])
206+
else
207+
raise TypeError("global in timeout must be an Integer") unless timeout[:global].is_a?(Integer)
208+
@conn = @conn.timeout(:global, write: timeout[:global], connect: 0, read: 0)
209+
end
210+
end
211+
212+
def add_proxy(proxy)
213+
if (proxy[:username].nil? || proxy[:password].nil?) && proxy[:headers].nil?
214+
@conn = @conn.via(proxy[:address], proxy[:port])
215+
elsif !proxy[:username].nil? && !proxy[:password].nil? && proxy[:headers].nil?
216+
@conn = @conn.via(proxy[:address], proxy[:port], proxy[:username], proxy[:password])
217+
elsif !proxy[:headers].nil? && (proxy[:username].nil? || proxy[:password].nil?)
218+
@conn = @conn.via(proxy[:address], proxy[:port], proxy[:headers])
219+
else
220+
@conn = @conn.via(proxy[:address], proxy[:port], proxy[:username], proxy[:password], proxy[:headers])
221+
end
222+
end
180223
end

0 commit comments

Comments
 (0)