Sha256: de1f77e73837864b1179c065d366318161288dd045e01d61e31ccb9e3cc7d0b4

Contents?: true

Size: 1.74 KB

Versions: 17

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Dor
  module Workflow
    class Client
      # Makes requests to the workflow service and retries them if necessary.
      class Requestor
        def initialize(connection:)
          @connection = connection
        end

        attr_reader :connection

        # 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
        # @param [String] uri_string resource to request
        # @param [String] meth REST method to use on resource (get, put, post, delete, etc.)
        # @param [String] payload body for (e.g. put) request
        # @param [Hash] opts addtional headers options
        # @return [Object] response from method
        def request(uri_string, meth = 'get', payload = '', opts = {})
          response = send_workflow_resource_request(uri_string, meth, payload, opts)
          response.body
        rescue Faraday::Error => e
          msg = "Failed to retrieve resource: #{meth} #{base_url}/#{uri_string}"
          msg += " (HTTP status #{e.response[:status]})" if e.respond_to?(:response) && e.response
          raise Dor::WorkflowException, msg
        end

        private

        ##
        # Get the configured URL for the connection
        def base_url
          connection.url_prefix
        end

        def send_workflow_resource_request(uri_string, meth = 'get', payload = '', opts = {})
          connection.public_send(meth, uri_string) do |req|
            req.body = payload unless meth == 'delete'
            req.params.update opts[:params] if opts[:params]
            req.headers.update opts.except(:params)
          end
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
dor-workflow-client-3.12.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.11.1 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.11.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.10.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.9.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.8.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.7.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.6.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.5.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.4.2 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.4.1 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.4.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.3.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.2.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.1.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.0.0 lib/dor/workflow/client/requestor.rb
dor-workflow-client-3.0.0.rc1 lib/dor/workflow/client/requestor.rb