Module: SBF::Client::Api::Request

Includes:
HTTParty
Defined in:
lib/stbaldricks/request.rb

Class Method Summary collapse

Class Method Details

.file_post_request(path:, params: {}, file:, filename:) ⇒ Object



59
60
61
62
63
64
65
66
67
# 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 = {}) ⇒ HTTParty::Response

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

Parameters:

  • path (string)
  • params (Hash) (defaults to: {})

Returns:

  • (HTTParty::Response)

Raises:



35
36
37
38
39
40
41
# 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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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 = {}) ⇒ HTTParty::Response

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

Parameters:

  • path (string)
  • params (Hash) (defaults to: {})

Returns:

  • (HTTParty::Response)

Raises:



51
52
53
54
55
56
57
# 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