Class: Transport::MiraklTransporter

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

Overview

HTTP transporter for Mirakl marketplace integrations

Constant Summary collapse

VALID_HTTP_METHODS =

Valid http methods.

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

Per-request timeout (seconds) for the Faraday-backed calls. #send_data
previously set this explicitly; #batch_download_data was unbounded — it now
shares the same 20s budget so a stalled poll can't hang the worker.

20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MiraklTransporter

Returns a new instance of MiraklTransporter.



16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/transport/mirakl_transporter.rb', line 16

def initialize(options = {})
  @options = options
  # if there's a profile, grab the headers to send from it
  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.



14
15
16
# File 'app/services/transport/mirakl_transporter.rb', line 14

def logger
  @logger
end

#profileObject (readonly)

Returns the value of attribute profile.



14
15
16
# File 'app/services/transport/mirakl_transporter.rb', line 14

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



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/transport/mirakl_transporter.rb', line 76

def batch_download_data(directory, pattern = nil)
  data = {}
  logger.info "Attempting connection to http endpoint: #{directory}"
  # Retry transient connect failures (e.g. Faraday::ConnectionFailed "Network
  # is unreachable" reaching marketplace.castorama.fr / adeo-marketplace.mirakl.net)
  # instead of letting them surface as exceptions. EdiOrderFlowWorker polls this
  # path every ~5 minutes, so a momentary network blip should self-heal rather
  # than page. The :retry middleware on #connection provides the backoff the
  # Retryable wrapper used to. AppSignal #1916.
  response = connection.get(directory)
  data[pattern || 'response'] = response.body.to_s
  data
end

#rm(remote_file_path) ⇒ Object



90
91
92
# File 'app/services/transport/mirakl_transporter.rb', line 90

def rm(remote_file_path)
  # Do nothing
end

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



28
29
30
31
32
33
34
35
36
37
# File 'app/services/transport/mirakl_transporter.rb', line 28

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 —
  # it backs off and retries the same transient transport failures in-band.
  http_res = connection.run_request(method.to_sym, remote_filepath, body, nil)
  logger.debug("HTTP request complete", url: remote_filepath, method: method, status: http_res&.status)
  { success: faraday_2xx?(http_res), http_result: http_res }
end

#send_file(remote_filepath, options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'app/services/transport/mirakl_transporter.rb', line 53

def send_file(remote_filepath, 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

  http_res = http_obj.post(remote_filepath, form: options)
  logger.debug("HTTP file upload complete", url: remote_filepath, method: 'post', status: http_res&.code)
  { success: successful?(http_res), http_result: http_res }
end

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



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/transport/mirakl_transporter.rb', line 39

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

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'app/services/transport/mirakl_transporter.rb', line 64

def successful?(http_res)
  res = false
  ack_res = true
  if ['text/xml', 'text/csv'].include?(http_res.mime_type)
    res = true if (http_res.code >= 200) && (http_res.code < 300)
  elsif (http_res.code >= 200) && (http_res.code < 300) && ack_res
    res = true
  end
  res
end