Module: Motor::ApiConfigs
- Defined in:
- lib/motor/api_configs.rb
Constant Summary collapse
- METHODS =
%w[get post put delete].freeze
- DEFAULT_HEADERS =
{ 'Content-Type' => 'application/json' }.freeze
- InvalidHttpMethod =
Class.new(StandardError)
Class Method Summary collapse
- .run(api_config, method: nil, path: nil, body: nil, params: {}, headers: {}) ⇒ Object
- .run_grapql(api_config, query:, variables: {}, headers: {}) ⇒ Object
Class Method Details
.run(api_config, method: nil, path: nil, body: nil, params: {}, headers: {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/motor/api_configs.rb', line 13 def run(api_config, method: nil, path: nil, body: nil, params: {}, headers: {}) method ||= 'get' raise InvalidHttpMethod unless METHODS.include?(method.downcase) Motor::NetHttpUtils.public_send( method.downcase.to_sym, api_config.url.to_s.sub(%r{/?\z}, '/') + path.delete_prefix('/'), params, DEFAULT_HEADERS.merge(headers).merge(api_config.headers), body&.to_json ) end |
.run_grapql(api_config, query:, variables: {}, headers: {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/motor/api_configs.rb', line 27 def run_grapql(api_config, query:, variables: {}, headers: {}) body = { query: query, variables: variables.merge(form_data: variables) } Motor::NetHttpUtils.post( api_config.url, {}, DEFAULT_HEADERS.merge(headers).merge(api_config.headers), body.to_json ) end |