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

- old
+ new

@@ -1,8 +1,9 @@ # frozen_string_literal: true require "active_support/core_ext/module/delegation" +require "active_support/core_ext/object/blank" require "faraday" require "singleton" require "ostruct" require "zeitwerk" @@ -47,11 +48,11 @@ self end delegate :config, :connection, :get, :post, to: :instance - delegate :fetch_hrid, :fetch_marc_hash, :has_instance_status?, to: :instance + delegate :fetch_hrid, :fetch_marc_hash, :has_instance_status?, :data_import, to: :instance end attr_accessor :config # Send an authenticated get request @@ -62,23 +63,33 @@ connection.get(path, params, {"x-okapi-token": config.token}) end UnexpectedResponse.call(response) unless response.success? + return nil if response.body.blank? + JSON.parse(response.body) end # Send an authenticated post request + # If the body is JSON, it will be automatically serialized # @param path [String] the path to the Folio API request - # @param request [json] request body to post to the API - def post(path, request = nil) + # @param body [Object] body to post to the API as JSON + def post(path, body = nil, content_type: "application/json") + req_body = (content_type == "application/json") ? body&.to_json : body response = TokenWrapper.refresh(config, connection) do - connection.post(path, request, {"x-okapi-token": config.token}) + req_headers = { + "x-okapi-token": config.token, + "content-type": content_type + } + connection.post(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 @@ -88,19 +99,28 @@ ) end # Public methods available on the FolioClient below def fetch_hrid(...) - inventory = Inventory.new(self) - inventory.fetch_hrid(...) + Inventory + .new(self) + .fetch_hrid(...) end def fetch_marc_hash(...) - source_storage = SourceStorage.new(self) - source_storage.fetch_marc_hash(...) + SourceStorage + .new(self) + .fetch_marc_hash(...) end def has_instance_status?(...) - inventory = Inventory.new(self) - inventory.has_instance_status?(...) + Inventory + .new(self) + .has_instance_status?(...) + end + + def data_import(...) + DataImport + .new(self) + .import(...) end end