mais_orcid_client 0.3.0 → 0.3.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: 5f67823f01c96aa641315a8d64c463bc469c18d746ad5fd05a449a66f3c63925
4
- data.tar.gz: 2f1435dabf75dd8c0956771f4519f978f0b745511310d3bfdc978bb0bf4bafb0
3
+ metadata.gz: b93e75dc3042635205b947c21d9697808b9bdefa9d9dae8598e03db1eac3e128
4
+ data.tar.gz: d714d5c0985596191dba26e801a43455dd926b307e5a88753c66e5a8550d918d
5
5
  SHA512:
6
- metadata.gz: '083990f9b00f45ee537c8f07ea34450160a6b161167345a9e058cacb08fcb68b3c4c710877ae03aefe6f02729d48345536ecba981e9784697646ad6a89857636'
7
- data.tar.gz: 93b51cbc03f7f5e2f8ea8e664cda30c07f817dbe592f0610ff69bfef1c0cbc50218122bb81d1e5a5bd631f5e99c12d4c82bca15add09c8d2a68fcf4f83238c78
6
+ metadata.gz: 227b68aa40fee6c5a55dbea773ef788bc5235c094a8ede1b717881ad9cb4e431de368a4d8e20b7564e60dc9c02f4f0657d47835a2bc490a4f2db174f6ba0f8e0
7
+ data.tar.gz: 8860974d178d49ddca9153d9a71a31ca25d4e2050541c02fc7d185c7ad04d45854dc4087022c6194b9c52883a2d7e385a4a6beb7be0ab5858c41608627e4cb37
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mais_orcid_client (0.3.0)
4
+ mais_orcid_client (0.3.1)
5
5
  activesupport (>= 4.2, < 8)
6
6
  faraday
7
7
  faraday-retry
@@ -11,12 +11,12 @@ PATH
11
11
  GEM
12
12
  remote: https://rubygems.org/
13
13
  specs:
14
- activesupport (7.0.6)
14
+ activesupport (7.0.7)
15
15
  concurrent-ruby (~> 1.0, >= 1.0.2)
16
16
  i18n (>= 1.6, < 2)
17
17
  minitest (>= 5.1)
18
18
  tzinfo (~> 2.0)
19
- addressable (2.8.4)
19
+ addressable (2.8.5)
20
20
  public_suffix (>= 2.0.2, < 6.0)
21
21
  ast (2.4.2)
22
22
  byebug (11.1.3)
@@ -39,7 +39,7 @@ GEM
39
39
  jwt (2.7.1)
40
40
  language_server-protocol (3.17.0.3)
41
41
  lint_roller (1.1.0)
42
- minitest (5.18.1)
42
+ minitest (5.19.0)
43
43
  multi_xml (0.6.0)
44
44
  oauth2 (2.0.9)
45
45
  faraday (>= 0.17.3, < 3.0)
@@ -58,7 +58,7 @@ GEM
58
58
  rainbow (3.1.1)
59
59
  rake (13.0.6)
60
60
  regexp_parser (2.8.1)
61
- rexml (3.2.5)
61
+ rexml (3.2.6)
62
62
  rspec (3.12.0)
63
63
  rspec-core (~> 3.12.0)
64
64
  rspec-expectations (~> 3.12.0)
@@ -91,7 +91,7 @@ GEM
91
91
  rubocop-performance (1.18.0)
92
92
  rubocop (>= 1.7.0, < 2.0)
93
93
  rubocop-ast (>= 0.4.0)
94
- rubocop-rspec (2.22.0)
94
+ rubocop-rspec (2.23.2)
95
95
  rubocop (~> 1.33)
96
96
  rubocop-capybara (~> 2.17)
97
97
  rubocop-factory_bot (~> 2.22)
@@ -127,7 +127,7 @@ GEM
127
127
  addressable (>= 2.8.0)
128
128
  crack (>= 0.3.2)
129
129
  hashdiff (>= 0.4.0, < 2.0.0)
130
- zeitwerk (2.6.8)
130
+ zeitwerk (2.6.11)
131
131
 
132
132
  PLATFORMS
133
133
  x86_64-darwin-19
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MaisOrcidClient
4
+ # The namespace for the "login" command
5
+ class Authenticator
6
+ attr_reader :client_id, :client_secret, :base_url
7
+
8
+ def self.token(client_id, client_secret, base_url)
9
+ new(client_id, client_secret, base_url).token
10
+ end
11
+
12
+ def initialize(client_id, client_secret, base_url)
13
+ @client_id = client_id
14
+ @client_secret = client_secret
15
+ @base_url = base_url
16
+ end
17
+
18
+ # @return [String]
19
+ def token
20
+ client = OAuth2::Client.new(client_id, client_secret, site: base_url,
21
+ token_url: "/api/oauth/token", authorize_url: "/api/oauth/authorize",
22
+ auth_scheme: :request_body)
23
+ client.client_credentials.get_token.token
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MaisOrcidClient
4
+ # Wraps API operations to request new access token if expired
5
+ class TokenWrapper
6
+ def self.refresh(config)
7
+ yield
8
+ rescue UnexpectedResponse::UnauthorizedError
9
+ config.token = Authenticator.token(config.client_id, config.client_secret, config.base_url)
10
+ yield
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class MaisOrcidClient
4
+ # Handles unexpected responses when communicating with Mais
5
+ class UnexpectedResponse
6
+ # Error raised when the Mais API returns a 401 Unauthorized
7
+ class UnauthorizedError < StandardError; end
8
+
9
+ # Error raised when the Mais API returns a 500 error
10
+ class ServerError < StandardError; end
11
+
12
+ # Error raised when the Mais API returns a response with an error message in it
13
+ class ResponseError < StandardError; end
14
+
15
+ def self.call(response)
16
+ case response.status
17
+ when 401
18
+ raise UnauthorizedError, "There was a problem with the access token: #{response.body}"
19
+ when 500
20
+ raise ServerError, "Mais server error: #{response.body}"
21
+ else
22
+ raise StandardError, "Unexpected response: #{response.status} #{response.body}"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class MaisOrcidClient
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
@@ -28,16 +28,20 @@ class MaisOrcidClient
28
28
  # @param client_secret [String] the client secret to authenticate with MAIS
