lib/folio_client.rb in folio_client-0.7.0 vs lib/folio_client.rb in folio_client-0.8.0

- old
+ new

@@ -15,11 +15,11 @@ include Singleton # Base class for all FolioClient errors class Error < StandardError; end - # Error raised by the Folio Auth API returns a 422 Unauthorized + # Error raised by the Folio Auth API returns a 401 Unauthorized class UnauthorizedError < Error; end # Error raised when the Folio API returns a 404 NotFound, or returns 0 results when one was expected class ResourceNotFound < Error; end @@ -30,10 +30,13 @@ class ForbiddenError < Error; end # Error raised when the Folio API returns a 500 class ServiceUnavailable < Error; end + # Error raised when the Folio API returns a 422 Unprocessable Entity + class ValidationError < Error; end + DEFAULT_HEADERS = { accept: "application/json, text/plain", content_type: "application/json" }.freeze @@ -47,12 +50,12 @@ instance.config.token = Authenticator.token(login_params, connection) self end - delegate :config, :connection, :get, :post, to: :instance - delegate :fetch_hrid, :fetch_marc_hash, :has_instance_status?, :data_import, to: :instance + delegate :config, :connection, :get, :post, :put, to: :instance + delegate :fetch_hrid, :fetch_external_id, :fetch_instance_info, :fetch_marc_hash, :has_instance_status?, :data_import, :holdings, :edit_marc_json, to: :instance end attr_accessor :config # Send an authenticated get request @@ -89,10 +92,31 @@ return nil if response.body.blank? JSON.parse(response.body) end + # Send an authenticated put request + # If the body is JSON, it will be automatically serialized + # @param path [String] the path to the Folio API request + # @param body [Object] body to put to the API as JSON + def put(path, body = nil, content_type: "application/json") + req_body = (content_type == "application/json") ? body&.to_json : body + response = TokenWrapper.refresh(config, connection) do + req_headers = { + "x-okapi-token": config.token, + "content-type": content_type + } + connection.put(path, req_body, req_headers) + end + + UnexpectedResponse.call(response) unless response.success? + + return nil if response.body.blank? + + JSON.parse(response.body) + end + # the base connection to the Folio API def connection @connection ||= Faraday.new( url: config.url, headers: DEFAULT_HEADERS.merge(config.okapi_headers || {}) @@ -104,10 +128,22 @@ Inventory .new(self) .fetch_hrid(...) end + def fetch_external_id(...) + Inventory + .new(self) + .fetch_external_id(...) + end + + def fetch_instance_info(...) + Inventory + .new(self) + .fetch_instance_info(...) + end + def fetch_marc_hash(...) SourceStorage .new(self) .fetch_marc_hash(...) end @@ -120,7 +156,18 @@ def data_import(...) DataImport .new(self) .import(...) + end + + def holdings(...) + Holdings + .new(self, ...) + end + + def edit_marc_json(...) + RecordsEditor + .new(self) + .edit_marc_json(...) end end