Class: BerkeleyLibrary::Util::URIs::Requester

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
Logging
Defined in:
lib/berkeley_library/util/uris/requester.rb,
lib/berkeley_library/util/uris/requester/class_methods.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

SUPPORTED_METHODS =

Constants

i[get head].freeze
RETRY_HEADER =
:retry_after
RETRY_STATUSES =
[429, 503].freeze
MAX_RETRY_DELAY_SECONDS =
10
MAX_RETRIES =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

get, get_response, head, head_response

Constructor Details

#initialize(method, url, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS) ⇒ Requester

Initializes a new Requester.

rubocop:disable Metrics/ParameterLists

Raises:

  • URI::InvalidURIError if the specified URL is invalid



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/berkeley_library/util/uris/requester.rb', line 43

def initialize(method, url, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS)
  raise ArgumentError, "#{method} not supported" unless SUPPORTED_METHODS.include?(method)
  raise ArgumentError, 'url cannot be nil' unless (uri = Validator.uri_or_nil(url))

  @method = method
  @url_str = url_str_with_params(uri, params)
  @headers = headers
  @log = log
  @max_retries = max_retries
  @max_retry_delay = max_retry_delay
end

Instance Attribute Details

#headersObject (readonly)


Attributes



27
28
29
# File 'lib/berkeley_library/util/uris/requester.rb', line 27

def headers
  @headers
end

#logObject (readonly)


Attributes



27
28
29
# File 'lib/berkeley_library/util/uris/requester.rb', line 27

def log
  @log
end

#max_retriesObject (readonly)


Attributes



27
28
29
# File 'lib/berkeley_library/util/uris/requester.rb', line 27

def max_retries
  @max_retries
end

#max_retry_delayObject (readonly)


Attributes



27
28
29
# File 'lib/berkeley_library/util/uris/requester.rb', line 27

def max_retry_delay
  @max_retry_delay
end

#methodObject (readonly)


Attributes



27
28
29
# File 'lib/berkeley_library/util/uris/requester.rb', line 27

def method
  @method
end

#url_strObject (readonly)


Attributes



27
28
29
# File 'lib/berkeley_library/util/uris/requester.rb', line 27

def url_str
  @url_str
end

Instance Method Details

#make_requestRestClient::Response



61
62
63
64
65
66
67
68
# File 'lib/berkeley_library/util/uris/requester.rb', line 61

def make_request
  execute_request.tap do |resp|
    log_response(resp)
  end
rescue RestClient::Exception => e
  log_response(e.response)
  raise
end