domoscio_viz 0.2.0 → 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: '03468c791eacd5b3f47a390530f79cf17e5958f62b4c10e4dbd6ae4f7142210e'
4
- data.tar.gz: 97dd49337711f35d774db1b9958524dc6ac9a20f77cda5173723cce4e79061f3
3
+ metadata.gz: 86781580f4a835cfa5ac44fd6bc32933955f484fecbef42222957ea05239bb2e
4
+ data.tar.gz: 58776787e717e2f6d6527c995a8d1b1865d5d4bc78a6067476b1dc049a9a39b5
5
5
  SHA512:
6
- metadata.gz: 6f96faeb12b74a5efe1d32e9e28a6d50763c05c969239114127c4cff14b45da9a6be097bd3596a2aa1976f3ab5601b916891fd423b68d5c2185a3ca4bb997b9f
7
- data.tar.gz: 60fce846d350aa913453f5c35de699a26e7bae9cf7379d56b968bee6b1ae433ccf7bc065eb612a450af028738a8d81cca75f7c5e002906e953b5a681bc882931
6
+ metadata.gz: 4dbb5722a87cb87d27c40809144a88235c87f4ecae0923ba3b7d02274397ce15902fea2e58a09c40ca72603684b57c57c6ff1745b2267dec6b56eae1230cb17a
7
+ data.tar.gz: 801a9125a3a74fc97c56cb83b01a2d531c5837e928469cdf120ac05d91e056f616725ede57265f73ffd939ffd001bac21e39f0281b6135e1134db766ef4d93e0
data/lib/domoscio_viz.rb CHANGED
@@ -2,16 +2,13 @@
2
2
  require 'net/https'
3
3
  require 'cgi/util'
4
4
  require 'multi_json'
5
-
6
5
  # helpers
7
6
  require 'domoscio_viz/version'
8
7
  require 'domoscio_viz/json'
9
8
  require 'domoscio_viz/errors'
10
9
  require 'domoscio_viz/authorization_token'
11
-
12
10
  # generators
13
11
  require 'domoscio_viz/generators/install_generator'
14
-
15
12
  # resources
16
13
  require 'domoscio_viz/http_calls'
17
14
  require 'domoscio_viz/resource'
@@ -20,28 +17,10 @@ require 'domoscio_viz/chart/chart'
20
17
  module DomoscioViz
21
18
 
22
19
  class Configuration
23
- attr_accessor :preproduction, :test, :root_url,
24
- :client_id, :client_passphrase,
25
- :temp_dir
26
-
27
- def preproduction
28
- @preproduction || false
29
- end
30
-
31
- def test
32
- @test || false
33
- end
20
+ attr_accessor :root_url, :client_id, :client_passphrase, :temp_dir
34
21
 
35
22
  def root_url
36
- if @preproduction == true
37
- if @test == true
38
- @root_url || "https://domoscio-viz-engine-preprod.azurewebsites.net"
39
- else
40
- @root_url || "https://domoscio-viz-engine-v2.azurewebsites.net"
41
- end
42
- else
43
- @root_url || "http://localhost:3002"
44
- end
23
+ @root_url ||= ""
45
24
  end
46
25
  end
47
26
 
@@ -59,40 +38,86 @@ module DomoscioViz
59
38
  end
60
39
 
61
40
  #
62
- def self.request(method, url, params={}, filters={}, headers = request_headers, before_request_proc = nil)
63
- return false if @disabled
64
- uri = api_uri(url)
65
- uri.query = URI.encode_www_form(filters) unless filters.empty?
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={})
66
52
 
67
- res = DomoscioViz.send_request(uri, method, params, headers, before_request_proc)
53
+ store_tokens, headers = request_headers
54
+ params.merge!({'per_page': 2000}) unless params[:per_page]
55
+ uri = api_uri(url)
68
56
 
69
- # decode json data
70
- begin
71
- data = DomoscioViz::JSON.load(res.body.nil? ? '' : res.body)
72
- unless (res.kind_of? Net::HTTPClientError) || (res.kind_of? Net::HTTPServerError)
73
- DomoscioViz::AuthorizationToken::Manager.storage.store({access_token: res['Accesstoken'], refresh_token: res['Refreshtoken']})
74
- end
75
- rescue MultiJson::LoadError
76
- data = {}
57
+ response = DomoscioViz.send_request(uri, method, params, headers)
58
+ return response if response.kind_of? DomoscioViz::ProcessingError
59
+
60
+ begin
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
64
+ rescue MultiJson::LoadError => exception
65
+ data = ProcessingError.new(uri, 500, exception, response.body, params)
66
+ rescue ResponseError => exception
67
+ data = exception
77
68
  end
