Class: Edi::Amazon::Retriever

Inherits:
BaseService show all
Includes:
RequestIdentifiable
Defined in:
app/services/edi/amazon/retriever.rb

Overview

I connect to a transport, retrieve files and save them in the Edi Communication log

Direct Known Subclasses

OrderRetriever

Constant Summary

Constants included from RequestIdentifiable

RequestIdentifiable::REQUEST_ID_HEADERS

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods included from RequestIdentifiable

#partner_request_id

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#instantiate_transporter(transporter, transporter_profile = nil) ⇒ Object



55
56
57
58
59
60
61
62
# File 'app/services/edi/amazon/retriever.rb', line 55

def instantiate_transporter(transporter, transporter_profile = nil)
  case transporter
  when :http_seller_api
    Transport::HttpSellerApiConnection.new({ profile: transporter_profile })
  else
    raise "Unknown transporter: #{transporter}"
  end
end

#process(partner:, category:, transporter: :http_seller_api, transporter_profile: nil, remote_path: nil, data_type: 'json', store_to_upload: false, file_info: {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/edi/amazon/retriever.rb', line 10

def process(partner:, category:, transporter: :http_seller_api,
            transporter_profile: nil, # Transporter profile for sftp connection, see secrets.yml
            remote_path: nil, # Name of partner for creating log entry, # Category for log entry
            data_type: 'json',
            store_to_upload: false,
            # store_to_upload: false stores the payload on the log's `data`.
            # true leaves `data` nil — it does NOT create an upload here,
            # despite the name. `file_info` is accepted and unused.
            file_info: {}) # rubocop:disable Lint/UnusedMethodArgument
  Rails.logger.debug { "Edi::Amazon::Retriever#Retriever, remote_path: #{remote_path}" }
  transport = instantiate_transporter(transporter, transporter_profile)
  begin
    res = transport.send_data('', remote_path, 'GET')
  rescue HTTP::RateLimitExceededError => e
    logger.warn "Amazon SP-API rate limited for #{category} retrieval. Skipping run; next request will retry. #{e.message}"
    return []
  end
  Rails.logger.debug { "Edi::Amazon::Retriever#Retriever, res: #{res}" }
  edi_logs = []
  EdiCommunicationLog.transaction do
    json_hash = nil
    if res[:success] && (data = res[:http_result]&.body.to_s).present?
      json_hash = JSON.parse(data).with_indifferent_access
      Rails.logger.debug { "Edi::Amazon::Retriever#Retriever, json_hash: #{json_hash}" }
      data_blob = nil
      data_blob = json_hash.to_json unless store_to_upload
    end
    if json_hash.present? # just skip generating edi communication logs if no records to process
      # A read has no submissionId, so the request id is the ONLY handle
      # Amazon support can trace a retrieval by.
      edi_log = EdiCommunicationLog.new(partner:,
                                        category:,
                                        data: data_blob,
                                        data_type:,
                                        notes: "REQUEST ID: #{partner_request_id(res[:http_result])}",
                                        transmit_datetime: Time.current)
      edi_log.save!
      logger.info "saved to edi log #{edi_log.id}"
      edi_logs << edi_log
    end
  end
  Rails.logger.debug { "Edi::Amazon::Retriever#Retriever, edi_logs: #{edi_logs}" }
  edi_logs
end