moesif_api 1.2.17 → 2.0.2

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: cbeae4f833a8dd46f08feb1363908774c9cac999361cee7069785a07cf31f049
4
- data.tar.gz: a05fe5f642e5b79dae01b37b8eb7cf3912d57ddd2e432693d6514882e4df3bea
3
+ metadata.gz: 0251e30afabbe270c78ce4240d5895c1414ab56d12b8ba6498d2bf6d6e5f7db5
4
+ data.tar.gz: 302d117fe223259e353e53510b139c3d1562a0f258afdbe24477fb9d3ca85015
5
5
  SHA512:
6
- metadata.gz: 58e2ddeafce8e3fe7e1cab639b8621cdbf233ad79f16d399ba7f4bfad633f9d63f57d5da32ca8afeb3abbc1b057fadcc37039b5e6e56ac813be3591a17e3c017
7
- data.tar.gz: cff9423f46905fb3951c1bfaaef7a274d3dc54b32327336cbfe3409b0ebba123fcf027fefe13eb79a388f18bdaede1f80d7704bc3c0599503f6341dc4cfda689
6
+ metadata.gz: aac5a817a9a29462c5c4e9f22c7f635b86a55b9af8d97e6b3f1f2d2574d8731b78ef52773bb9e1a6f3e59b27ee309e50039ac1802f4238acb4aa863a758b1acb
7
+ data.tar.gz: 85512bb4f2a0f0b5420dfd005ebc159b93f79ec12cc4301f3114f36543342d12e3c1c0bc538e48a889713b9efebf8b9ff1152363b2cfa9e257d5879398e4aead
@@ -8,13 +8,16 @@ module MoesifApi
8
8
  # Your Application Id for authentication/authorization
9
9
  @application_id = 'SET_ME'
10
10
 
11
- @version = '1.2.12'
11
+ @version = '2.0.2'
12
+
13
+ @user_agent = "moesifapi-ruby/2.0.2"
12
14
 
13
15
  # create the getters and setters
14
16
  class << self
15
17
  attr_accessor :base_uri
16
18
  attr_accessor :application_id
17
19
  attr_accessor :version
20
+ attr_accessor :user_agent
18
21
  end
19
22
  end
20
23
  end
@@ -90,11 +90,7 @@ module MoesifApi
90
90
  _query_url = APIHelper.clean_url _query_builder
91
91
 
92
92
  # prepare headers
93
- _headers = {
94
- 'content-type' => 'application/json; charset=utf-8',
95
- 'X-Moesif-Application-Id' => Configuration.application_id,
96
- 'User-Agent' => 'moesifapi-ruby/' + Configuration.version
97
- }
93
+ _headers = build_request_headers()
98
94
 
99
95
  # Create the HttpRequest object for the call, fetch and wrap the respone in a HttpContext object
100
96
  _response, _context = send_moesif(_query_url, _headers, body)
@@ -120,12 +116,7 @@ module MoesifApi
120
116
  _query_url = APIHelper.clean_url _query_builder
121
117
 
122
118
  # prepare headers
123
- _headers = {
124
- 'content-type' => 'application/json; charset=utf-8',
125
- 'X-Moesif-Application-Id' => Configuration.application_id,
126
- 'User-Agent' => 'moesifapi-ruby/' + Configuration.version
127
- }
128
-
119
+ _headers = build_request_headers()
129
120
  # Create the HttpRequest object for the call, fetch and wrap the respone in a HttpContext object
130
121
  _response, _context = send_moesif(_query_url, _headers, body)
131
122
 
@@ -148,11 +139,7 @@ module MoesifApi
148
139
  _query_url = APIHelper.clean_url _query_builder
149
140
 
150
141
  # prepare headers
151
- _headers = {
152
- 'content-type' => 'application/json; charset=utf-8',
153
- 'X-Moesif-Application-Id' => Configuration.application_id,
154
- 'User-Agent' => 'moesifapi-ruby/' + Configuration.version
155
- }
142
+ _headers = build_request_headers()
156
143
 
157
144
  # Create the HttpRequest object for the call, fetch and wrap the respone in a HttpContext object
158
145
  _response, _context = send_moesif(_query_url, _headers, body)
@@ -175,10 +162,7 @@ module MoesifApi
175
162
  _query_url = APIHelper.clean_url _query_builder
176
163
 
177
164
  # prepare headers
178
- _headers = {
179
- 'content-type' => 'application/json; charset=utf-8',
180
- 'X-Moesif-Application-Id' => Configuration.application_id
181
- }
165
+ _headers = build_request_headers()
182
166
 
