folio_client 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb2abde077e892ded8a30319b08e773915d646d5c0193b8080789a49c73014e6
4
- data.tar.gz: 9a8de9e86652903db4068919af862d08abfa9a0cd86fea4d78500d206620fa25
3
+ metadata.gz: 960e3e0d138665bbf21b4f609fb7c789c52110101bf303c42ba2c98b93583b98
4
+ data.tar.gz: b2690e1193c7391a6e4bd6bae4c4f417f395ed91faa50d6215c43e1fc39aaadd
5
5
  SHA512:
6
- metadata.gz: b187b89f764fce8d797f3caa2b654f08895868cfa2732b049b3e40c30439ddc4f38aae1ae77f7f8ab0e433844c600ab615daa72d12a7768f261d2f44e232d897
7
- data.tar.gz: f7244e622f86eb96c8562baa62781c30cebc9909060945eac932b9ed8bcb3e3e08e6ba8578b1bd7712ac7964c3c7df630b2dffed8ed59708ee59cad1859b247a
6
+ metadata.gz: 42cbfadefe92754026e08435bd06378e6dee17ae97e539bd9d71d7833e873c02194ad1762690b55087f002f5fca553f63579a981dfe915674ee95fb8e839bbf2
7
+ data.tar.gz: 4e2067aa01d4cd2534079cc9a5d11834d73432cd05c209fc57553713ec7d07a27dcf458ec52cace086fab3971d0fe3b9048e3e30e0669d3736d2ea37dcd0589b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- folio_client (0.2.0)
4
+ folio_client (0.3.0)
5
5
  activesupport (>= 4.2, < 8)
6
6
  faraday
7
7
  zeitwerk
@@ -66,7 +66,7 @@ GEM
66
66
  unicode-display_width (>= 2.4.0, < 3.0)
67
67
  rubocop-ast (1.26.0)
68
68
  parser (>= 3.2.1.0)
69
- rubocop-capybara (2.17.0)
69
+ rubocop-capybara (2.17.1)
70
70
  rubocop (~> 1.41)
71
71
  rubocop-performance (1.15.2)
72
72
  rubocop (>= 1.7.0, < 2.0)
@@ -97,6 +97,7 @@ GEM
97
97
 
98
98
  PLATFORMS
99
99
  x86_64-darwin-19
100
+ x86_64-darwin-20
100
101
  x86_64-linux
101
102
 
102
103
  DEPENDENCIES
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class FolioClient
4
+ # Lookup records in Folio Source Storage
5
+ class SourceStorage
6
+ attr_accessor :client
7
+
8
+ # @param client [FolioClient] the configured client
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ # @param instance_hrid [String] the key to use for MARC lookup
14
+ # @return [Hash] hash representation of the MARC. should be usable by MARC::Record.new_from_hash (from ruby-marc gem)
15
+ # @raises NotFound, MultipleRecordsForIdentifier
16
+ def fetch_marc_hash(instance_hrid:)
17
+ response_hash = client.get("/source-storage/source-records", {instanceHrid: instance_hrid})
18
+
19
+ record_count = response_hash["totalRecords"]
20
+ raise FolioClient::UnexpectedResponse::ResourceNotFound, "No records found for #{instance_hrid}" if record_count.zero?
21
+ raise FolioClient::UnexpectedResponse::MultipleResourcesFound, "Expected 1 record for #{instance_hrid}, but found #{record_count}" if record_count > 1
22
+
23
+ response_hash["sourceRecords"].first["parsedRecord"]["content"]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class FolioClient
4
+ # Wraps API operations to request new access token if expired
5
+ class TokenWrapper
6
+ def self.refresh(config, connection)
7
+ yield
8
+ rescue UnexpectedResponse::UnauthorizedError
9
+ config.token = Authenticator.token(config.login_params, connection)
10
+ yield
11
+ end
12
+ end
13
+ end
@@ -6,9 +6,12 @@ class FolioClient
6
6
  # Error raised by the Folio Auth API returns a 422 Unauthorized
7
7
  class UnauthorizedError < StandardError; end
8
8
 
9
- # Error raised when the Folio API returns a 404 NotFound
9
+ # Error raised when the Folio API returns a 404 NotFound, or returns 0 results when one was expected
10
10
  class ResourceNotFound < StandardError; end
11
11
 
12
+ # Error raised when e.g. exactly one result was expected, but more than one was returned
13
+ class MultipleResourcesFound < StandardError; end
14
+
12
15
  # Error raised when the Folio API returns a 403 Forbidden
13
16
  class ForbiddenError < StandardError; end
14
17
 
@@ -18,6 +21,8 @@ class FolioClient
18
21
  # @param [Faraday::Response] response
19
22
  def self.call(response)
20
23
  case response.status
24
+ when 401
25
+ raise UnauthorizedError, "There was a problem with the access token: #{response.body}"
21
26
  when 403
22
27
  raise ForbiddenError, "The operation requires privileges which the client does not have: #{response.body}"
23
28
  when 404
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class FolioClient
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/folio_client.rb CHANGED
@@ -31,7 +31,7 @@ class FolioClient
31
31
  end
32
32
 
33
33
  delegate :config, :connection, :get, :post, to: :instance
34
- delegate :fetch_hrid, to: :instance
34
+ delegate :fetch_hrid, :fetch_marc_hash, to: :instance
35
35
  end
36
36
 
37
37
  attr_accessor :config
@@ -66,8 +66,19 @@ class FolioClient
66
66
  )
67
67
  end
68
68
 
69
+ # Public methods available on the FolioClient below
70
+ # Wrap methods in `TokenWrapper` to ensure a new token is fetched automatically if expired
69
71
  def fetch_hrid(...)
70
- inventory = Inventory.new(self)
71
- inventory.fetch_hrid(...)
72
+ TokenWrapper.refresh(config, connection) do
73
+ inventory = Inventory.new(self)
74
+ inventory.fetch_hrid(...)
75
+ end
76
+ end
77
+
78
+ def fetch_marc_hash(...)
79
+ TokenWrapper.refresh(config, connection) do
80
+ source_storage = SourceStorage.new(self)
81
+ source_storage.fetch_marc_hash(...)
82
+ end
72
83
  end
73
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: folio_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Mangiafico
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-15 00:00:00.000000000 Z
11
+ date: 2023-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -163,6 +163,8 @@ files:
163
163
  - lib/folio_client.rb
164
164
  - lib/folio_client/authenticator.rb
165
165
  - lib/folio_client/inventory.rb
166
+ - lib/folio_client/source_storage.rb
167
+ - lib/folio_client/token_wrapper.rb
166
168
  - lib/folio_client/unexpected_response.rb
167
169
  - lib/folio_client/version.rb
168
170
  homepage: https://github.com/sul-dlss/folio_client
@@ -187,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
189
  - !ruby/object:Gem::Version
188
190
  version: '0'
189
191
  requirements: []
190
- rubygems_version: 3.4.5
192
+ rubygems_version: 3.3.7
191
193
  signing_key:
192
194
  specification_version: 4
193
195
  summary: Interface for interacting with the Folio ILS API.