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

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

Class Method Summary collapse

Class Method Details

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



65
66
67
68
69
70
71
72
73
# File 'lib/stbaldricks/request.rb', line 65

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:



37
38
39
40
41
42
43
44
45
46
# File 'lib/stbaldricks/request.rb', line 37

def self.get_request(path, params = {})
  raise SBF::Client::InvalidURIError, "Invalid URI: #{path}" unless valid_uri?(path)

  # Use a public request if the user has not logged in
  return public_get_request(path, params) if user_token.nil?

  request_log('GET', path, params) do
    get(path, options(query: params, auth: user_token))
  end
end

.options(auth: nil, type: :get, query: nil, body: nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/stbaldricks/request.rb', line 97

def self.options(auth: nil, type: :get, query: nil, body: nil)
  {}.tap do |options|
    options.merge!(base_uri: SBF::Client::Configuration.base_uri,
                   headers: {
                     'Accept' => 'application/json',
                     'X-Request-Id' => SBF::Client::Configuration.request_id
                   },
                   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:



56
57
58
59
60
61
62
63
# File 'lib/stbaldricks/request.rb', line 56

def self.post_request(path, params = {})
  raise SBF::Client::InvalidURIError, "Invalid URI: #{path}" unless valid_uri?(path)
  raise SBF::Client::Error, 'Not yet authenticated' if user_token.nil?

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

.public_get_request(path, params = {}) ⇒ Object

Makes a HTTP GET request to the specified path with specified params This is used for login which is the only request which should use the public key.



78
79
80
81
82
83
84
# File 'lib/stbaldricks/request.rb', line 78

def self.public_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: general_token))
  end
end

.public_post_request(path, params = {}) ⇒ Object

Makes a HTTP POST request to the specified path with specified params This is used for login which is the only request which should use the public key.



89
90
91
92
93
94
95
# File 'lib/stbaldricks/request.rb', line 89

def self.public_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: general_token))
  end
end