78
69
 
79
70
  data
80
71
  end
81
72
 
82
- def self.send_request(uri, method, params, headers, before_request_proc)
83
- res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| # , use_ssl: uri.scheme == 'https') do |http|
73
+ private
74
+
75
+ # This function catches usual Http errors during calls
76
+ #
77
+ def self.send_request(uri, method, params, headers)
78
+ begin
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
82
+ rescue Timeout::Error, Errno::EINVAL, HTTP::ConnectionError, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => exception
83
+ ProcessingError.new(uri, 500, exception, response, params)
84
+ end
85
+ end
86
+
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?
92
+ raise ResponseError.new(uri, 500, {error: {status: 500, message: 'VisualizationEngine not available'}}, {}, params)
93
+ else
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)
96
+ end
97
+ end
98
+ end
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|
84
104
  req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers)
85
- req.body = DomoscioViz::JSON.dump(params)
86
- before_request_proc.call(req) if before_request_proc
105
+ req.body = DomoscioRails::JSON.dump(params)
87
106
  http.request req
88
107
  end
89
108
  end
90
109
 
91
- private
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
92
118
 
93
119
  def self.user_agent
94
120
  @uname ||= get_uname
95
-
96
121
  {
97
122
  bindings_version: DomoscioViz::VERSION,
98
123
  lang: 'ruby',
@@ -108,34 +133,48 @@ module DomoscioViz
108
133
  'uname lookup failed'
109
134
  end
110
135
 
136
+ # Process the token loading and analyze
137
+ # will return the processed headers and a token store flag
138
+ #
111
139
  def self.request_headers
112
- auth_token = DomoscioViz::AuthorizationToken::Manager.get_token
113
-
114
- if !auth_token.is_a? String
115
- headers = {
116
- 'user_agent' => "#{DomoscioViz.user_agent}",
117
- 'ClientId' => "#{DomoscioViz.configuration.client_id}",
118
- 'AccessToken' => "#{auth_token[:access_token]}",
119
- 'RefreshToken' => "#{auth_token[:refresh_token]}",
120
- 'Content-Type' => 'application/json'
121
- }
122
- else
123
- headers = {
124
- 'user_agent' => "#{DomoscioViz.user_agent}",
125
- 'ClientId' => "#{DomoscioViz.configuration.client_id}",
126
- 'Authorization' => "Token token=#{DomoscioViz.configuration.client_passphrase}",
127
- 'Content-Type' => 'application/json'
128
- }
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]
129
149
  end
130
- 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
+ }
131
172
  end
132
173
 
133
174
  DomoscioViz.configure do |c|
134
- c.preproduction = false
135
175
  c.client_id = nil
136
176
  c.client_passphrase = nil
137
177
  c.temp_dir = File.expand_path('../tmp', __FILE__)
138
178
  FileUtils.mkdir_p(c.temp_dir) unless File.directory?(c.temp_dir)
139
179
  end
140
-
141
- end
180
+ end
@@ -1,6 +1,5 @@
1
1
  module DomoscioViz
2
2
  module AuthorizationToken
3
-
4
3
  class Manager
5
4
 
6
5
  class << self
@@ -13,9 +12,7 @@ module DomoscioViz
13
12
  end
14
13
 
15
14
  def get_token
16
- token = storage.get
17
- token = DomoscioViz.configuration.client_passphrase if token.nil?
18
- token
15
+ storage.get
19
16
  end
20
17
  end
21
18
  end
@@ -36,9 +33,7 @@ module DomoscioViz
36
33
 
37
34
  def initialize(temp_dir = nil)
38
35
  @temp_dir = temp_dir || DomoscioViz.configuration.temp_dir
39
- if !@temp_dir
40
- raise "Path to temporary folder is not defined"
41
- end
36
+ raise "Path to temporary folder is not defined" unless @temp_dir
42
37
  end
43
38
 
44
39
  def get
