Module: BerkeleyLibrary::Util::URIs::Requester::ClassMethods

Included in:
BerkeleyLibrary::Util::URIs::Requester
Defined in:
lib/berkeley_library/util/uris/requester/class_methods.rb

Overview

rubocop:disable Metrics/ParameterLists

Instance Method Summary collapse

Instance Method Details

#get(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS, timeout: DEFAULT_TIMEOUT_SECONDS) ⇒ String

Performs a GET request and returns the response body as a string.

Parameters:

  • uri (URI, String)

    the URI to GET

  • params (Hash) (defaults to: {})

    the query parameters to add to the URI. (Note that the URI may already include query parameters.)

  • headers (Hash) (defaults to: {})

    the request headers.

  • log (Boolean) (defaults to: true)

    whether to log each request URL and response code

  • max_retries (Integer) (defaults to: MAX_RETRIES)

    the maximum number of times to retry after a 429 or 503 with Retry-After

  • max_retry_delay (Integer) (defaults to: MAX_RETRY_DELAY_SECONDS)

    the maximum retry delay (in seconds) to accept in a Retry-After header

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT_SECONDS)

    the request timeout in seconds (RestClient will use this to set both open and read timeouts)

Returns:

  • (String)

    the body as a string.

Raises:

  • (RestClient::Exception)

    in the event of an unsuccessful request.



18
19
20
21
22
# File 'lib/berkeley_library/util/uris/requester/class_methods.rb', line 18

def get(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS,
        timeout: DEFAULT_TIMEOUT_SECONDS)
  resp = make_request(:get, uri, params, headers, log, max_retries, max_retry_delay, timeout)
  resp.body
end

#get_response(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS, timeout: DEFAULT_TIMEOUT_SECONDS) ⇒ RestClient::Response

Performs a GET request and returns the response, even in the event of a failed request.

Parameters:

  • uri (URI, String)

    the URI to GET

  • params (Hash) (defaults to: {})

    the query parameters to add to the URI. (Note that the URI may already include query parameters.)

  • headers (Hash) (defaults to: {})

    the request headers.

  • log (Boolean) (defaults to: true)

    whether to log each request URL and response code

Returns:

  • (RestClient::Response)

    the response



47
48
49
50
51
52
# File 'lib/berkeley_library/util/uris/requester/class_methods.rb', line 47

def get_response(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS,
                 timeout: DEFAULT_TIMEOUT_SECONDS)
  make_request(:get, uri, params, headers, log, max_retries, max_retry_delay, timeout)
rescue RestClient::Exception => e
  e.response
end

#head(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS, timeout: DEFAULT_TIMEOUT_SECONDS) ⇒ Integer

Performs a HEAD request and returns the response status as an integer. Note that unlike BerkeleyLibrary::Util::URIs#get, this does not raise an error in the event of an unsuccessful request.

Parameters:

  • uri (URI, String)

    the URI to HEAD

  • params (Hash) (defaults to: {})

    the query parameters to add to the URI. (Note that the URI may already include query parameters.)

  • headers (Hash) (defaults to: {})

    the request headers.

  • log (Boolean) (defaults to: true)

    whether to log each request URL and response code

Returns:

  • (Integer)

    the response code as an integer.



33
34
35
36
37
# File 'lib/berkeley_library/util/uris/requester/class_methods.rb', line 33

def head(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS,
         timeout: DEFAULT_TIMEOUT_SECONDS)
  head_response(uri, params: params, headers: headers, log: log, max_retries: max_retries, max_retry_delay: max_retry_delay,
                     timeout: timeout).code
end

#head_response(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS, timeout: DEFAULT_TIMEOUT_SECONDS) ⇒ RestClient::Response

Performs a HEAD request and returns the response, even in the event of a failed request.

Parameters:

  • uri (URI, String)

    the URI to HEAD

  • params (Hash) (defaults to: {})

    the query parameters to add to the URI. (Note that the URI may already include query parameters.)

  • headers (Hash) (defaults to: {})

    the request headers.

  • log (Boolean) (defaults to: true)

    whether to log each request URL and response code

Returns:

  • (RestClient::Response)

    the response



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

def head_response(uri, params: {}, headers: {}, log: true, max_retries: MAX_RETRIES, max_retry_delay: MAX_RETRY_DELAY_SECONDS,
                  timeout: DEFAULT_TIMEOUT_SECONDS)
  make_request(:head, uri, params, headers, log, max_retries, max_retry_delay, timeout)
rescue RestClient::Exception => e
  return e.response if e.response

  raise
end