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 +4 -4
- data/Gemfile.lock +7 -7
- data/lib/mais_orcid_client/authenticator.rb +26 -0
- data/lib/mais_orcid_client/token_wrapper.rb +13 -0
- data/lib/mais_orcid_client/unexpected_response.rb +26 -0
- data/lib/mais_orcid_client/version.rb +1 -1
- data/lib/mais_orcid_client.rb +27 -32
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b93e75dc3042635205b947c21d9697808b9bdefa9d9dae8598e03db1eac3e128
|
4
|
+
data.tar.gz: d714d5c0985596191dba26e801a43455dd926b307e5a88753c66e5a8550d918d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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
|
data/lib/mais_orcid_client.rb
CHANGED
@@ -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.
|
32
|
-
|
33
|
-
|
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 :
|
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
|
-
|
111
|
+
TokenWrapper.refresh(config) do
|
112
|
+
response = conn.get("/mais/orcid/v1#{path}")
|
108
113
|
|
109
|
-
|
114
|
+
return if allow404 && response.status == 404
|
110
115
|
|
111
|
-
|
116
|
+
return UnexpectedResponse.call(response) unless response.success?
|
112
117
|
|
113
|
-
|
114
|
-
|
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
|
-
|
121
|
+
body
|
122
|
+
end
|
117
123
|
end
|
118
124
|
|
119
|
-
# @return [Faraday::Connection]
|
120
125
|
def conn
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
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
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
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.
|
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-
|
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.
|
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.
|