rail-locator-api 0.0.23 → 0.0.26

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: df7a3f52cdad808bec0023a5b18ef9ecc103d96e6f8c10d63ebf987a62c77269
4
- data.tar.gz: 56068b6c8098816850a2aa6e1432960968fd1da771633868ce6ab698b7b5162b
3
+ metadata.gz: c00795da8742e8613a6fb433a73d18a40642ed9376aaa4994ab49f000ee8cd41
4
+ data.tar.gz: 5c3b5cd9930eea15966513164d8a08e9d3bc1b14ec74315ab16cdc5d778e44c1
5
5
  SHA512:
6
- metadata.gz: 2fb4c5bf534755f7f0c0c74f0c2dac9eaff3154dfdd796b59256e178c3c3b0739e9f527097fe309f2e2dab5d0748f5d5550a9622ff6ceb17b213058fab18cd0b
7
- data.tar.gz: 7e8952a547389078beaae8e39c7c19e378b4c3f343c5c73554300d63db1227ae15a0592439737b0637b54614152688abf672623af0fbfa8769558bbc0dfeb33f
6
+ metadata.gz: 613e4325d9e5f0db04e796d706a6c94ec26f73c1bc20606faca0f22782adc533a347f7d09face9099c6bc663142b0db615c7b9d9d0ff54b48234ea6ce58286e3
7
+ data.tar.gz: fd0d792d5c0424d7387824222c70058dfb6892fd6f5687e5f0307a224fe952c465bfa3d844d3caefa02090b060403548954c736b3b8c038df597ca051c43b22b
@@ -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: {}, first_time: true)
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
- if [401, 404].include?(e.try(:response).try(:dig, :status)) && first_time
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: {}, first_time: true)
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
- if [401, 404].include?(e.try(:response).try(:dig, :status)) && first_time
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.api_key && self.api_endpoint
180
- raise RailLocatorApi::RailLocatorApiError, "You must set an api_key prior to making a call #{self.api_key} #{self.api_endpoint}"
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
@@ -91,12 +93,12 @@ module RailLocatorApi
91
93
  end
92
94
 
93
95
  class << self
94
- attr_accessor :access_token, :api_key, :api_user_email, :api_user_password, :api_auth_method, :timeout, :open_timeout,
96
+ attr_accessor :access_token, :refresh_token, :api_key, :api_user_email, :api_user_password, :api_auth_method, :timeout, :open_timeout,
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, api_key: self.api_key, api_user_email: self.api_user_email,
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,
@@ -1,3 +1,3 @@
1
1
  module RailLocatorApi
2
- VERSION = "0.0.23"
2
+ VERSION = "0.0.26"
3
3
  end
@@ -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
@@ -25,7 +39,7 @@ module RailLocatorApi
25
39
  request.headers['accept'] = 'application/json'
26
40
  end
27
41
 
28
- JSON.parse(response.body).try(:dig, "access_token")
42
+ JSON.parse(response.body)
29
43
  end
30
44
 
31
45
  def setup
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rail-locator-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.23
4
+ version: 0.0.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Osetrov