domoscio_viz 0.2.4 → 0.2.5

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: 887c55dd8caa52f79ff7cdb18b53f7ed134b538f0d3783d5f4826bbe99efe0db
4
- data.tar.gz: 6229db276d697d67b482d5c6bedf8bf0cd5a236ce3336f73f5187ab4d995af85
3
+ metadata.gz: 86781580f4a835cfa5ac44fd6bc32933955f484fecbef42222957ea05239bb2e
4
+ data.tar.gz: 58776787e717e2f6d6527c995a8d1b1865d5d4bc78a6067476b1dc049a9a39b5
5
5
  SHA512:
6
- metadata.gz: 665710baedb373e38e2cfb5a821f95ce52d5ba80d16de6d56127a8cc90ad1b80cbd9391ae45a377f679f6316bff32623f99e5c1598c1bd4122207d6dae616c4e
7
- data.tar.gz: e02a53ce48feffc03b70377f129339dd3e4c7ad43f0b776be02baa67d9d8741ec9fc2544ba19e762d36576f8d2bdd1f2a7eb2a433bb9d69cbfce5fb704ffae79
6
+ metadata.gz: 4dbb5722a87cb87d27c40809144a88235c87f4ecae0923ba3b7d02274397ce15902fea2e58a09c40ca72603684b57c57c6ff1745b2267dec6b56eae1230cb17a
7
+ data.tar.gz: 801a9125a3a74fc97c56cb83b01a2d531c5837e928469cdf120ac05d91e056f616725ede57265f73ffd939ffd001bac21e39f0281b6135e1134db766ef4d93e0
data/lib/domoscio_viz.rb CHANGED
@@ -17,28 +17,10 @@ require 'domoscio_viz/chart/chart'
17
17
  module DomoscioViz
18
18
 
19
19
  class Configuration
20
- attr_accessor :preproduction, :test, :root_url,
21
- :client_id, :client_passphrase,
22
- :temp_dir
23
-
24
- def preproduction
25
- @preproduction || false
26
- end
27
-
28
- def test
29
- @test || false
30
- end
20
+ attr_accessor :root_url, :client_id, :client_passphrase, :temp_dir
31
21
 
32
22
  def root_url
33
- if @preproduction == true
34
- if @test == true
35
- @root_url || "https://domoscio-viz-engine-v2-preprod.azurewebsites.net"
36
- else
37
- @root_url || "https://visualization-engine.domoscio.com"
38
- end
39
- else
40
- @root_url || "http://localhost:3002"
41
- end
23
+ @root_url ||= ""
42
24
  end
43
25
  end
44
26
 
@@ -55,50 +37,85 @@ module DomoscioViz
55
37
  URI(configuration.root_url + url)
56
38
  end
57
39
 
58
- def self.request(method, url, params={}, filters={}, headers = request_headers, before_request_proc = nil)
59
- return false if @disabled
40
+ #
41
+ # - +method+: HTTP method; lowercase symbol, e.g. :get, :post etc.
42
+ # - +url+: the part after Configuration#root_url
43
+ # - +params+: hash; entity data for creation, update etc.; will dump it by JSON and assign to Net::HTTPRequest#body
44
+ #
45
+ # Performs HTTP requests to Adaptive Engine
46
+ # On token issues, will try once to get a new token then will output a DomoscioViz::ReponseError with details
47
+ #
48
+ # Raises DomoscioViz::ResponseError on Adaptive Error Status
49
+ # Raises DomoscioViz::ProcessingError on Internal Error
50
+ #
51
+ def self.request(method, url, params={})
52
+
53
+ store_tokens, headers = request_headers
54
+ params.merge!({'per_page': 2000}) unless params[:per_page]
60
55
  uri = api_uri(url)
61
- uri.query = URI.encode_www_form(filters) unless filters.empty?
62
- res = DomoscioViz.send_request(uri, method, params, headers, before_request_proc)
63
- return res if res.kind_of? DomoscioViz::ProcessingError
56
+
57
+ response = DomoscioViz.send_request(uri, method, params, headers)
58
+ return response if response.kind_of? DomoscioViz::ProcessingError
59
+
64
60
  begin
65
- raise_http_failure(uri, res, params)
66
- data = DomoscioViz::JSON.load(res.body.nil? ? '' : res.body)
67
- DomoscioViz::AuthorizationToken::Manager.storage.store({access_token: res['Accesstoken'], refresh_token: res['Refreshtoken']})
61
+ raise_http_failure(uri, response, params)
62
+ data = DomoscioViz::JSON.load(response.body.nil? ? '' : response.body)
63
+ DomoscioViz::AuthorizationToken::Manager.storage.store({access_token: response['Accesstoken'], refresh_token: response['Refreshtoken']}) if store_tokens
68
64
  rescue MultiJson::LoadError => exception
69
- return ProcessingError.new(uri, 500, exception, res.body, params)
65
+ data = ProcessingError.new(uri, 500, exception, response.body, params)
70
66
  rescue ResponseError => exception
71
- return exception
67
+ data = exception
72
68
  end
69
+
73
70
  data
74
71
  end
75
72
 
76
- def self.send_request(uri, method, params, headers, before_request_proc)
73
+ private
74
+
75
+ # This function catches usual Http errors during calls
76
+ #
77
+ def self.send_request(uri, method, params, headers)
77
78
  begin
78
- res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
79
- req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
80
- req.body = DomoscioRails::JSON.dump(params)
81
- before_request_proc.call(req) if before_request_proc
82
- http.request req
83
- end
79
+ response = perform_call(uri, method, params, headers)
80
+ response = retry_call_and_store_tokens(uri, method, params, headers) if ['401','403'].include? response.code
81
+ response
84
82
  rescue Timeout::Error, Errno::EINVAL, HTTP::ConnectionError, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => exception
85
- ProcessingError.new(uri, 500, exception, res, params)
83
+ ProcessingError.new(uri, 500, exception, response, params)
86
84
  end
87
85
  end
88
86
 
89
- private
90
-
91
- def self.raise_http_failure(uri, res, params)
92
- unless res.kind_of? Net::HTTPSuccess
93
- if res.blank?
87
+ # This helper will check the response status and build the correcponding DomoscioRails::ResponseError
88
+ #
89
+ def self.raise_http_failure(uri, response, params)
90
+ unless response.kind_of? Net::HTTPSuccess
91
+ if response.blank?
94
92
  raise ResponseError.new(uri, 500, {error: {status: 500, message: 'VisualizationEngine not available'}}, {}, params)
95
93
  else
96
- body = DomoscioRails::JSON.load((res.body.nil? ? '' : res.body), :symbolize_keys => true)
97
- raise ResponseError.new(uri, res.code.to_i, body, res.body, params)
94
+ body = DomoscioViz::JSON.load((response.body.nil? ? '' : response.body), :symbolize_keys => true)
95
+ raise ResponseError.new(uri, response.code.to_i, body, response.body, params)
98
96
  end
99
97
  end
100
98
  end
101
99
 
100
+ # Actual HTTP call is performed here
101
+ #
102
+ def self.perform_call(uri, method, params, headers)
103
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
104
+ req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
105
+ req.body = DomoscioRails::JSON.dump(params)
106
+ http.request req
107
+ end
108
+ end
109
+
110
+ # This method is called when AdaptiveEngine returns tokens errors
111
+ # Action on those errors is to retry and request new tokens, those new token are then stored
112
+ def self.retry_call_and_store_tokens(uri, method, params, headers)
113
+ headers = request_new_tokens
114
+ response = perform_call(uri, method, params, headers)
115
+ DomoscioViz::AuthorizationToken::Manager.storage.store({access_token: response['Accesstoken'], refresh_token: response['Refreshtoken']})
116
+ response
117
+ end
118
+
102
119
  def self.user_agent
103
120
  @uname ||= get_uname
104
121
  {
@@ -116,29 +133,45 @@ module DomoscioViz
116
133
  'uname lookup failed'
117
134
  end
118
135
 
136
+ # Process the token loading and analyze
137
+ # will return the processed headers and a token store flag
138
+ #
119
139
  def self.request_headers
120
- auth_token = DomoscioViz::AuthorizationToken::Manager.get_token
121
- if !auth_token.is_a? String
122
- headers = {
123
- 'user_agent' => "#{DomoscioViz.user_agent}",
124
- 'ClientId' => "#{DomoscioViz.configuration.client_id}",
125
- 'AccessToken' => "#{auth_token[:access_token]}",
126
- 'RefreshToken' => "#{auth_token[:refresh_token]}",
127
- 'Content-Type' => 'application/json'
128
- }
129
- else
130
- headers = {
131
- 'user_agent' => "#{DomoscioViz.user_agent}",
132
- 'ClientId' => "#{DomoscioViz.configuration.client_id}",
133
- 'Authorization' => "Token token=#{DomoscioViz.configuration.client_passphrase}",
134
- 'Content-Type' => 'application/json'
135
- }
140
+ begin
141
+ auth_token = DomoscioViz::AuthorizationToken::Manager.get_token
142
+ if auth_token && auth_token[:access_token] && auth_token[:refresh_token]
143
+ [false, send_current_tokens(auth_token)]
144
+ else
145
+ [true, request_new_tokens]
146
+ end
147
+ rescue SyntaxError, StandardError
148
+ [true, request_new_tokens]
136
149
  end
137
- headers
150
+ end
151
+
152
+ # If stored token successfully loaded we build the header with them
153
+ #
154
+ def self.send_current_tokens(auth_token)
155
+ {
156
+ 'user_agent' => "#{DomoscioViz.user_agent}",
157
+ 'ClientId' => "#{DomoscioViz.configuration.client_id}",
158
+ 'AccessToken' => "#{auth_token[:access_token]}",
159
+ 'RefreshToken' => "#{auth_token[:refresh_token]}",
160
+ 'Content-Type' => 'application/json'
161
+ }
162
+ end
163
+
164
+ # If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
165
+ def self.request_new_tokens
166
+ {
167
+ 'user_agent' => "#{DomoscioViz.user_agent}",
168
+ 'ClientId' => "#{DomoscioViz.configuration.client_id}",
169
+ 'Authorization' => "Token token=#{DomoscioViz.configuration.client_passphrase}",
170
+ 'Content-Type' => 'application/json'
171
+ }
138
172
  end
139
173
 
140
174
  DomoscioViz.configure do |c|
141
- c.preproduction = false
142
175
  c.client_id = nil
143
176
  c.client_passphrase = nil
144
177
  c.temp_dir = File.expand_path('../tmp', __FILE__)
@@ -12,9 +12,7 @@ module DomoscioViz
12
12
  end
13
13
 
14
14
  def get_token
15
- token = storage.get
16
- token = DomoscioViz.configuration.client_passphrase if token.nil?
17
- token
15
+ storage.get
18
16
  end
19
17
  end
20
18
  end
@@ -35,9 +33,7 @@ module DomoscioViz
35
33
 
36
34
  def initialize(temp_dir = nil)
37
35
  @temp_dir = temp_dir || DomoscioViz.configuration.temp_dir
38
- if !@temp_dir
39
- raise "Path to temporary folder is not defined"
40
- end
36
+ raise "Path to temporary folder is not defined" unless @temp_dir
41
37
  end
42
38
 
43
39
  def get
@@ -1,9 +1,4 @@
1
1
  DomoscioViz.configure do |c|
2
- if Rails.env == "production"
3
- c.preproduction = true
4
- else
5
- c.preproduction = false
6
- end
7
2
  c.client_id = ENV['DOMOSCIO_ID']
8
3
  c.client_passphrase = ENV['DOMOSCIO_PASSWORD']
9
4
  c.temp_dir = File.expand_path('../tmp', __FILE__)
@@ -1,3 +1,3 @@
1
1
  module DomoscioViz
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domoscio_viz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
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-01-18 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
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  - !ruby/object:Gem::Version
64
64
  version: '0'
65
65
  requirements: []
66
- rubygems_version: 3.1.2
66
+ rubygems_version: 3.0.8
67
67
  signing_key:
68
68
  specification_version: 4
69
69
  summary: Summary of DomoscioViz.