domoscio_rails 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/domoscio_rails.rb +92 -53
- data/lib/domoscio_rails/authorization_token.rb +4 -11
- data/lib/domoscio_rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7089254584b320486a1c5791a61d4831e894a7d15865476edd927b0b6c26b10d
|
4
|
+
data.tar.gz: 24bad35c6b24c8a87da9d9f61f3d56258ce33d74f7f1a12c8fe40c3aa780eebf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bca18d4b3107390472ab74e91482b064c3f281da971066ac2c20f46771207b4062e3fe2de83fa2358ab3385700c8a4f41346ab71382782d58413958ac5fa78f2
|
7
|
+
data.tar.gz: 63d7d44e90b63f62715d213f636a17fc024aebcbcef001c27331fc03b40b8bf8ca46e8e14aeae93d7b0d1d02626030593d5f34987fb2209a2e90d871d887804a
|
data/lib/domoscio_rails.rb
CHANGED
@@ -38,11 +38,11 @@ module DomoscioRails
|
|
38
38
|
|
39
39
|
# Refers to AdaptiveEngine Version
|
40
40
|
def version
|
41
|
-
@version
|
41
|
+
@version ||= 2
|
42
42
|
end
|
43
43
|
|
44
44
|
def root_url
|
45
|
-
@root_url
|
45
|
+
@root_url ||= ""
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -63,42 +63,44 @@ module DomoscioRails
|
|
63
63
|
# - +method+: HTTP method; lowercase symbol, e.g. :get, :post etc.
|
64
64
|
# - +url+: the part after Configuration#root_url
|
65
65
|
# - +params+: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body
|
66
|
-
# - +filters+: hash; pagination params etc.; will encode it by URI and assign to URI#query
|
67
|
-
# - +headers+: hash; request_headers by default
|
68
|
-
# - +before_request_proc+: optional proc; will call it passing the Net::HTTPRequest instance just before Net::HTTPRequest#request
|
69
66
|
#
|
70
|
-
#
|
67
|
+
# Performs HTTP requests to Adaptive Engine
|
68
|
+
# On token issues, will try once to get a new token then will output a DomoscioRails::ReponseError with details
|
71
69
|
#
|
72
|
-
|
73
|
-
|
74
|
-
|
70
|
+
# Raises DomoscioRails::ResponseError on Adaptive Error Status
|
71
|
+
# Raises DomoscioRails::ProcessingError on Internal Error
|
72
|
+
#
|
73
|
+
def self.request(method, url, params={})
|
74
|
+
|
75
|
+
store_tokens, headers = request_headers
|
75
76
|
params.merge!({'per_page': 2000}) unless params[:per_page]
|
76
77
|
uri = api_uri(url)
|
77
|
-
|
78
|
-
|
79
|
-
return
|
78
|
+
|
79
|
+
response = DomoscioRails.send_request(uri, method, params, headers)
|
80
|
+
return response if response.kind_of? DomoscioRails::ProcessingError
|
81
|
+
|
80
82
|
begin
|
81
|
-
raise_http_failure(uri,
|
82
|
-
data = DomoscioRails::JSON.load(
|
83
|
-
DomoscioRails::AuthorizationToken::Manager.storage.store({access_token:
|
83
|
+
raise_http_failure(uri, response, params)
|
84
|
+
data = DomoscioRails::JSON.load(response.body.nil? ? '' : response.body)
|
85
|
+
DomoscioRails::AuthorizationToken::Manager.storage.store({access_token: response['Accesstoken'], refresh_token: response['Refreshtoken']}) if store_tokens
|
84
86
|
rescue MultiJson::LoadError => exception
|
85
|
-
data = ProcessingError.new(uri, 500, exception,
|
87
|
+
data = ProcessingError.new(uri, 500, exception, response.body, params)
|
86
88
|
rescue ResponseError => exception
|
87
89
|
data = exception
|
88
90
|
end
|
89
91
|
|
90
|
-
if
|
91
|
-
pagetotal = (
|
92
|
+
if response['Total']
|
93
|
+
pagetotal = (response['Total'].to_i / response['Per-Page'].to_f).ceil
|
92
94
|
for j in 2..pagetotal
|
93
|
-
|
94
|
-
return
|
95
|
+
response = DomoscioRails.send_request(uri, method, params.merge({page: j}), headers)
|
96
|
+
return response if response.kind_of? DomoscioRails::ProcessingError
|
95
97
|
begin
|
96
|
-
raise_http_failure(uri,
|
97
|
-
body = DomoscioRails::JSON.load(
|
98
|
+
raise_http_failure(uri, response, params)
|
99
|
+
body = DomoscioRails::JSON.load(response.body.nil? ? '' : response.body)
|
98
100
|
data += body
|
99
101
|
data.flatten!
|
100
102
|
rescue MultiJson::LoadError => exception
|
101
|
-
return ProcessingError.new(uri, 500, exception,
|
103
|
+
return ProcessingError.new(uri, 500, exception, response.body, params)
|
102
104
|
rescue ResponseError => exception
|
103
105
|
return exception
|
104
106
|
end
|
@@ -107,32 +109,52 @@ module DomoscioRails
|
|
107
109
|
data
|
108
110
|
end
|
109
111
|
|
110
|
-
|
112
|
+
private
|
113
|
+
|
114
|
+
# This function catches usual Http errors during calls
|
115
|
+
#
|
116
|
+
def self.send_request(uri, method, params, headers)
|
111
117
|
begin
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
before_request_proc.call(req) if before_request_proc
|
116
|
-
http.request req
|
117
|
-
end
|
118
|
+
response = perform_call(uri, method, params, headers)
|
119
|
+
response = retry_call_and_store_tokens(uri, method, params, headers) if ['401','403'].include? response.code
|
120
|
+
response
|
118
121
|
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => exception
|
119
|
-
ProcessingError.new(uri, 500, exception,
|
122
|
+
ProcessingError.new(uri, 500, exception, response)
|
120
123
|
end
|
121
124
|
end
|
122
125
|
|
123
|
-
|
124
|
-
|
125
|
-
def self.raise_http_failure(uri,
|
126
|
-
unless
|
127
|
-
if
|
126
|
+
# This helper will check the response status and build the correcponding DomoscioRails::ResponseError
|
127
|
+
#
|
128
|
+
def self.raise_http_failure(uri, response, params)
|
129
|
+
unless response.kind_of? Net::HTTPSuccess
|
130
|
+
if response.blank?
|
128
131
|
raise ResponseError.new(uri, 500, {error: {status: 500, message: 'AdaptiveEngine not available'}}, {}, params)
|
129
132
|
else
|
130
|
-
body = DomoscioRails::JSON.load((
|
131
|
-
raise ResponseError.new(uri,
|
133
|
+
body = DomoscioRails::JSON.load((response.body.nil? ? '' : response.body), :symbolize_keys => true)
|
134
|
+
raise ResponseError.new(uri, response.code.to_i, body, response.body, params)
|
132
135
|
end
|
133
136
|
end
|
134
137
|
end
|
135
138
|
|
139
|
+
# Actual HTTP call is performed here
|
140
|
+
#
|
141
|
+
def self.perform_call(uri, method, params, headers)
|
142
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
143
|
+
req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
|
144
|
+
req.body = DomoscioRails::JSON.dump(params)
|
145
|
+
http.request req
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# This method is called when AdaptiveEngine returns tokens errors
|
150
|
+
# Action on those errors is to retry and request new tokens, those new token are then stored
|
151
|
+
def self.retry_call_and_store_tokens(uri, method, params, headers)
|
152
|
+
headers = request_new_tokens
|
153
|
+
response = perform_call(uri, method, params, headers)
|
154
|
+
DomoscioRails::AuthorizationToken::Manager.storage.store({access_token: response['Accesstoken'], refresh_token: response['Refreshtoken']})
|
155
|
+
response
|
156
|
+
end
|
157
|
+
|
136
158
|
def self.user_agent
|
137
159
|
@uname ||= get_uname
|
138
160
|
{
|
@@ -150,22 +172,39 @@ module DomoscioRails
|
|
150
172
|
'uname lookup failed'
|
151
173
|
end
|
152
174
|
|
175
|
+
# Process the token loading and analyze
|
176
|
+
# will return the processed headers and a token store flag
|
177
|
+
#
|
153
178
|
def self.request_headers
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
headers = {
|
164
|
-
'user_agent' => "#{DomoscioRails.user_agent}",
|
165
|
-
'Authorization' => "Token token=#{DomoscioRails.configuration.client_passphrase}",
|
166
|
-
'Content-Type' => 'application/json'
|
167
|
-
}
|
179
|
+
begin
|
180
|
+
auth_token = DomoscioRails::AuthorizationToken::Manager.get_token
|
181
|
+
if auth_token && auth_token[:access_token] && auth_token[:refresh_token]
|
182
|
+
[false, send_current_tokens(auth_token)]
|
183
|
+
else
|
184
|
+
[true, request_new_tokens]
|
185
|
+
end
|
186
|
+
rescue SyntaxError, StandardError
|
187
|
+
[true, request_new_tokens]
|
168
188
|
end
|
169
|
-
|
189
|
+
end
|
190
|
+
|
191
|
+
# If stored token successfully loaded we build the header with them
|
192
|
+
#
|
193
|
+
def self.send_current_tokens(auth_token)
|
194
|
+
{
|
195
|
+
'user_agent' => "#{DomoscioRails.user_agent}",
|
196
|
+
'AccessToken' => "#{auth_token[:access_token]}",
|
197
|
+
'RefreshToken' => "#{auth_token[:refresh_token]}",
|
198
|
+
'Content-Type' => 'application/json'
|
199
|
+
}
|
200
|
+
end
|
201
|
+
|
202
|
+
# If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
|
203
|
+
def self.request_new_tokens
|
204
|
+
{
|
205
|
+
'user_agent' => "#{DomoscioRails.user_agent}",
|
206
|
+
'Authorization' => "Token token=#{DomoscioRails.configuration.client_passphrase}",
|
207
|
+
'Content-Type' => 'application/json'
|
208
|
+
}
|
170
209
|
end
|
171
210
|
end
|
@@ -11,9 +11,7 @@ module DomoscioRails
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def get_token
|
14
|
-
|
15
|
-
token = DomoscioRails.configuration.client_passphrase if token.nil?
|
16
|
-
token
|
14
|
+
storage.get
|
17
15
|
end
|
18
16
|
end
|
19
17
|
end
|
@@ -29,13 +27,9 @@ module DomoscioRails
|
|
29
27
|
|
30
28
|
class FileStorage
|
31
29
|
require 'yaml'
|
32
|
-
@temp_dir
|
33
30
|
|
34
|
-
def initialize
|
35
|
-
|
36
|
-
if !@temp_dir
|
37
|
-
raise "Path to temporary folder is not defined"
|
38
|
-
end
|
31
|
+
def initialize
|
32
|
+
raise "Path to temporary folder is not defined" unless DomoscioRails.configuration.temp_dir
|
39
33
|
end
|
40
34
|
|
41
35
|
def get
|
@@ -60,8 +54,7 @@ module DomoscioRails
|
|
60
54
|
end
|
61
55
|
|
62
56
|
def file_path
|
63
|
-
|
64
|
-
File.join(@temp_dir, "DomoscioRails.AuthorizationToken.FileStore.tmp")
|
57
|
+
File.join(DomoscioRails.configuration.temp_dir, "DomoscioRails.AuthorizationToken.FileStore.tmp")
|
65
58
|
end
|
66
59
|
end
|
67
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: domoscio_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benoit Praly
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|