Sha256: 5e51d907ba2b6dbfd9b53e55e3f6b3850c344d0cea44ed25268b7855fa6c9cd9

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require 'json'

module Dor
  module Services
    class Client
      # API calls around background job results from dor-services-app
      class BackgroundJobResults < VersionedService
        # Get status/result of a background job
        # @param job_id [String] required string representing a job identifier
        # @raise [NotFoundResponse] when the response is a 404 (object not found)
        # @raise [UnexpectedResponse] on an unsuccessful response from the server
        # @return [String] result of background job
        def show(job_id:)
          resp = connection.get do |req|
            req.url "#{api_version}/background_job_results/#{job_id}"
            req.headers['Accept'] = 'application/json'
          end
          # We expect this endpoint to semi-regularly return 422 responses, so do
          # not bother raising the exception
          return JSON.parse(resp.body).with_indifferent_access if resp.success? || resp.status == 422

          raise_exception_based_on_response!(resp)
        end

        private

        def raise_exception_based_on_response!(response)
          raise (response.status == 404 ? NotFoundResponse : UnexpectedResponse),
                ResponseErrorFormatter.format(response: response)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dor-services-client-3.1.0 lib/dor/services/client/background_job_results.rb
dor-services-client-3.0.0 lib/dor/services/client/background_job_results.rb