Class: Transport::BasicHttpApiConnection

Inherits:
Object
  • Object
show all
Defined in:
app/services/transport/basic_http_api_connection.rb

Overview

Basic HTTP API connection with simple auth for EDI integrations

Constant Summary collapse

VALID_HTTP_METHODS =
%w[get put post patch delete head]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BasicHttpApiConnection

Returns a new instance of BasicHttpApiConnection.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/services/transport/basic_http_api_connection.rb', line 9

def initialize(options = {})
  @options = options
  # if there's a profile, grab the username and profile to encode the Basic HTTP headers to send from it
  @success_call = 'Ack'
  if (profile = @options[:profile])
    username = Heatwave::Configuration.fetch(profile&.to_sym, :username)
    password = Heatwave::Configuration.fetch(profile&.to_sym, :password)
    @headers = { Authorization: ActionController::HttpAuthentication::Basic.encode_credentials(username, password) }
  end
  @headers ||= {}
  @logger = options[:logger] || Rails.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'app/services/transport/basic_http_api_connection.rb', line 7

def logger
  @logger
end

#profileObject (readonly)

Returns the value of attribute profile.



7
8
9
# File 'app/services/transport/basic_http_api_connection.rb', line 7

def profile
  @profile
end

Instance Method Details

#batch_download_data(directory, pattern = nil) ⇒ Object

In one shot returns a hash, hash key is filename, hash value is content of file



50
51
52
53
54
55
56
57
58
59
# File 'app/services/transport/basic_http_api_connection.rb', line 50

def batch_download_data(directory, pattern = nil)
  data = {}
  logger.info "Attempting connection to http endpoint: #{directory}"
  http_obj = HTTP
  @headers.each do |k, v|
    http_obj = http_obj.headers(k.to_s.upcase.dasherize => v)
  end
  data[pattern || 'response'] = http_obj.get(directory).to_s
  data
end

#rm(remote_file_path) ⇒ Object



61
62
63
# File 'app/services/transport/basic_http_api_connection.rb', line 61

def rm(remote_file_path)
  # Do nothing
end

#send_data(data, remote_filepath, method = 'post') ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/transport/basic_http_api_connection.rb', line 22

def send_data(data, remote_filepath, method = 'post')
  method = 'put' unless VALID_HTTP_METHODS.include?(method)
  http_obj = HTTP
  @headers.each do |k, v|
    http_obj = http_obj.headers(k.to_s.upcase.dasherize => v) # here the header key goes from say "x_houzz_api_ssl_token" to "X-HOUZZ-API-SSL-TOKEN"
  end
  logger.debug('BasicHttpApiConnection send_data starting', header_count: @headers.size)
  http_res = nil
  attempt_number_reached = 0
  Retryable.retryable(tries: 3, sleep: lambda { |n| 4**n }, on: Retryable::TIMEOUT_CLASSES) do |attempt_number, _exception|
    http_res = http_obj.timeout(180).send(method, remote_filepath, json: JSON.parse(data))
    attempt_number_reached = attempt_number + 1
  end
  logger.debug('BasicHttpApiConnection request complete', url: remote_filepath, method: method, attempts: attempt_number_reached, status: http_res&.code)
  { success: successful?(http_res), http_result: http_res, attempt_number_reached: attempt_number_reached }
end

#successful?(http_res) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
# File 'app/services/transport/basic_http_api_connection.rb', line 39

def successful?(http_res)
  res = false
  if http_res.code >= 200 and http_res.code < 300
    res = true
    ack = JSON.parse(http_res.body.to_s).with_indifferent_access[@success_call.to_sym]
    res = false if ack and ack != 'Success'
  end
  res
end