domoscio_viz 0.2.4 → 0.2.5a

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: c21fbe0307ea47d6608df642b9a72d6a684d707002aad710369a1deeb6e8b150
4
+ data.tar.gz: 2e8c0a44e2c64fb0bec2d7b3e38ed8feccd52183e34c17f0100db24d8a23253c
5
5
  SHA512:
6
- metadata.gz: 665710baedb373e38e2cfb5a821f95ce52d5ba80d16de6d56127a8cc90ad1b80cbd9391ae45a377f679f6316bff32623f99e5c1598c1bd4122207d6dae616c4e
7
- data.tar.gz: e02a53ce48feffc03b70377f129339dd3e4c7ad43f0b776be02baa67d9d8741ec9fc2544ba19e762d36576f8d2bdd1f2a7eb2a433bb9d69cbfce5fb704ffae79
6
+ metadata.gz: 31c4a07d614f7228da16b172b166df19bb46559658e3d4b2e1facd74952ff5f610ac536eb411f1b8723104c6628bdf17f99af2966c4dc7ba63f8734e2a14cb75
7
+ data.tar.gz: ea7ee09cad87f5c02af87783729b71e01c9bcb46ac45666902b89b360a1cf1806ccafccf5651a84e1864f5a40dd9f3ab8d34c072ea754d9e1018af4d25919ccd
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,83 @@ 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
60
54
  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
55
+
56
+ response = DomoscioViz.send_request(uri, method, params, headers)
57
+ return response if response.kind_of? DomoscioViz::ProcessingError
58
+
64
59
  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']})
60
+ raise_http_failure(uri, response, params)
61
+ data = DomoscioViz::JSON.load(response.body.nil? ? '' : response.body)
62
+ DomoscioViz::AuthorizationToken::Manager.storage.store({access_token: response['Accesstoken'], refresh_token: response['Refreshtoken']}) if store_tokens
68
63
  rescue MultiJson::LoadError => exception
69
- return ProcessingError.new(uri, 500, exception, res.body, params)
64
+ data = ProcessingError.new(uri, 500, exception, response.body, params)
70
65
  rescue ResponseError => exception
71
- return exception
66
+ data = exception
72
67
  end
68
+
73
69
  data
74
70
  end
75
71
 
76
- def self.send_request(uri, method, params, headers, before_request_proc)
72
+ private
73
+
74
+ # This function catches usual Http errors during calls
75
+ #
76
+ def self.send_request(uri, method, params, headers)
77
77
  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
78
+ response = perform_call(uri, method, params, headers)
79
+ response = retry_call_and_store_tokens(uri, method, params, headers) if ['401','403'].include? response.code
80
+ response
84
81
  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)
82
+ ProcessingError.new(uri, 500, exception, response, params)
86
83
  end
87
84
  end
88
85
 
89
- private
90
-
91
- def self.raise_http_failure(uri, res, params)
92
- unless res.kind_of? Net::HTTPSuccess
93
- if res.blank?
86
+ # This helper will check the response status and build the correcponding DomoscioRails::ResponseError
87
+ #
88
+ def self.raise_http_failure(uri, response, params)
89
+ unless response.kind_of? Net::HTTPSuccess
90
+ if response.blank?
94
91
  raise ResponseError.new(uri, 500, {error: {status: 500, message: 'VisualizationEngine not available'}}, {}, params)
95
92
  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)
93
+ raise ResponseError.new(uri, response.code.to_i, DomoscioViz::JSON.load((response.body.nil? ? '' : response.body), :symbolize_keys => true), response.body, params)
98
94
  end
99
95
  end
100
96
  end
101
97
 
98
+ # Actual HTTP call is performed here
99
+ #
100
+ def self.perform_call(uri, method, params, headers)
101
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
102
+ req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
103
+ req.body = DomoscioRails::JSON.dump(params)
104
+ http.request req
105
+ end
106
+ end
107
+
108
+ # This method is called when AdaptiveEngine returns tokens errors
109
+ # Action on those errors is to retry and request new tokens, those new token are then stored
110
+ def self.retry_call_and_store_tokens(uri, method, params, headers)
111
+ headers = request_new_tokens
112
+ response = perform_call(uri, method, params, headers)
113
+ DomoscioViz::AuthorizationToken::Manager.storage.store({access_token: response['Accesstoken'], refresh_token: response['Refreshtoken']})
114
+ response
115
+ end
116
+
102
117
  def self.user_agent
103
118
  @uname ||= get_uname
104
119
  {
@@ -116,29 +131,45 @@ module DomoscioViz
116
131
  'uname lookup failed'
117
132
  end
118
133
 
134
+ # Process the token loading and analyze
135
+ # will return the processed headers and a token store flag
136
+ #
119
137
  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
- }
138
+ begin
139
+ auth_token = DomoscioViz::AuthorizationToken::Manager.get_token
140
+ if auth_token && auth_token[:access_token] && auth_token[:refresh_token]
141
+ [false, send_current_tokens(auth_token)]
142
+ else
143
+ [true, request_new_tokens]
144
+ end
145
+ rescue SyntaxError, StandardError
146
+ [true, request_new_tokens]
136
147
  end
137
- headers
148
+ end
149
+
150
+ # If stored token successfully loaded we build the header with them
151
+ #
152
+ def self.send_current_tokens(auth_token)
153
+ {
154
+ 'user_agent' => "#{DomoscioViz.user_agent}",
155
+ 'ClientId' => "#{DomoscioViz.configuration.client_id}",
156
+ 'AccessToken' => "#{auth_token[:access_token]}",
157
+ 'RefreshToken' => "#{auth_token[:refresh_token]}",
158
+ 'Content-Type' => 'application/json'
159
+ }
160
+ end
161
+
162
+ # If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
163
+ def self.request_new_tokens
164
+ {
165
+ 'user_agent' => "#{DomoscioViz.user_agent}",
166
+ 'ClientId' => "#{DomoscioViz.configuration.client_id}",
167
+ 'Authorization' => "Token token=#{DomoscioViz.configuration.client_passphrase}",
168
+ 'Content-Type' => 'application/json'
169
+ }
138
170
  end
139
171
 
140
172
  DomoscioViz.configure do |c|
141
- c.preproduction = false
142
173
  c.client_id = nil
143
174
  c.client_passphrase = nil
144
175
  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.5a"
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.5a
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-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -59,11 +59,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
59
  version: '0'
60
60
  required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  requirements:
62
- - - ">="
62
+ - - ">"
63
63
  - !ruby/object:Gem::Version
64
- version: '0'
64
+ version: 1.3.1
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.