29
29
  # @param base_url [String] the base URL for the API
30
30
  def configure(client_id:, client_secret:, base_url:)
31
- instance.base_url = base_url
32
- instance.client_id = client_id
33
- instance.client_secret = client_secret
31
+ instance.config = OpenStruct.new(
32
+ token: Authenticator.token(client_id, client_secret, base_url),
33
+ client_id:,
34
+ client_secret:,
35
+ base_url:
36
+ )
37
+
34
38
  self
35
39
  end
36
40
 
37
- delegate :fetch_orcid_users, :fetch_orcid_user, to: :instance
41
+ delegate :config, :fetch_orcid_users, :fetch_orcid_user, to: :instance
38
42
  end
39
43
 
40
- attr_accessor :base_url, :client_id, :client_secret
44
+ attr_accessor :config
41
45
 
42
46
  # @param [int] limit number of users requested
43
47
  # @param [int] page_size number of users per page
@@ -104,41 +108,32 @@ class MaisOrcidClient
104
108
  end
105
109
 
106
110
  def get_response(path, allow404: false)
107
- response = conn.get("/mais/orcid/v1#{path}")
111
+ TokenWrapper.refresh(config) do
112
+ response = conn.get("/mais/orcid/v1#{path}")
108
113
 
109
- return if allow404 && response.status == 404
114
+ return if allow404 && response.status == 404
110
115
 
111
- raise "UIT MAIS ORCID User API returned #{response.status}" if response.status != 200
116
+ return UnexpectedResponse.call(response) unless response.success?
112
117
 
113
- body = JSON.parse(response.body).with_indifferent_access
114
- raise "UIT MAIS ORCID User API returned an error: #{response.body}" if body.key?(:error)
118
+ body = JSON.parse(response.body).with_indifferent_access
119
+ raise UnexpectedResponse::ResponseError, "UIT MAIS ORCID User API returned an error: #{response.body}" if body.key?(:error)
115
120
 
116
- body
121
+ body
122
+ end
117
123
  end
118
124
 
119
- # @return [Faraday::Connection]
120
125
  def conn
121
- @conn ||= begin
122
- conn = Faraday.new(url: base_url) do |faraday|
123
- faraday.request :retry, max: 3,
124
- interval: 0.5,
125
- interval_randomness: 0.5,
126
- backoff_factor: 2
127
- end
128
- conn.options.timeout = 500
129
- conn.options.open_timeout = 10
130
- conn.headers[:user_agent] = "stanford-library-sul-pub"
131
- conn.headers[:authorization] = token
132
- conn
126
+ conn = Faraday.new(url: config.base_url) do |faraday|
127
+ faraday.request :retry, max: 3,
128
+ interval: 0.5,
129
+ interval_randomness: 0.5,
130
+ backoff_factor: 2
133
131
  end
134
- end
135
-
136
- def token
137
- client = OAuth2::Client.new(client_id, client_secret, site: base_url,
138
- token_url: "/api/oauth/token", authorize_url: "/api/oauth/authorize",
139
- auth_scheme: :request_body)
140
- token = client.client_credentials.get_token
141
- "Bearer #{token.token}"
132
+ conn.options.timeout = 500
133
+ conn.options.open_timeout = 10
134
+ conn.headers[:user_agent] = "stanford-library-sul-pub"
135
+ conn.headers[:authorization] = "Bearer #{config.token}"
136
+ conn
142
137
  end
143
138
 
144
139
  # @param [string] orcidid which can include a full URI, e.g. "https://sandbox.orcid.org/0000-0002-7262-6251"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mais_orcid_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Mangiafico
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-07-31 00:00:00.000000000 Z
12
+ date: 2023-08-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -217,6 +217,9 @@ files:
217
217
  - README.md
218
218
  - Rakefile
219
219
  - lib/mais_orcid_client.rb
220
+ - lib/mais_orcid_client/authenticator.rb
221
+ - lib/mais_orcid_client/token_wrapper.rb
222
+ - lib/mais_orcid_client/unexpected_response.rb
220
223
  - lib/mais_orcid_client/version.rb
221
224
  - mais_orcid_client.gemspec
222
225
  homepage: https://github.com/sul-dlss/mais_orcid_client
@@ -241,7 +244,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
241
244
  - !ruby/object:Gem::Version
242
245
  version: '0'
243
246
  requirements: []
244
- rubygems_version: 3.4.10
247
+ rubygems_version: 3.4.13
245
248
  signing_key:
246
249
  specification_version: 4
247
250
  summary: Interface for interacting with the MAIS's ORCID API.