183
167
  # Create the HttpRequest object for the call
184
168
  _request = @http_client.get _query_url, headers: _headers
@@ -198,8 +182,7 @@ module MoesifApi
198
182
  # Global error handling using HTTP status codes.
199
183
  validate_response(_context)
200
184
 
201
- # Return the response
202
- return _response
185
+ return _response.json_body, _context
203
186
  end
204
187
 
205
188
  # Get App Governance Rules
@@ -216,10 +199,7 @@ module MoesifApi
216
199
  _query_url = APIHelper.clean_url _query_builder
217
200
 
218
201
  # prepare headers
219
- _headers = {
220
- 'content-type' => 'application/json; charset=utf-8',
221
- 'X-Moesif-Application-Id' => Configuration.application_id
222
- }
202
+ _headers = build_request_headers()
223
203
 
224
204
  # Create the HttpRequest object for the call
225
205
  _request = @http_client.get _query_url, headers: _headers
@@ -240,7 +220,7 @@ module MoesifApi
240
220
  validate_response(_context)
241
221
 
242
222
  # Return the response
243
- return _response
223
+ return _response.json_body, _context
244
224
  end
245
225
 
246
226
  # Update Data for a Single Company
@@ -257,11 +237,7 @@ module MoesifApi
257
237
  _query_url = APIHelper.clean_url _query_builder
258
238
 
259
239
  # prepare headers
260
- _headers = {
261
- 'content-type' => 'application/json; charset=utf-8',
262
- 'X-Moesif-Application-Id' => Configuration.application_id,
263
- 'User-Agent' => 'moesifapi-ruby/' + Configuration.version
264
- }
240
+ _headers = build_request_headers()
265
241
 
266
242
  # Create the HttpRequest object for the call, fetch and wrap the respone in a HttpContext object
267
243
  _response, _context = send_moesif(_query_url, _headers, body)
@@ -1,20 +1,27 @@
1
-
2
-
3
1
  module MoesifApi
4
2
  class BaseController
5
3
  attr_accessor :http_client, :http_call_back
6
4
 
7
- @@http_client = UnirestClient.new
5
+ @@faraday_http_client = FaradayClient.new
8
6
 
9
7
  def initialize(http_client: nil, http_call_back: nil)
10
- @http_client = http_client ||= @@http_client
8
+ @http_client = http_client ||= @@faraday_http_client
11
9
  @http_call_back = http_call_back
12
10
  end
13
11
 
14
- def validate_response(context)
15
- if !context.response.status_code.between?(200, 208) #[200,208] = HTTP OK
16
- raise APIException.new 'HTTP Response Not OK', context
17
- end
12
+ def build_request_headers
13
+ {
14
+ 'content-type' => 'application/json; charset=utf-8',
15
+ 'X-Moesif-Application-Id' => Configuration.application_id,
16
+ 'User-Agent' => Configuration.user_agent || ('moesifapi-ruby/' + Configuration.version)
17
+ }
18
+ end
19
+
20
+ def validate_response(context)
21
+ return if context.response.status_code.between?(200, 208) # [200,208] = HTTP OK
22
+
23
+ raise APIException.new 'HTTP Response Not OK', context
18
24
  end
25
+
19
26
  end
20
- end
27
+ end
@@ -21,12 +21,7 @@ module MoesifApi
21
21
  _query_url = APIHelper.clean_url _query_builder
22
22
 
23
23
  # prepare headers
24
- _headers = {
25
- 'accept' => 'application/json',
26
- 'X-Moesif-Application-Id' => Configuration.application_id,
27
- 'User-Agent' => 'moesifapi-ruby/' + Configuration.version
28
- }
29
-
24
+ _headers = build_request_headers()
30
25
  # Create the HttpRequest object for the call
31
26
  _request = @http_client.get _query_url, headers: _headers
32
27
 
