domoscio_viz 0.2.0 → 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: '03468c791eacd5b3f47a390530f79cf17e5958f62b4c10e4dbd6ae4f7142210e'
4
- data.tar.gz: 97dd49337711f35d774db1b9958524dc6ac9a20f77cda5173723cce4e79061f3
3
+ metadata.gz: c21fbe0307ea47d6608df642b9a72d6a684d707002aad710369a1deeb6e8b150
4
+ data.tar.gz: 2e8c0a44e2c64fb0bec2d7b3e38ed8feccd52183e34c17f0100db24d8a23253c
5
5
  SHA512:
6
- metadata.gz: 6f96faeb12b74a5efe1d32e9e28a6d50763c05c969239114127c4cff14b45da9a6be097bd3596a2aa1976f3ab5601b916891fd423b68d5c2185a3ca4bb997b9f
7
- data.tar.gz: 60fce846d350aa913453f5c35de699a26e7bae9cf7379d56b968bee6b1ae433ccf7bc065eb612a450af028738a8d81cca75f7c5e002906e953b5a681bc882931
6
+ metadata.gz: 31c4a07d614f7228da16b172b166df19bb46559658e3d4b2e1facd74952ff5f610ac536eb411f1b8723104c6628bdf17f99af2966c4dc7ba63f8734e2a14cb75
7
+ data.tar.gz: ea7ee09cad87f5c02af87783729b71e01c9bcb46ac45666902b89b360a1cf1806ccafccf5651a84e1864f5a40dd9f3ab8d34c072ea754d9e1018af4d25919ccd
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,84 @@ 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
+ uri = api_uri(url)
68
55
 
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 = {}
56
+ response = DomoscioViz.send_request(uri, method, params, headers)
57
+ return response if response.kind_of? DomoscioViz::ProcessingError
58
+
59
+ begin
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
63
+ rescue MultiJson::LoadError => exception
64
+ data = ProcessingError.new(uri, 500, exception, response.body, params)
65
+ rescue ResponseError => exception
66
+ data = exception
77
67
  end
78
68
 
79
69
  data
80
70
  end
81
71
 
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|
72
+ private
73
+
74
+ # This function catches usual Http errors during calls
75
+ #
76
+ def self.send_request(uri, method, params, headers)
77
+ begin
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
81
+ rescue Timeout::Error, Errno::EINVAL, HTTP::ConnectionError, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => exception
82
+ ProcessingError.new(uri, 500, exception, response, params)
83
+ end
84
+ end
85
+
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?
91
+ raise ResponseError.new(uri, 500, {error: {status: 500, message: 'VisualizationEngine not available'}}, {}, params)
92
+ else
93
+ raise ResponseError.new(uri, response.code.to_i, DomoscioViz::JSON.load((response.body.nil? ? '' : response.body), :symbolize_keys => true), response.body, params)
94
+ end
95
+ end
96
+ end
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|
84
102
  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
103
+ req.body = DomoscioRails::JSON.dump(params)
87
104
  http.request req
88
105
  end
89
106
  end
90
107
 
91
- private
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
92
116
 
93
117
  def self.user_agent
94
118
  @uname ||= get_uname
95
-
96
119
  {
97
120
  bindings_version: DomoscioViz::VERSION,
98
121
  lang: 'ruby',
@@ -108,34 +131,48 @@ module DomoscioViz
108
131
  'uname lookup failed'
109
132
  end
110
133
 
134
+ # Process the token loading and analyze
135
+ # will return the processed headers and a token store flag
136
+ #
111
137
  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
- }
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]
129
147
  end
130
- 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
+ }
131
170
  end
132
171
 
133
172
  DomoscioViz.configure do |c|
134
- c.preproduction = false
135
173
  c.client_id = nil
136
174
  c.client_passphrase = nil
137
175
  c.temp_dir = File.expand_path('../tmp', __FILE__)
138
176
  FileUtils.mkdir_p(c.temp_dir) unless File.directory?(c.temp_dir)
139
177
  end
140
-
141
- end
178
+ 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.5a"
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.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: 2020-11-04 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
@@ -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
@@ -60,11 +59,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
59
  version: '0'
61
60
  required_rubygems_version: !ruby/object:Gem::Requirement
62
61
  requirements:
63
- - - ">="
62
+ - - ">"
64
63
  - !ruby/object:Gem::Version
65
- version: '0'
64
+ version: 1.3.1
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