module SBF::Client::Api::Request

Public Class Methods

file_post_request(path:, params: {}, file:, filename:) click to toggle source
# File lib/stbaldricks/request.rb, line 59
def self.file_post_request(path:, params: {}, file:, filename:)
  raise SBF::Client::InvalidURIError, "Invalid URI: #{path}" unless valid_uri?(path)
  raise SBF::Client::Error, 'Not yet authenticated' if user_token.nil?
  raise SBF::Client::Error, 'Invalid File' unless file.is_a?(File) || file.is_a?(Tempfile)

  request_log('POST', path, params) do
    post(path, options(body: params.merge(file: file, original_filename: filename), type: :file_post, auth: user_token))
  end
end
get_request(path, params = {}) click to toggle source

Makes a HTTP GET request to the specified path with specified params and authentication credentials based on the configured client class. This method isn't intended to be used by a user and is for internal use only

@param path [string] @param params [Hash] @return [HTTParty::Response]

# File lib/stbaldricks/request.rb, line 35
def self.get_request(path, params = {})
  raise SBF::Client::InvalidURIError, "Invalid URI: #{path}" unless valid_uri?(path)

  request_log('GET', path, params) do
    get(path, options(query: params, auth: user_token || general_token))
  end
end
options(auth: nil, type: :get, query: nil, body: nil) click to toggle source
# File lib/stbaldricks/request.rb, line 69
def self.options(auth: nil, type: :get, query: nil, body: nil)
  {}.tap do |options|
    options.merge!(
      base_uri: SBF::Client::Configuration.base_uri,
      headers: {}.tap do |hsh|
        hsh['Accept'] = 'application/json'
        hsh['X-Request-Id'] = SBF::Client::Configuration.request_id
        hsh['X-Forwarded-For'] = SBF::Client::Configuration.forwarded_for unless SBF::Client::Configuration.forwarded_for.blank?
      end,
      verify: verify_ssl_peer?
    )
    options[:body] = body if body
    options[:query] = query if query
    options[:headers]['Content-Type'] = 'application/json' if type == :post
    options[:headers]['Content-Type'] = 'multipart/form-data' if type == :file_post
    options[:headers]['Authorization'] = "Bearer #{auth}" if auth
  end
end
post_request(path, params = {}) click to toggle source

Makes a HTTP POST request to the specified path with specified params and authentication credentials based on the configured client class. Raises an error if the client library isn't configured with user specific credentials. This method isn't intended to be used by a user and is for internal use only

@param path [string] @param params [Hash] @return [HTTParty::Response] @raise [Error]

# File lib/stbaldricks/request.rb, line 51
def self.post_request(path, params = {})
  raise SBF::Client::InvalidURIError, "Invalid URI: #{path}" unless valid_uri?(path)

  request_log('POST', path, params) do
    post(path, options(body: params.to_json, type: :post, auth: user_token || general_token))
  end
end

Private Class Methods

general_token() click to toggle source
# File lib/stbaldricks/request.rb, line 88
def self.general_token
  SBF::Client::Configuration.general_token
end
request_log(type, path, params) { || ... } click to toggle source
# File lib/stbaldricks/request.rb, line 98
def self.request_log(type, path, params)
  SBF::Client::LOG.debug { "SBF::Client - #{type}: #{path} | Body: #{JSON.pretty_generate(params)}" }
  response = yield
  SBF::Client::LOG.debug { "SBF::Client - Response: #{response}" }
  response
end
user_token() click to toggle source
# File lib/stbaldricks/request.rb, line 93
def self.user_token
  SBF::Client::Configuration.user_token
end
valid_uri?(path) click to toggle source
# File lib/stbaldricks/request.rb, line 106
def self.valid_uri?(path)
  URI.parse(path)
  true
rescue URI::InvalidURIError
  false
end
verify_ssl_peer?() click to toggle source
# File lib/stbaldricks/request.rb, line 114
def self.verify_ssl_peer?
  (ENV['VERIFY_SSL_PEER'] || true).to_b
end