class SoarHttpClient
Constants
- VERSION
Attributes
auditing[R]
connection[R]
max_sleep_seconds[R]
min_sleep_seconds[R]
persistent_connection[R]
retries[R]
url[R]
Public Class Methods
new(url:, retries: 2, auditing: nil, persistent_connection: false, timeout: 5, basic_auth: nil, min_sleep_seconds: 0.5, max_sleep_seconds: 1)
click to toggle source
# File lib/soar_http_client.rb, line 7 def initialize(url:, retries: 2, auditing: nil, persistent_connection: false, timeout: 5, basic_auth: nil, min_sleep_seconds: 0.5, max_sleep_seconds: 1) @semaphore = Mutex.new @semaphore.synchronize { @url = url @retries = retries @auditing = auditing @persistent_connection = persistent_connection @min_sleep_seconds = min_sleep_seconds @max_sleep_seconds = max_sleep_seconds @connection = Faraday.new(:url => @url) do |faraday| faraday.options[:open_timeout] = timeout faraday.options[:timeout] = timeout faraday.request :url_encoded faraday.adapter Faraday.default_adapter if persistent_connection == false faraday.adapter :net_http_persistent if persistent_connection == true faraday.basic_auth basic_auth[:user], basic_auth[:password] if basic_auth && (basic_auth[:user] && basic_auth[:password]) end } end
Public Instance Methods
delete(path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil)
click to toggle source
# File lib/soar_http_client.rb, line 45 def delete(path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil) do_request(verb: 'delete', path: path, params: params, body: body, token: token, headers: headers, flow_identifier: flow_identifier) end
get(path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil)
click to toggle source
# File lib/soar_http_client.rb, line 29 def get(path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil) do_request(verb: 'get', path: path, params: params, body: body, token: token, headers: headers, flow_identifier: flow_identifier) end
patch(path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil)
click to toggle source
# File lib/soar_http_client.rb, line 41 def patch(path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil) do_request(verb: 'patch', path: path, params: params, body: body, token: token, headers: headers, flow_identifier: flow_identifier) end
post(path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil)
click to toggle source
# File lib/soar_http_client.rb, line 33 def post(path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil) do_request(verb: 'post', path: path, params: params, body: body, token: token, headers: headers, flow_identifier: flow_identifier) end
put(path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil)
click to toggle source
# File lib/soar_http_client.rb, line 37 def put(path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil) do_request(verb: 'put', path: path, params: params, body: body, token: token, headers: headers, flow_identifier: flow_identifier) end
Private Instance Methods
back_off(attempt)
click to toggle source
# File lib/soar_http_client.rb, line 81 def back_off(attempt) sleep_seconds = [@min_sleep_seconds * (2 ** (attempt - 1)), @max_sleep_seconds].min sleep_seconds = sleep_seconds * (0.5 * (1 + rand())) sleep_seconds = [@min_sleep_seconds, sleep_seconds].max sleep sleep_seconds end
do_request(verb: ,path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil)
click to toggle source
# File lib/soar_http_client.rb, line 50 def do_request(verb: ,path:, params: nil, body: nil, token: nil, headers: nil, flow_identifier: nil) @semaphore.synchronize { begin if params.nil? params = { 'flow_identifier' => flow_identifier } if flow_identifier else params = params.merge({ 'flow_identifier' => flow_identifier } ) if flow_identifier end tries ||= 0 result = @connection.send(verb) do |req| req.url path req.params = params if params req.body = body if body req.headers = headers if headers req.headers['Authorization'] = token if token end rescue => ex tries += 1 back_off(tries) retry if(tries < retries) @auditing.audit_exception(exception: ex, flow_id: flow_identifier, message: "Max allowed retries reached, while doing HTTP #{verb.upcase} #{@url + path}") if @auditing != nil raise ex else @auditing.warn("Retried #{tries} times, while doing HTTP #{verb.upcase} #{@url + path}", flow_identifier) if tries > 0 && @auditing != nil return result end } end