@@ -1,24 +1,26 @@
1
1
  module DomoscioViz
2
-
3
2
  # Generic error superclass for MangoPay specific errors.
4
3
  # Currently never instantiated directly.
5
4
  # Currently only single subclass used.
6
5
  class Error < StandardError
7
6
  end
8
-
9
- # Thrown from any MangoPay API call whenever
10
- # it returns response with HTTP code != 200.
7
+ # ResponseError from VizEngine
11
8
  class ResponseError < Error
12
-
13
- attr_reader :request_url, :code, :details
14
-
15
- def initialize(request_url, code, details)
16
- @request_url, @code, @details = request_url, code, details
9
+ attr_reader :request_url, :code, :details, :body, :request_params
10
+ def initialize(request_url, code, details = {}, body = nil, request_params = {})
11
+ @request_url, @code, @details, @body, @request_params = request_url, code, details, body, request_params
17
12
  super(message) if message
18
13
  end
14
+ def message; @details.is_a?(Hash) ? @details.dig(:error, :message) : @details; end
15
+ end
19
16
 
20
- def message; @details['Message']; end
21
- def type; @details['Type']; end
22
- def errors; @details['errors']; end
17
+ # ProcessingError from Domoscio_viz
18
+ class ProcessingError < Error
19
+ attr_reader :request_url, :code, :details, :body, :request_params
20
+ def initialize(request_url, code, details = {}, body = nil, request_params = {})
21
+ @request_url, @code, @details, @body, @request_params = request_url, code, details, body, request_params
22
+ super(message) if message
23
+ end
24
+ def message; @details.message; end
23
25
  end
24
26
  end
@@ -1,12 +1,10 @@
1
1
  require 'rails/generators'
2
-
3
2
  module DomoscioViz
4
3
  class InstallGenerator < ::Rails::Generators::Base
5
4
  source_root File.expand_path('../templates', __FILE__)
6
5
  desc "Generate config file for DomoscioViz configuration"
7
-
8
6
  def install
9
- copy_file "install.rb", "config/initializers/domoscio_viz.rb"
7
+ copy_file "install.rb", "config/initializers/domoscio_viz.rb"
10
8
  end
11
9
  end
12
10
  end
@@ -1,13 +1,6 @@
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
-
10
4
  c.temp_dir = File.expand_path('../tmp', __FILE__)
11
-
12
5
  FileUtils.mkdir_p(c.temp_dir) unless File.directory?(c.temp_dir)
13
6
  end
@@ -2,15 +2,10 @@ module DomoscioViz
2
2
  module HTTPCalls
3
3
  module GetUrl
4
4
  module ClassMethods
5
- # In order to catch current SocketsTimeoutError that seem to appear when the VizEngine stays Idle for too long
6
- # Perform the request a second time if it fails the fisrt time
7
5
  def get_url(util_name = nil, params = {})
8
- response = DomoscioViz.request(:post, url(util_name), params)
9
- response = DomoscioViz.request(:post, url(util_name), params) unless response && response.is_a?(Hash) && response['url'] && response['success'] == true
10
- response
6
+ DomoscioViz.request(:post, url(util_name), params)
11
7
  end
12
8
  end
13
-
14
9
  def self.included(base)
15
10
  base.extend(ClassMethods)
16
11
  end
@@ -21,4 +21,4 @@ module DomoscioViz
21
21
  end
22
22
  end
23
23
  end
24
- end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module DomoscioViz
2
- VERSION = "0.2.0"
3
- end
2
+ VERSION = "0.2.5"
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.0
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: 2020-11-04 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
@@ -44,7 +44,6 @@ files:
44
44
  - lib/domoscio_viz/json.rb
45
45
  - lib/domoscio_viz/resource.rb
46
46
  - lib/domoscio_viz/version.rb
47
- - lib/tasks/domoscio_rails_tasks.rake
48
47
  homepage: http://www.domoscio.com
49
48
  licenses:
50
49
  - MIT
@@ -64,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
63
  - !ruby/object:Gem::Version
65
64
  version: '0'
66
65
  requirements: []
67
- rubygems_version: 3.1.2
66
+ rubygems_version: 3.0.8
68
67
  signing_key:
69
68
  specification_version: 4
70
69
  summary: Summary of DomoscioViz.
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :domoscio_viz do
3
- # # Task goes here
4
- # end