Class: Transport::BasicHttpApiConnection
- Inherits:
-
Object
- Object
- Transport::BasicHttpApiConnection
- Defined in:
- app/services/transport/basic_http_api_connection.rb
Overview
Basic HTTP API connection with simple auth for EDI integrations
Constant Summary collapse
- VALID_HTTP_METHODS =
Valid http methods.
%w[get put post patch delete head].freeze
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
-
#batch_download_data(directory, pattern = nil) ⇒ Object
In one shot returns a hash, hash key is filename, hash value is content of file.
-
#initialize(options = {}) ⇒ BasicHttpApiConnection
constructor
A new instance of BasicHttpApiConnection.
- #rm(remote_file_path) ⇒ Object
- #send_data(data, remote_filepath, method = 'post') ⇒ Object
-
#successful?(http_res) ⇒ Boolean
send_data is the only caller, and it is Faraday-backed (#status).
Constructor Details
#initialize(options = {}) ⇒ BasicHttpApiConnection
Returns a new instance of BasicHttpApiConnection.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/services/transport/basic_http_api_connection.rb', line 12 def initialize( = {}) @options = # if there's a profile, grab the username and profile to encode the Basic HTTP headers to send from it @success_call = 'Ack' if (profile = @options[:profile]) username = Heatwave::Configuration.fetch(profile&.to_sym, :username) password = Heatwave::Configuration.fetch(profile&.to_sym, :password) @headers = { Authorization: ActionController::HttpAuthentication::Basic.encode_credentials(username, password) } end @headers ||= {} @logger = [:logger] || Rails.logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'app/services/transport/basic_http_api_connection.rb', line 10 def logger @logger end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
10 11 12 |
# File 'app/services/transport/basic_http_api_connection.rb', line 10 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
49 50 51 52 53 54 55 56 57 58 |
# File 'app/services/transport/basic_http_api_connection.rb', line 49 def batch_download_data(directory, pattern = nil) data = {} logger.info "Attempting connection to http endpoint: #{directory}" http_obj = HTTP @headers.each do |k, v| http_obj = http_obj.headers(k.to_s.upcase.dasherize => v) end data[pattern || 'response'] = http_obj.get(directory).to_s data end |
#rm(remote_file_path) ⇒ Object
60 61 62 |
# File 'app/services/transport/basic_http_api_connection.rb', line 60 def rm(remote_file_path) # Do nothing end |
#send_data(data, remote_filepath, method = 'post') ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'app/services/transport/basic_http_api_connection.rb', line 25 def send_data(data, remote_filepath, method = 'post') method = 'put' unless VALID_HTTP_METHODS.include?(method) logger.debug('BasicHttpApiConnection send_data starting', header_count: @headers.size) # The :retry middleware on #connection replaces the old Retryable wrapper; # the retry_block counts retries so we still report attempt_number_reached. attempts = 1 http_res = connection(retry_block: ->(**) { attempts += 1 }).run_request(method.to_sym, remote_filepath, JSON.parse(data), nil) logger.debug('BasicHttpApiConnection request complete', url: remote_filepath, 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
send_data is the only caller, and it is Faraday-backed (#status). batch_download_data
never calls this.
38 39 40 41 42 43 44 45 46 |
# File 'app/services/transport/basic_http_api_connection.rb', line 38 def successful?(http_res) res = false if (http_res.status >= 200) && (http_res.status < 300) res = true ack = JSON.parse(http_res.body.to_s).with_indifferent_access[@success_call.to_sym] res = false if ack && (ack != 'Success') end res end |