Class: Transport::HttpApiConnection

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

Overview

Generic HTTP API connection for EDI integrations

Constant Summary collapse

VALID_HTTP_METHODS =

Valid http methods.

%w[get put post patch delete head].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HttpApiConnection

Returns a new instance of HttpApiConnection.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/services/transport/http_api_connection.rb', line 12

def initialize(options = {})
  @options = options
  # if there's a profile, grab the headers to send from it
  @success_call = 'Ack'
  if (profile = @options[:profile])
    @headers = Heatwave::Configuration.fetch(profile&.to_sym)
    @headers = @headers.reject { |k| k == :hostname } # remove hostname which isn't used as a header
  end
  @headers ||= {}
  @headers = @headers.merge(options[:headers]) if options[:headers].present?
  @logger = options[:logger] || Rails.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'app/services/transport/http_api_connection.rb', line 10

def logger
  @logger
end

#profileObject (readonly)

Returns the value of attribute profile.



10
11
12
# File 'app/services/transport/http_api_connection.rb', line 10

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



57
58
59
60
61
62
63
64
65
66
# File 'app/services/transport/http_api_connection.rb', line 57

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



68
69
70
# File 'app/services/transport/http_api_connection.rb', line 68

def rm(remote_file_path)
  # Do nothing
end

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



25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/transport/http_api_connection.rb', line 25

def send_data(data, remote_filepath, method = 'put')
  method = method.downcase
  method = 'put' unless VALID_HTTP_METHODS.include?(method)
  body = data.present? ? JSON.parse(data) : nil
  # The :retry middleware on #connection replaces the old Retryable wrapper;
  # the retry_block counts retries so we still report attempt_number_reached.
  attempts = 1
  http_res = connection(retry_block: ->(**) { attempts += 1 }).run_request(method.to_sym, remote_filepath, body, nil)
  logger.debug('HTTP request complete', url: remote_filepath, method: method, attempts: attempts, status: http_res&.status)
  { success: faraday_successful?(http_res), http_result: http_res, attempt_number_reached: attempts }
end

#send_mirakl_data(data, remote_filepath, filename, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/services/transport/http_api_connection.rb', line 37

def send_mirakl_data(data, remote_filepath, filename, options = {})
  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
  # Build XML file to send
  io = StringIO.new(data)
  file = HTTP::FormData::File.new(io, filename:)
  options[:file] = file
  http_res = http_obj.post(remote_filepath, form: options)
  logger.debug('HTTP Mirakl request complete', url: remote_filepath, method: 'post', status: http_res&.code)
  { success: successful?(http_res), http_result: http_res }
end

#successful?(http_res) ⇒ Boolean

Success check for the http.rb-backed #send_mirakl_data.

Returns:

  • (Boolean)


52
53
54
# File 'app/services/transport/http_api_connection.rb', line 52

def successful?(http_res)
  evaluate_success(status: http_res.code, content_type: http_res.mime_type, body: http_res.body.to_s)
end