folio_client 0.5.0 → 0.6.1

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: ba0a7a402a239866f9e2e3aaeae1a7a3fa4f113a97aa5fac4b5f8a54037fb734
4
- data.tar.gz: 4184b99c342fe2d9d01ab7763f2fc8407f93f70a8370e44552e1a639ac1753fb
3
+ metadata.gz: db6b3bd02eac883e93ca7a24725f1c3d4f1ef48b34a3aa6e9326070e1903f600
4
+ data.tar.gz: e2592286592f8fe064b326373dc3f8ca3123a7fa775fedcb23f00da97dc58638
5
5
  SHA512:
6
- metadata.gz: 9a1a19fbfe4e6dfe76e30ba00fe22873cfcd96f7df5241db8118d5ed8d7b16812e03bed56653418bf6883acf1447dd8fc93364d3adf5703fac31468fcbc83055
7
- data.tar.gz: f1b8d6af99291e0d4a2c12a2fb7831cef7ba7587c6f979c5ae764b77c665860b6115fee27cd0f18cbabfc3a553385d64728297c07ce3999478f094fa143d9035
6
+ metadata.gz: 7ee03f5f2ae933bab57d6a3c6b2be09d7933b98f51ea66c07ffb09e821c4a5c9a08564125c01cd0858a4347465f8e588314b25bc26296851e61e593f8e1c42bd
7
+ data.tar.gz: '0170689c2a3d9bd2547b976cd43b7258cc3dd6c51a5a8caba9ba62774d3425e62f624024c155d3b728a7dea8df30bce23ee7f111f628c5c1649c8327891e1cc9'
data/.rubocop/custom.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 3.1
2
+ TargetRubyVersion: 3.0
3
3
  DisplayCopNames: true
4
4
  SuggestExtensions: false
5
5
  Exclude:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- folio_client (0.5.0)
4
+ folio_client (0.6.1)
5
5
  activesupport (>= 4.2, < 8)
6
6
  faraday
7
7
  zeitwerk
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  # FolioClient
7
7
 
8
- FolioClient is a Ruby gem that acts as a client to the RESTful HTTP APIs provided by the Folio ILS API.
8
+ FolioClient is a Ruby gem that acts as a client to the RESTful HTTP APIs provided by the Folio ILS API. It requires ruby 3.0 or better.
9
9
 
10
10
  ## Installation
11
11
 
@@ -19,6 +19,8 @@ If bundler is not being used to manage dependencies, install the gem by executin
19
19
 
20
20
  ## Usage
21
21
 
22
+ The gem should be configured first, and then you can either call API endpoints directly using GET or POST, or more commonly, use the helper methods provided, as described in the section below.
23
+
22
24
  ```ruby
23
25
  require 'folio_client'
24
26
 
@@ -29,7 +31,9 @@ client = FolioClient.configure(
29
31
  okapi_headers: { 'X-Okapi-Tenant': 'sul', 'User-Agent': 'FolioApiClient' }
30
32
  )
31
33
 
32
- response = client.get('/organizations/organizations')
34
+ response = client.get('/organizations/organizations', {query_string_param: 'abcdef'})
35
+
36
+ response = client.post('/some/post/endpoint', params_hash.to_json)
33
37
  ```
34
38
 
35
39
  Note that the settings will live in the consumer of this gem and would typically be used like this:
@@ -44,6 +48,30 @@ client = FolioClient.configure(
44
48
  )