@@ -0,0 +1,46 @@
1
+ require 'faraday'
2
+ require 'faraday/net_http_persistent'
3
+ require 'faraday/retry'
4
+ require 'json'
5
+
6
+ module MoesifApi
7
+ class FaradayClient < HttpClient
8
+ def initialize
9
+ super()
10
+ retry_options = {
11
+ max: 3,
12
+ interval: 0.1,
13
+ interval_randomness: 0.5,
14
+ backoff_factor: 2
15
+ }
16
+
17
+ @connection = Faraday.new({}) do |f|
18
+ f.request :retry, retry_options
19
+ f.adapter :net_http_persistent, pool_size: 5 do |http|
20
+ http.idle_timeout = 100
21
+ end
22
+ end
23
+ end
24
+
25
+ # Method overridden from HttpClient.
26
+ def execute_as_string(http_request)
27
+ response = @connection.run_request(http_request.http_method.downcase.to_sym,
28
+ http_request.query_url,
29
+ http_request.parameters, # body
30
+ http_request.headers)
31
+
32
+ convert_response(response)
33
+ end
34
+
35
+ # Method overridden from HttpClient.
36
+ def convert_response(response)
37
+ if response.body.is_a?(String)
38
+ raw_body = response.body
39
+ elsif !response.body.nil?
40
+ raw_body = JSON.generate(response.body)
41
+ end
42
+
43
+ HttpResponse.new(response.status, response.headers.dup, raw_body)
44
+ end
45
+ end
46
+ end
@@ -2,9 +2,9 @@
2
2
 
3
3
  module MoesifApi
4
4
  class HttpResponse
5
- attr_accessor :status_code, :headers, :raw_body
6
-
7
- # The constructor.
5
+ attr_accessor :status_code, :headers, :raw_body, :json_body
6
+
7
+ # The constructor.
8
8
  # @param [Integer] The status code returned by the server.
9
9
  # @param [Hash] The headers sent by the server in the response.
10
10
  # @param [String] The raw body of the response.
@@ -14,6 +14,16 @@ module MoesifApi
14
14
  @status_code = status_code
15
15
  @headers = headers
16
16
  @raw_body = raw_body
17
+
18
+ begin
19
+ if !raw_body.nil? and raw_body.is_a?(String) and (raw_body.start_with?("{") || raw_body.start_with?("["))
20
+ @json_body = JSON.parse(raw_body)
21
+ end
22
+ rescue StandardError => e
23
+ # we can't parse json body
24
+ # but it is ok, move forward anyways
25
+ # print "can't parse json " + e.to_s
26
+ end
17
27
  end
18
28
  end
19
29
  end
@@ -15,8 +15,11 @@ module MoesifApi
15
15
  end
16
16
 
17
17
  # Initializer with authentication and configuration parameters
18
- def initialize(application_id)
18
+ def initialize(application_id, user_agent=nil)
19
19
  Configuration.application_id = application_id
20
+ unless user_agent.nil?
21
+ Configuration.user_agent = user_agent
22
+ end
20
23
  end
21
24
  end
22
25
  end
data/lib/moesif_api.rb CHANGED
@@ -1,7 +1,6 @@
1
1
 
2
2
  require 'openssl'
3
3
  require 'json'
4
- require 'unirest'
5
4
 
6
5
  # Exceptions
7
6
  require_relative 'moesif_api/exceptions/api_exception.rb'
@@ -18,7 +17,7 @@ require_relative 'moesif_api/http/http_method_enum.rb'
18
17
  require_relative 'moesif_api/http/http_request.rb'
19
18
  require_relative 'moesif_api/http/http_response.rb'
20
19
  require_relative 'moesif_api/http/http_context.rb'
21
- require_relative 'moesif_api/http/unirest_client.rb'
20
+ require_relative 'moesif_api/http/faraday_client.rb'
22
21
 
23
22
  # Models
24
23
  require_relative 'moesif_api/models/base_model.rb'
@@ -164,11 +164,20 @@ class ApiControllerTests < ControllerTestBase
164
164
 
165
165
  # Get the application configuration
166
166
  def test_get_app_config()
167
-
168
167
  # Perform the API call through the SDK function
169
- app_config = self.class.controller.get_app_config()
168
+ app_config, _context = self.class.controller.get_app_config()
169
+ print "gonna print the app_cofig\n"
170
+ print app_config.to_s + "\n"
171
+ # Test response code
172
+ assert_equal(@response_catcher.response.status_code, 200)
173
+ end
170
174
 
175
+ def test_get_rules()
176
+ # Perform the API call through the SDK function
177
+ rules, _context = self.class.controller.get_rules()
171
178
  # Test response code
179
+ print 'gonna print the rules'
180
+ print rules.to_s
172
181
  assert_equal(@response_catcher.response.status_code, 200)
173
182
  end
174
183
 
@@ -9,8 +9,11 @@ class HttpResponseCatcher < MoesifApi::HttpCallBack
9
9
  # Catching the response
10
10
  def on_after_response(context)
11
11
  @response = context.response
12
+ # print "I am here in on after response\n"
13
+ # print "response raw_body is a string " + @response.raw_body.is_a?(String).to_s + "\n"
14
+ # print @response.raw_body + "\n"
15
+ # print "response json_body is a hash" + @response.json_body.is_a?(Hash).to_s
16
+ # print @response.json_body.to_s
17
+ # print "\nfnished printing json body"
12
18
  end
