Class: Transport::HttpApiUploadConnection
- Inherits:
-
Object
- Object
- Transport::HttpApiUploadConnection
- 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 =
%w[get put post patch delete head]
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ HttpApiUploadConnection
constructor
A new instance of HttpApiUploadConnection.
- #send_data(data, remote_filepath, method = 'put') ⇒ Object
- #successful?(http_res) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ HttpApiUploadConnection
Returns a new instance of HttpApiUploadConnection.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/services/transport/http_api_upload_connection.rb', line 9 def initialize( = {}) @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([:headers]) if [:headers].present? @logger = [:logger] || Rails.logger logger.debug("HttpApiUploadConnection initialized", header_count: @headers.size) end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'app/services/transport/http_api_upload_connection.rb', line 7 def logger @logger end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
7 8 9 |
# File 'app/services/transport/http_api_upload_connection.rb', line 7 def profile @profile end |
Instance Method Details
#send_data(data, remote_filepath, method = 'put') ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/services/transport/http_api_upload_connection.rb', line 22 def send_data(data, remote_filepath, method = 'put') method = 'put' unless VALID_HTTP_METHODS.include?(method) 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" logger.info "Using headers: #{k.to_s.upcase.dasherize}: #{v} to #{remote_filepath}" end http_res = nil payload = {} payload[:body] = data if data.present? attempt_number_reached = 0 Retryable.retryable(tries: 3, sleep: lambda { |n| 4**n }, on: Retryable::TIMEOUT_CLASSES) do |attempt_number, exception| http_res = http_obj.timeout(180).send(method, remote_filepath, **payload) attempt_number_reached = attempt_number + 1 end logger.debug("HTTP upload complete", url: remote_filepath, method: method, attempts: attempt_number_reached, status: http_res&.code) { success: successful?(http_res), http_result: http_res, attempt_number_reached:attempt_number_reached } end |
#successful?(http_res) ⇒ Boolean
41 42 43 44 45 |
# File 'app/services/transport/http_api_upload_connection.rb', line 41 def successful?(http_res) res = false res = true if http_res.code >= 200 and http_res.code < 300 res end |