domoscio_rails 0.3.5 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7cabe98aeb3d48cbe9e3221c798052ca22e9838641b0f0eb5c814651b56b86e
4
- data.tar.gz: add23b57f3520e3c63c943beb1e915b3ca75f82ff1a015048079342c2f9ba981
3
+ metadata.gz: 7089254584b320486a1c5791a61d4831e894a7d15865476edd927b0b6c26b10d
4
+ data.tar.gz: 24bad35c6b24c8a87da9d9f61f3d56258ce33d74f7f1a12c8fe40c3aa780eebf
5
5
  SHA512:
6
- metadata.gz: 641267c7319ab1653ceb738267fa8f4faa2705d13d19f0e139c95b6f862c33eaedb23c4d0ae47bde11dd37ba7dbf2a423d63976b61e699ae1fdca0855030e403
7
- data.tar.gz: 0db98990280a8ea3eb7d4ed485f02e5a0117127c03b8474220740d247ebd307e64161e1ead8200dc4235b22dc822e389d19c24326c0123dfa1aa4a813a443ae7
6
+ metadata.gz: bca18d4b3107390472ab74e91482b064c3f281da971066ac2c20f46771207b4062e3fe2de83fa2358ab3385700c8a4f41346ab71382782d58413958ac5fa78f2
7
+ data.tar.gz: 63d7d44e90b63f62715d213f636a17fc024aebcbcef001c27331fc03b40b8bf8ca46e8e14aeae93d7b0d1d02626030593d5f34987fb2209a2e90d871d887804a
@@ -38,11 +38,11 @@ module DomoscioRails
38
38
 
39
39
  # Refers to AdaptiveEngine Version
40
40
  def version
41
- @version || 2
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
- # Raises DomoscioRails::ResponseError if response code != 200.
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
- def self.request(method, url, params={}, filters={}, headers = request_headers, before_request_proc = nil)
73
- return false if @disabled
74
- # sets a default page size to 2k
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
- uri.query = URI.encode_www_form(filters) unless filters.empty?
78
- res = DomoscioRails.send_request(uri, method, params, headers, before_request_proc)
79
- return res if res.kind_of? DomoscioRails::ProcessingError
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, res, params)
82
- data = DomoscioRails::JSON.load(res.body.nil? ? '' : res.body)
83
- DomoscioRails::AuthorizationToken::Manager.storage.store({access_token: res['Accesstoken'], refresh_token: res['Refreshtoken']})
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, res.body, params)
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 res['Total'] && !filters[:page]
91
- pagetotal = (res['Total'].to_i / res['Per-Page'].to_f).ceil
92
+ if response['Total']
93
+ pagetotal = (response['Total'].to_i / response['Per-Page'].to_f).ceil
92
94
  for j in 2..pagetotal
93
- res = DomoscioRails.send_request(uri, method, params.merge({page: j}), headers, before_request_proc)
94
- return res if res.kind_of? DomoscioRails::ProcessingError
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, res, params)
97
- body = DomoscioRails::JSON.load(res.body.nil? ? '' : res.body)
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, res.body, params)
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
- def self.send_request(uri, method, params, headers, before_request_proc)
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
- res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
113
- req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
114
- req.body = DomoscioRails::JSON.dump(params)
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, res)
122
+ ProcessingError.new(uri, 500, exception, response)
120
123
  end
121
124
  end
122
125
 
123
- private
124
-
125
- def self.raise_http_failure(uri, res, params)
126
- unless res.kind_of? Net::HTTPSuccess
127
- if res.blank?
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((res.body.nil? ? '' : res.body), :symbolize_keys => true)
131
- raise ResponseError.new(uri, res.code.to_i, body, res.body, params)
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
- auth_token = DomoscioRails::AuthorizationToken::Manager.get_token
155
- if !auth_token.is_a? String
156
- headers = {
157
- 'user_agent' => "#{DomoscioRails.user_agent}",
158
- 'AccessToken' => "#{auth_token[:access_token]}",
159
- 'RefreshToken' => "#{auth_token[:refresh_token]}",
160
- 'Content-Type' => 'application/json'
161
- }
162
- else
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
- headers
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
- token = storage.get
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(temp_dir = nil)
35
- @temp_dir = temp_dir || DomoscioRails.configuration.temp_dir
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
- @temp_dir = DomoscioRails.configuration.temp_dir
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
@@ -1,3 +1,3 @@
1
1
  module DomoscioRails
2
- VERSION = "0.3.5"
2
+ VERSION = "0.3.6"
3
3
  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.5
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-02-11 00:00:00.000000000 Z
11
+ date: 2021-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails