rail-locator-api 0.0.22 → 0.0.25
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rail-locator-api/api_request.rb +10 -18
- data/lib/rail-locator-api/request.rb +9 -7
- data/lib/rail-locator-api/version.rb +1 -1
- data/lib/rail-locator-api.rb +15 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b25b34e0bbca2dc11a3a0e5c1582763bab4d1d010d7a35e5aeb6cc09122b07b
|
4
|
+
data.tar.gz: 5bd43dabc4467fe756210544a910290b4d65b59dbddbd15b33d36f2e9277d4b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b45549523425ac1dd977927991534507273156f0ab8d14d82e75f7e73a46156b408aec2864d0bbf4bc2a9de0ab6436cd8cfa766d52d7f1f8237439106e5216ce
|
7
|
+
data.tar.gz: 00aa0506a19cc0b1bd4dff0b5b9a11152cf068360cc469fd7829f69673959ba662b670435e92758633a899fff1a55624cad00b70d2da8de2add99935b04dae62
|
@@ -5,7 +5,7 @@ module RailLocatorApi
|
|
5
5
|
@request_builder = builder
|
6
6
|
end
|
7
7
|
|
8
|
-
def post(params: nil, headers: nil, suffix: nil, body: {}
|
8
|
+
def post(params: nil, headers: nil, suffix: nil, body: {})
|
9
9
|
validate_api_key
|
10
10
|
begin
|
11
11
|
response = self.rest_client(suffix).post do |request|
|
@@ -13,17 +13,11 @@ module RailLocatorApi
|
|
13
13
|
end
|
14
14
|
parse_response(response)
|
15
15
|
rescue => e
|
16
|
-
|
17
|
-
RailLocatorApi::Request.access_token = RailLocatorApi.generate_access_token.try(:dig, "access_token")
|
18
|
-
sleep(0.3.second)
|
19
|
-
self.post(params: params, headers: headers, suffix: suffix, body: body, first_time: false)
|
20
|
-
else
|
21
|
-
handle_error(e)
|
22
|
-
end
|
16
|
+
handle_error(e)
|
23
17
|
end
|
24
18
|
end
|
25
19
|
|
26
|
-
def get(params: nil, headers: nil, body: {}
|
20
|
+
def get(params: nil, headers: nil, body: {})
|
27
21
|
validate_api_key
|
28
22
|
|
29
23
|
begin
|
@@ -32,13 +26,7 @@ module RailLocatorApi
|
|
32
26
|
end
|
33
27
|
parse_response(response)
|
34
28
|
rescue => e
|
35
|
-
|
36
|
-
RailLocatorApi::Request.access_token = RailLocatorApi.generate_access_token.try(:dig, "access_token")
|
37
|
-
sleep(0.3.second)
|
38
|
-
self.get(params: params, headers: headers, first_time: false)
|
39
|
-
else
|
40
|
-
handle_error(e)
|
41
|
-
end
|
29
|
+
handle_error(e)
|
42
30
|
end
|
43
31
|
end
|
44
32
|
|
@@ -50,6 +38,10 @@ module RailLocatorApi
|
|
50
38
|
@request_builder.access_token
|
51
39
|
end
|
52
40
|
|
41
|
+
def refresh_token
|
42
|
+
@request_builder.refresh_token
|
43
|
+
end
|
44
|
+
|
53
45
|
def api_key
|
54
46
|
@request_builder.api_key
|
55
47
|
end
|
@@ -176,8 +168,8 @@ module RailLocatorApi
|
|
176
168
|
def validate_api_key
|
177
169
|
case self.api_auth_method
|
178
170
|
when :api_key
|
179
|
-
unless self.
|
180
|
-
raise RailLocatorApi::RailLocatorApiError, "You must set an
|
171
|
+
unless self.access_token && self.api_endpoint
|
172
|
+
raise RailLocatorApi::RailLocatorApiError, "You must set an access_token prior to making a call #{self.access_token} #{self.api_endpoint}"
|
181
173
|
end
|
182
174
|
when :base64
|
183
175
|
unless self.api_user_email && self.api_user_password
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module RailLocatorApi
|
2
2
|
class Request
|
3
|
-
attr_accessor :access_token, :api_key, :api_user_email, :api_user_password, :api_auth_method, :api_endpoint,
|
3
|
+
attr_accessor :access_token, :refresh_token, :api_key, :api_user_email, :api_user_password, :api_auth_method, :api_endpoint,
|
4
4
|
:timeout, :open_timeout, :proxy, :ssl_options, :faraday_adapter, :symbolize_keys, :debug, :logger, :test
|
5
5
|
|
6
6
|
AUTH_METHODS = [:keycloak, :api_key, :base64]
|
@@ -8,7 +8,7 @@ module RailLocatorApi
|
|
8
8
|
DEFAULT_TIMEOUT = 60
|
9
9
|
DEFAULT_OPEN_TIMEOUT = 60
|
10
10
|
|
11
|
-
def initialize(access_token: nil, api_key: nil, api_user_email: nil, api_user_password: nil,
|
11
|
+
def initialize(access_token: nil, refresh_token: nil, api_key: nil, api_user_email: nil, api_user_password: nil,
|
12
12
|
api_endpoint: nil, api_auth_method: nil, timeout: nil, open_timeout: nil, proxy: nil, ssl_options: nil,
|
13
13
|
faraday_adapter: nil, symbolize_keys: false, debug: false, logger: nil, test: false)
|
14
14
|
|
@@ -19,10 +19,12 @@ module RailLocatorApi
|
|
19
19
|
|
20
20
|
@access_token = access_token || self.class.access_token || RailLocatorApi.generate_access_token.try(:dig, "access_token")
|
21
21
|
@access_token = @access_token.strip if @access_token
|
22
|
-
@access_token = RailLocatorApi::generate_access_token if @access_token.nil?
|
22
|
+
@access_token = RailLocatorApi::generate_access_token.try(:dig, "access_token") if @access_token.nil?
|
23
|
+
@refresh_token = refresh_token || self.class.refresh_token
|
24
|
+
@refresh_token = @refresh_token.strip if @refresh_token
|
23
25
|
|
24
|
-
@api_user_email = api_user_email || RailLocatorApi::api_user_email || ENV['API_USER_EMAIL']
|
25
|
-
@api_user_password = api_user_password || RailLocatorApi::api_user_password || ENV['API_USER_PASSWORD']
|
26
|
+
@api_user_email = api_user_email || RailLocatorApi::api_user_email || ENV['API_USER_EMAIL'] || ""
|
27
|
+
@api_user_password = api_user_password || RailLocatorApi::api_user_password || ENV['API_USER_PASSWORD'] || ""
|
26
28
|
@api_endpoint = api_endpoint || self.class.api_endpoint
|
27
29
|
@api_endpoint = RailLocatorApi::api_endpoint if @api_endpoint.nil?
|
28
30
|
@api_auth_method = api_auth_method || DEFAULT_AUTH_METHOD
|
@@ -95,8 +97,8 @@ module RailLocatorApi
|
|
95
97
|
:api_endpoint, :proxy, :ssl_options, :faraday_adapter, :symbolize_keys, :debug, :logger, :test
|
96
98
|
|
97
99
|
def method_missing(sym, *args, &block)
|
98
|
-
new(access_token: self.access_token,
|
99
|
-
api_user_password: self.api_user_email,
|
100
|
+
new(access_token: self.access_token, refresh_token: self.refresh_token, api_key: self.api_key,
|
101
|
+
api_user_email: self.api_user_email, api_user_password: self.api_user_email,
|
100
102
|
api_auth_method: self.api_auth_method, api_endpoint: self.api_endpoint,
|
101
103
|
timeout: self.timeout, open_timeout: self.open_timeout, faraday_adapter: self.faraday_adapter,
|
102
104
|
symbolize_keys: self.symbolize_keys, debug: self.debug, proxy: self.proxy, ssl_options: self.ssl_options, logger: self.logger,
|
data/lib/rail-locator-api.rb
CHANGED
@@ -14,7 +14,21 @@ module RailLocatorApi
|
|
14
14
|
else
|
15
15
|
payload = "grant_type=#{RailLocatorApi.keycloak_grant_type}&client_id=#{RailLocatorApi.keycloak_client_id}&code=#{RailLocatorApi::Request.api_key}&redirect_uri=#{RailLocatorApi.keycloak_redirect_uri}"
|
16
16
|
end
|
17
|
+
connection = Faraday.new(RailLocatorApi.keycloak_token_url, proxy: nil,
|
18
|
+
ssl: { version: "TLSv1_2" }) do |faraday|
|
19
|
+
faraday.adapter Faraday.default_adapter
|
20
|
+
faraday.response :logger, ::Logger.new(STDOUT), bodies: true
|
21
|
+
end
|
22
|
+
|
23
|
+
response = connection.post RailLocatorApi.keycloak_token_url, payload do |request|
|
24
|
+
request.headers['accept'] = 'application/json'
|
25
|
+
end
|
26
|
+
|
27
|
+
JSON.parse(response.body)
|
28
|
+
end
|
17
29
|
|
30
|
+
def refresh_access_token
|
31
|
+
payload = "grant_type=refresh_token&client_id=#{RailLocatorApi.keycloak_client_id}&refresh_token=#{RailLocatorApi::Request.access_token}&redirect_uri=#{RailLocatorApi.keycloak_redirect_uri}"
|
18
32
|
connection = Faraday.new(RailLocatorApi.keycloak_token_url, proxy: nil,
|
19
33
|
ssl: { version: "TLSv1_2" }) do |faraday|
|
20
34
|
faraday.adapter Faraday.default_adapter
|
@@ -22,11 +36,10 @@ module RailLocatorApi
|
|
22
36
|
end
|
23
37
|
|
24
38
|
response = connection.post RailLocatorApi.keycloak_token_url, payload do |request|
|
25
|
-
request.headers['Content-Type'] = 'application/json'
|
26
39
|
request.headers['accept'] = 'application/json'
|
27
40
|
end
|
28
41
|
|
29
|
-
JSON.parse(response.body)
|
42
|
+
JSON.parse(response.body)
|
30
43
|
end
|
31
44
|
|
32
45
|
def setup
|