13
19
  end
14
-
15
-
16
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moesif_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.17
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moesif, Inc
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-07-07 00:00:00.000000000 Z
12
+ date: 2023-08-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test-unit
@@ -32,39 +32,75 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 3.5.0
34
34
  - !ruby/object:Gem::Dependency
35
- name: json_mapper
35
+ name: faraday
36
36
  requirement: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.2'
41
- - - ">="
40
+ version: '2'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ - !ruby/object:Gem::Dependency
49
+ name: faraday-net_http_persistent
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
42
53
  - !ruby/object:Gem::Version
43
- version: 0.2.1
54
+ version: '2'
44
55
  type: :runtime
45
56
  prerelease: false
46
57
  version_requirements: !ruby/object:Gem::Requirement
47
58
  requirements:
48
59
  - - "~>"
49
60
  - !ruby/object:Gem::Version
50
- version: '0.2'
51
- - - ">="
61
+ version: '2'
62
+ - !ruby/object:Gem::Dependency
63
+ name: faraday-retry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2'
69
+ type: :runtime
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2'
76
+ - !ruby/object:Gem::Dependency
77
+ name: json_mapper
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
52
88
  - !ruby/object:Gem::Version
53
- version: 0.2.1
89
+ version: '0'
54
90
  - !ruby/object:Gem::Dependency
55
- name: moesif_unirest
91
+ name: net-http-persistent
56
92
  requirement: !ruby/object:Gem::Requirement
57
93
  requirements:
58
94
  - - "~>"
59
95
  - !ruby/object:Gem::Version
60
- version: 1.1.6
96
+ version: '4'
61
97
  type: :runtime
62
98
  prerelease: false
63
99
  version_requirements: !ruby/object:Gem::Requirement
64
100
  requirements:
65
101
  - - "~>"
66
102
  - !ruby/object:Gem::Version
67
- version: 1.1.6
103
+ version: '4'
68
104
  description: Collection/Data Ingestion API
69
105
  email: derric@moesif.com
70
106
  executables: []
@@ -80,13 +116,13 @@ files:
80
116
  - lib/moesif_api/controllers/base_controller.rb
81
117
  - lib/moesif_api/controllers/health_controller.rb
82
118
  - lib/moesif_api/exceptions/api_exception.rb
119
+ - lib/moesif_api/http/faraday_client.rb
83
120
  - lib/moesif_api/http/http_call_back.rb
84
121
  - lib/moesif_api/http/http_client.rb
85
122
  - lib/moesif_api/http/http_context.rb
86
123
  - lib/moesif_api/http/http_method_enum.rb
87
124
  - lib/moesif_api/http/http_request.rb
88
125
  - lib/moesif_api/http/http_response.rb
89
- - lib/moesif_api/http/unirest_client.rb
90
126
  - lib/moesif_api/models/base_model.rb
91
127
  - lib/moesif_api/models/campaign_model.rb
92
128
  - lib/moesif_api/models/company_model.rb
@@ -112,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
148
  requirements:
113
149
  - - ">="
114
150
  - !ruby/object:Gem::Version
115
- version: '2.0'
151
+ version: '2.6'
116
152
  required_rubygems_version: !ruby/object:Gem::Requirement
117
153
  requirements:
118
154
  - - ">="
@@ -1,36 +0,0 @@
1
-
2
-
3
- module MoesifApi
4
- class UnirestClient < HttpClient
5
- # Method overridden from HttpClient.
6
- def execute_as_string(http_request)
7
- if http_request.username || http_request.password
8
- auth = {:user=>http_request.username, :password=>http_request.password}
9
- end
10
-
11
- response = Unirest.method(http_request.http_method.downcase).call(http_request.query_url,
12
- headers: http_request.headers, parameters: http_request.parameters,
13
- auth: auth)
14
-
15
- return convert_response(response)
16
- end
17
-
18
- # Method overridden from HttpClient.
19
- def execute_as_binary(http_request)
20
- if http_request.username || http_request.password
21
- auth = {:user=>http_request.username, :password=>http_request.password}
22
- end
23
-
24
- response = Unirest.method(http_request.http_method.downcase).call(http_request.query_url,
25
- headers: http_request.headers, parameters: http_request.parameters,
26
- auth: auth)
27
-
28
- return convert_response(response)
29
- end
30
-
31
- # Method overridden from HttpClient.
32
- def convert_response(response)
33
- return HttpResponse.new(response.code, response.headers.dup, response.raw_body.dup)
34
- end
35
- end
36
- end