Class: Edi::Amazon::Sender
- Inherits:
-
BaseEdiService
- Object
- BaseService
- BaseEdiService
- Edi::Amazon::Sender
- Defined in:
- app/services/edi/amazon/sender.rb
Overview
Service object: sender.
Constant Summary
Constants included from RequestIdentifiable
RequestIdentifiable::REQUEST_ID_HEADERS
Constants included from Edi::AddressAbbreviator
Edi::AddressAbbreviator::MAX_LENGTH
Instance Attribute Summary
Attributes inherited from BaseEdiService
Attributes inherited from BaseService
Instance Method Summary collapse
- #instantiate_transporter(transporter, transporter_profile = nil) ⇒ Object
-
#process(partner:, category:, transporter: :http_seller_api, transporter_profile: nil, data_type: 'xml', remote_path: nil, http_method: 'POST', edi_communication_logs: nil) ⇒ Object
What type of transporter?.
-
#response_notes(res, remote_path:, http_method:, body:) ⇒ String
The audit trail we keep on the ECL for one SP-API call.
Methods inherited from BaseEdiService
#amazon_feed_product_type, #duplicate_po_already_notified?, #initialize, #mark_duplicate_po_as_notified, #onboard_ordered_catalog_items, #report_order_creation_issues, #safe_process_edi_communication_log
Methods included from RequestIdentifiable
Methods included from Edi::AddressAbbreviator
#abbreviate_street, #collect_street_originals, #record_address_abbreviation_notes
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger
Constructor Details
This class inherits a constructor from Edi::BaseEdiService
Instance Method Details
#instantiate_transporter(transporter, transporter_profile = nil) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'app/services/edi/amazon/sender.rb', line 66 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, data_type: 'xml', remote_path: nil, http_method: 'POST', edi_communication_logs: nil) ⇒ Object
What type of transporter?
7 8 9 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 |
# File 'app/services/edi/amazon/sender.rb', line 7 def process(partner:, category:, transporter: :http_seller_api, transporter_profile: nil, data_type: 'xml', # rubocop:disable Lint/UnusedMethodArgument remote_path: nil, http_method: 'POST', # we are patching this to allow a PATCH with mode=VALIDATION_PREVIEW edi_communication_logs: nil) # Which category to send, e.g order_confirm transport = instantiate_transporter(transporter, transporter_profile) edi_communication_logs ||= EdiCommunicationLog.requiring_processing.where(partner:).where(category:).order(:created_at) [edi_communication_logs].flatten.each do |ecl| logger.info "Sending #{category} data #{ecl.data} to using transporter: #{transporter} using profile #{transporter_profile}" res = transport.send_data(ecl.data, remote_path, http_method) ecl.transmit_datetime = Time.current data = res[:http_result]&.body.to_s ecl.notes = response_notes(res, remote_path:, http_method:, body: data) # need to deal with a 204 and empty body as a valid response from the shipmentConfirm API endpoint see: https://developer-docs.amazon.com/sp-api/reference/confirmshipment json_hash = nil json_hash = JSON.parse(data)&.with_indifferent_access if data.present? transaction_id = json_hash&.dig('submissionId') || "#{Time.current.to_datetime.to_fs(:crm_default)}_#{res[:attempt_number_reached]}_#{ecl.id}" ecl.transaction_id = transaction_id status = json_hash&.dig('status')&.to_s&.upcase || 'ACCEPTED' logger.info "Result: HTTP CODE: #{res[:http_result]&.status}, HTTP BODY: #{res[:http_result]&.body}" if res[:success] && %w[ACCEPTED VALID].include?(status) ecl.complete! else ecl.error end rescue HTTP::RateLimitExceededError => e # Amazon SP-API returned 429. Rather than blocking the Sidekiq thread with sleep, # we schedule this ECL to be picked up on the next flow run via transmit_after. # The requiring_processing scope already gates on transmit_after <= Time.current. retry_after_seconds = e.retry_after.to_i.positive? ? e.retry_after.to_i : 1.hour.to_i transmit_after_time = Time.current + retry_after_seconds ecl.update_columns(transmit_after: transmit_after_time) logger.warn "Amazon SP-API rate limited for ECL #{ecl.id} (#{ecl.category}/#{ecl.partner}). " \ "Scheduled retry via transmit_after: #{transmit_after_time.iso8601} " \ "(#{retry_after_seconds}s from now). #{e.}" end edi_communication_logs end |
#response_notes(res, remote_path:, http_method:, body:) ⇒ String
The audit trail we keep on the ECL for one SP-API call. REQUEST ID is
Amazon's x-amzn-RequestId — their developer support requires it to trace
a call, and a read carries no submissionId to quote instead.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/services/edi/amazon/sender.rb', line 53 def response_notes(res, remote_path:, http_method:, body:) result = res[:http_result] <<~NOTES.strip Timestamp: #{Time.current.to_datetime.to_fs(:crm_default)} REMOTE_PATH: #{remote_path} HTTP CODE: #{result&.status} HTTP METHOD: #{http_method} ATTEMPT NUMBER: #{res[:attempt_number_reached]} REQUEST ID: #{partner_request_id(result)} HTTP BODY: #{body} NOTES end |