Class: RestAPIBuilder::RequestSingleton

Inherits:
Object
  • Object
show all
Includes:
UrlHelper
Defined in:
lib/rest_api_builder/request.rb

Instance Method Summary collapse

Methods included from UrlHelper

#full_url

Instance Method Details

#execute(base_url:, method:, body: nil, headers: {}, query: nil, path: nil, logger: nil, parse_json: false, rest_client_options: {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rest_api_builder/request.rb', line 15

def execute(
  base_url:,
  method:,
  body: nil,
  headers: {},
  query: nil,
  path: nil,
  logger: nil,
  parse_json: false,
  rest_client_options: {}
)
  if method == :get && body
    raise ArgumentError, 'GET requests do not support body'
  end

  response_parser = RestAPIBuilder::RestClientResponseParser.new(logger: logger, parse_json: parse_json)
  headers = headers.merge(params: query) if query

  begin
    response = RestClient::Request.execute(
      method: method,
      url: full_url(base_url, path),
      payload: body,
      headers: headers,
      log: logger,
      **rest_client_options
    )
    response_parser.parse_response(response, success: true)
  rescue RestClient::RequestFailed => e
    raise e unless e.response

    response_parser.parse_response(e.response, success: false)
  end
end

#json_execute(headers: {}, body: nil, **options) ⇒ Object



9
10
11
12
13
# File 'lib/rest_api_builder/request.rb', line 9

def json_execute(headers: {}, body: nil, **options)
  headers = headers.merge(content_type: :json)
  body &&= JSON.generate(body)
  execute(**options, parse_json: true, headers: headers, body: body)
end