45
49
  ```
46
50
 
51
+ The client is smart enough to automatically request a new token if it detects the one it is using has expired. If for some reason, you want to immediately request a new token, you can do this:
52
+
53
+ ```ruby
54
+ client.config.token = FolioClient::Authenticator.token(client.config.login_params, client.connection)
55
+ ```
56
+
57
+ ## API Coverage
58
+
59
+ FolioClient provides a number of methods to simplify connecting to the RESTful HTTP API of the Folio API. In this section we list all of the available methods, reflecting how much of the API the client covers. Note that this assumes the client has already been configured, as described above. See dor-services-app for an example of configuration and usage.
60
+
61
+ ```ruby
62
+ # Lookup an instance hrid given a barcode
63
+ # returns a string if found, nil if nothing found
64
+ client.fetch_hrid(barcode: "12345")
65
+ => "a7927874"
66
+
67
+ # Request a MARC record given an instance hrid
68
+ # returns a hash if found; raises FolioClient::UnexpectedResponse::ResourceNotFound if instance_hrid not found
69
+ client.fetch_marc_hash(instance_hrid: "a7927874")
70
+ => {"fields"=>
71
+ [{"003"=>"FOLIO"}....]
72
+ }
73
+ ```
74
+
47
75
  ## Development
48
76
 
49
77
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -4,7 +4,7 @@ class FolioClient
4
4
  # Wraps API operations to request new access token if expired
5
5
  class TokenWrapper
6
6
  def self.refresh(config, connection)
7
- yield
7
+ yield.tap { |response| UnexpectedResponse.call(response) unless response.success? }
8
8
  rescue UnauthorizedError
9
9
  config.token = Authenticator.token(config.login_params, connection)
10
10
  yield
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class FolioClient
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.1"
5
5
  end
data/lib/folio_client.rb CHANGED
@@ -41,7 +41,7 @@ class FolioClient
41
41
  # @param login_params [Hash] the folio client login params (username:, password:)
42
42
  # @param okapi_headers [Hash] the okapi specific headers to add (X-Okapi-Tenant:, User-Agent:)
43
43
  def configure(url:, login_params:, okapi_headers:)
44
- instance.config = OpenStruct.new(url:, login_params:, okapi_headers:, token: nil)
44
+ instance.config = OpenStruct.new(url: url, login_params: login_params, okapi_headers: okapi_headers, token: nil)
45
45
 
46
46
  instance.config.token = Authenticator.token(login_params, connection)
47
47
 
@@ -58,7 +58,9 @@ class FolioClient
58
58
  # @param path [String] the path to the Folio API request
59
59
  # @param request [Hash] params to get to the API
60
60
  def get(path, params = {})
61
- response = connection.get(path, params, {"x-okapi-token": config.token})
61
+ response = TokenWrapper.refresh(config, connection) do
62
+ connection.get(path, params, {"x-okapi-token": config.token})
63
+ end
62
64
 
63
65
  UnexpectedResponse.call(response) unless response.success?
64
66
 
@@ -69,7 +71,9 @@ class FolioClient
69
71
  # @param path [String] the path to the Folio API request
70
72
  # @param request [json] request body to post to the API
71
73
  def post(path, request = nil)
72
- response = connection.post(path, request, {"x-okapi-token": config.token})
74
+ response = TokenWrapper.refresh(config, connection) do
75
+ connection.post(path, request, {"x-okapi-token": config.token})
76
+ end
73
77
 
74
78
  UnexpectedResponse.call(response) unless response.success?
75
79
 
@@ -85,25 +89,18 @@ class FolioClient
85
89
  end
86
90
 
87
91
  # Public methods available on the FolioClient below
88
- # Wrap methods in `TokenWrapper` to ensure a new token is fetched automatically if expired
89
92
  def fetch_hrid(...)
90
- TokenWrapper.refresh(config, connection) do
91
- inventory = Inventory.new(self)
92
- inventory.fetch_hrid(...)
93
- end
93
+ inventory = Inventory.new(self)
94
+ inventory.fetch_hrid(...)
94
95
  end
95
96
 
96
97
  def fetch_marc_hash(...)
97
- TokenWrapper.refresh(config, connection) do
98
- source_storage = SourceStorage.new(self)
99
- source_storage.fetch_marc_hash(...)
100
- end
98
+ source_storage = SourceStorage.new(self)
99
+ source_storage.fetch_marc_hash(...)
101
100
  end
102
101
 
103
102
  def has_instance_status?(...)
104
- TokenWrapper.refresh(config, connection) do
105
- inventory = Inventory.new(self)
106
- inventory.has_instance_status?(...)
107
- end
103
+ inventory = Inventory.new(self)
104
+ inventory.has_instance_status?(...)
108
105
  end
109
106
  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.5.0
4
+ version: 0.6.1
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-27 00:00:00.000000000 Z
11
+ date: 2023-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport