Class: Dor::Workflow::Client::Requestor

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/workflow/client/requestor.rb

Overview

Makes requests to the workflow service and retries them if necessary.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection:) ⇒ Requestor

Returns a new instance of Requestor.



8
9
10
# File 'lib/dor/workflow/client/requestor.rb', line 8

def initialize(connection:)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



12
13
14
# File 'lib/dor/workflow/client/requestor.rb', line 12

def connection
  @connection
end

Instance Method Details

#request(uri_string, meth = 'get', payload = '', opts = {}) ⇒ Object

calls workflow_resource[uri_string]."#meth" with variable number of optional arguments The point of this is to wrap ALL remote calls with consistent error handling and logging

Parameters:

  • uri_string (String)

    resource to request

  • meth (String) (defaults to: 'get')

    REST method to use on resource (get, put, post, delete, etc.)

  • payload (String) (defaults to: '')

    body for (e.g. put) request

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

    addtional headers options

Returns:

  • (Object)

    response from method

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dor/workflow/client/requestor.rb', line 22

def request(uri_string, meth = 'get', payload = '', opts = {})
  response = send_workflow_resource_request(uri_string, meth, payload, opts)
  response.body
rescue Faraday::Error => e
  # `status` is set to `nil` if:
  # * `e` does not respond to `:response`
  # * `e` responds to `:response` and:
  #   * `e.response` is `nil`
  #   * `e.response` is a hash missing the `:status` key
  # else it is set to the value of `e.response[:status]`
  status = e.try(:response)&.fetch(:status, nil)
  msg = "Failed to retrieve resource: #{meth} #{base_url}/#{uri_string}"
  msg += " (HTTP status #{status})" unless status.nil?
  raise (status == 404 ? Dor::MissingWorkflowException : Dor::WorkflowException), msg
end