Class: Transport::HttpApiUploadConnection

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

Overview

HTTP API connection with file upload support 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 = {}) ⇒ HttpApiUploadConnection

Returns a new instance of HttpApiUploadConnection.



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

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
  logger.debug("HttpApiUploadConnection initialized", header_count: @headers.size)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'app/services/transport/http_api_upload_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_upload_connection.rb', line 10

def profile
  @profile
end

Instance Method Details

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/transport/http_api_upload_connection.rb', line 25

def send_data(data, remote_filepath, method = 'put')
  method = 'put' unless VALID_HTTP_METHODS.include?(method)
  # remote_filepath is an Amazon presigned upload URL whose query string carries
  # the AWS signature; log only the path (no query) and header names (no values)
  # so secrets never land in app logs. The request itself still uses the full URL.
  safe_url = remote_filepath.to_s.split('?', 2).first
  @headers.each_key { |k| logger.info "Using header: #{k.to_s.upcase.dasherize} to #{safe_url}" }
  # The :retry middleware on #connection replaces the old Retryable wrapper;
  # the retry_block counts retries so we still report attempt_number_reached.
  # data is sent as the raw request body (no JSON re-encoding), matching the
  # http.rb `body:` option this used before.
  attempts = 1
  http_res = connection(retry_block: ->(**) { attempts += 1 }).run_request(method.to_sym, remote_filepath, data.presence, nil)
  logger.debug("HTTP upload complete", url: safe_url, method: method, attempts: attempts, status: http_res&.status)
  { success: successful?(http_res), http_result: http_res, attempt_number_reached: attempts }
end

#successful?(http_res) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/services/transport/http_api_upload_connection.rb', line 42

def successful?(http_res)
  http_res.status >= 200 && http_res.status < 300
end