moesif_api 1.2.17 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbeae4f833a8dd46f08feb1363908774c9cac999361cee7069785a07cf31f049
4
- data.tar.gz: a05fe5f642e5b79dae01b37b8eb7cf3912d57ddd2e432693d6514882e4df3bea
3
+ metadata.gz: d889e01e7b3cc16297fa23ad6ff7a0125ff7689f78bd85647eeca511c606cc09
4
+ data.tar.gz: 4160000b4f7ae1dd9fd24821dbed4d4fe0f44497f4c9e4deb71ce2d2deefda92
5
5
  SHA512:
6
- metadata.gz: 58e2ddeafce8e3fe7e1cab639b8621cdbf233ad79f16d399ba7f4bfad633f9d63f57d5da32ca8afeb3abbc1b057fadcc37039b5e6e56ac813be3591a17e3c017
7
- data.tar.gz: cff9423f46905fb3951c1bfaaef7a274d3dc54b32327336cbfe3409b0ebba123fcf027fefe13eb79a388f18bdaede1f80d7704bc3c0599503f6341dc4cfda689
6
+ metadata.gz: 1c11e000cccb456b87cc1857dd5071344254b7b30c2264e44bb0302908fd1ea189368406b244b4c484cbe0eb44f94d448233e64e4c98beb27226206b8b06a898
7
+ data.tar.gz: 43a7c046d17cdce30a7cf03d0d3a0661bb5418a9d49d0459147a74de851f98faf73e99d2ca214d46910dd85dedbba8e422bb4733f74ddedf7b4325c8babfea11
@@ -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.1'
12
+
13
+ @user_agent = "moesifapi-ruby/2.0.1"
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.1
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-07-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test-unit
@@ -31,6 +31,66 @@ dependencies:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 3.5.0
34
+ - !ruby/object:Gem::Dependency
35
+ name: faraday
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.7'
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '2.6'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '2.7'
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '2.6'
54
+ - !ruby/object:Gem::Dependency
55
+ name: faraday-net_http_persistent
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.0'
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.0.1
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '2.0'
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 2.0.1
74
+ - !ruby/object:Gem::Dependency
75
+ name: faraday-retry
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: 2.2.0
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.0.0
84
+ type: :runtime
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 2.2.0
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.0.0
34
94
  - !ruby/object:Gem::Dependency
35
95
  name: json_mapper
36
96
  requirement: !ruby/object:Gem::Requirement
@@ -52,19 +112,25 @@ dependencies:
52
112
  - !ruby/object:Gem::Version
53
113
  version: 0.2.1
54
114
  - !ruby/object:Gem::Dependency
55
- name: moesif_unirest
115
+ name: net-http-persistent
56
116
  requirement: !ruby/object:Gem::Requirement
57
117
  requirements:
58
118
  - - "~>"
59
119
  - !ruby/object:Gem::Version
60
- version: 1.1.6
120
+ version: '4.0'
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 4.0.1
61
124
  type: :runtime
62
125
  prerelease: false
63
126
  version_requirements: !ruby/object:Gem::Requirement
64
127
  requirements:
65
128
  - - "~>"
66
129
  - !ruby/object:Gem::Version
67
- version: 1.1.6
130
+ version: '4.0'
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: 4.0.1
68
134
  description: Collection/Data Ingestion API
69
135
  email: derric@moesif.com
70
136
  executables: []
@@ -80,13 +146,13 @@ files:
80
146
  - lib/moesif_api/controllers/base_controller.rb
81
147
  - lib/moesif_api/controllers/health_controller.rb
82
148
  - lib/moesif_api/exceptions/api_exception.rb
149
+ - lib/moesif_api/http/faraday_client.rb
83
150
  - lib/moesif_api/http/http_call_back.rb
84
151
  - lib/moesif_api/http/http_client.rb
85
152
  - lib/moesif_api/http/http_context.rb
86
153
  - lib/moesif_api/http/http_method_enum.rb
87
154
  - lib/moesif_api/http/http_request.rb
88
155
  - lib/moesif_api/http/http_response.rb
89
- - lib/moesif_api/http/unirest_client.rb
90
156
  - lib/moesif_api/models/base_model.rb
91
157
  - lib/moesif_api/models/campaign_model.rb
92
158
  - lib/moesif_api/models/company_model.rb
@@ -112,7 +178,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
178
  requirements:
113
179
  - - ">="
114
180
  - !ruby/object:Gem::Version
115
- version: '2.0'
181
+ version: '2.6'
116
182
  required_rubygems_version: !ruby/object:Gem::Requirement
117
183
  requirements:
118
184
  - - ">="
@@ -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