Class: RestClientWrapper::RestClient

Inherits:
Object
  • Object
show all
Includes:
Http
Defined in:
lib/rest_client_wrapper/rest_client.rb

Overview

RestClient

Constant Summary collapse

DEFAULT_RETRY =
{ max_retry: 0, wait: 0 }.freeze
DEFAULT_CONFIG =
{
  retries: {
    401 => { max_retry: 1, wait: 0 }, # unauthorized
    429 => { max_retry: 3, wait: 3 }  # too many requests
  }
}.freeze

Constants included from Http

Http::SUCCESS_STATUS_CODES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Http

bad_request?, not_found?, ok?, success?, too_many_requests?, unauthorized?

Constructor Details

#initialize(host:, config: {}) ⇒ RestClient

Returns a new instance of RestClient.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rest_client_wrapper/rest_client.rb', line 44

def initialize(host:, config: {})
  @host          = host
  @config        = config
  @retry_configs = {}.reverse_merge(DEFAULT_CONFIG[:retries])

  @config[:retries]&.each do |k, v|
    next unless Rack::Utils::HTTP_STATUS_CODES[k.to_i] # skip invalid codes

    @retry_configs[k.to_i] = v.reverse_merge(DEFAULT_RETRY)
  end

  _reset_retries
end

Instance Attribute Details

#authenticatorObject

Returns the value of attribute authenticator.



34
35
36
# File 'lib/rest_client_wrapper/rest_client.rb', line 34

def authenticator
  @authenticator
end

#paginatorObject

Returns the value of attribute paginator.



34
35
36
# File 'lib/rest_client_wrapper/rest_client.rb', line 34

def paginator
  @paginator
end

Instance Method Details

#execute(request:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rest_client_wrapper/rest_client.rb', line 58

def execute(request:)
  _reset_retries
  _validate_request(request)
  _set_auth(request)
  url = _build_uri(request)

  loop do
    access_token = @authenticator.is_a?(Authenticator::Oauth) ? @authenticator.access_token : nil
    response_code = nil

    begin
      payload = Request::HTTP_METHOD_FOR_JSON.include?(request.http_method) && request.headers[:content_type] == :json ? request.payload.to_json : request.payload
      request.headers[:params] = request.query_params
      response = ::RestClient::Request.execute({ method: request.http_method, url: url, payload: payload, headers: request.headers })
      response_code = response&.code
    rescue StandardError => e
      response_code = e.response&.code
      # Any response that doesn't have a status of 200...207 will be raised as an exception from Rest-Client
      raise RestClientError.new("API request encountered an unhandled exception", e.response, e) unless _retry?(response_code)
    end

    return Response.new(response.headers, _parse_response(response), response.code) if Http.success?(response_code)
    raise RestClientNotSuccessful.new("API request was not successful", response) unless _retry?(response_code)

    _wait_and_retry(response_code, access_token)
    next
  end
end

#execute_paginated_request(request:, data: true) ⇒ Object



87
88
89
# File 'lib/rest_client_wrapper/rest_client.rb', line 87

def execute_paginated_request(request:, data: true)
  return self.make_request_for_pages({ http_method: request.http_method, uri: request.uri, segment_params: request.segment_params, query_params: request.query_params, headers: request.headers, data: data }) # rubocop:disable Metrics/LineLength
end

#make_request(http_method:, uri:, payload: {}, segment_params: {}, query_params: {}, headers: {}) ⇒ Object



91
92
93
94
95
# File 'lib/rest_client_wrapper/rest_client.rb', line 91

def make_request(http_method:, uri:, payload: {}, segment_params: {}, query_params: {}, headers: {})
  request = Request.new({ http_method: http_method, uri: uri, payload: payload, segment_params: segment_params, query_params: query_params })
  request.headers = headers
  return self.execute({ request: request })
end

#make_request_for_pages(http_method:, uri:, segment_params: {}, query_params: {}, headers: {}, data: false) ⇒ Object

Raises:



97
98
99
100
101
102
# File 'lib/rest_client_wrapper/rest_client.rb', line 97

def make_request_for_pages(http_method:, uri:, segment_params: {}, query_params: {}, headers: {}, data: false)
  raise RestClientError.new("Paginator not set, unable to make API call", nil, nil) unless @paginator

  @paginator.rest_client ||= self
  return @paginator.paginate({ http_method: http_method, uri: uri, segment_params: segment_params, query_params: query_params, headers: headers, data: data })
end