Sha256: e275692b42411618716e7c89d17f79a460b723775fc6f431d628bc11b7325f10
Contents?: true
Size: 1.91 KB
Versions: 1
Compression:
Stored size: 1.91 KB
Contents
# frozen_string_literal: true module Dor module Services class Client # @abstract API calls to a versioned endpoint class VersionedService EXCEPTION_CLASS = { 400 => BadRequestError, 401 => UnauthorizedResponse, 404 => NotFoundResponse, 409 => ConflictResponse, 412 => PreconditionFailedResponse }.freeze def initialize(connection:, version:) @connection = connection @api_version = version end # Common interface for handling asynchronous results def async_result(url:) AsyncResult.new(url: url) end private attr_reader :connection, :api_version def raise_exception_based_on_response!(response, object_identifier = nil) data = if response.headers['content-type'] == 'application/json' JSON.parse(response.body) else {} end exception_class = EXCEPTION_CLASS.fetch(response.status, UnexpectedResponse) raise exception_class.new(response: response, object_identifier: object_identifier, errors: data.fetch('errors', [])) end def build_cocina_from_response(response) cocina_object = Cocina::Models.build(JSON.parse(response.body)) Cocina::Models.with_metadata(cocina_object, response.headers['ETag'], created: date_from_header(response, 'X-Created-At'), modified: date_from_header(response, 'Last-Modified')) end def build_json_from_cocina(cocina_object) Cocina::Models.without_metadata(cocina_object).to_json end def date_from_header(response, key) response.headers[key]&.to_datetime end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dor-services-client-9.1.0 | lib/dor/services/client/versioned_service.rb |