kapacitor-ruby 1.0.1 → 1.0.2

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
  SHA1:
3
- metadata.gz: 79bbedb54161374f17dfd6d1781e1baf8d483760
4
- data.tar.gz: 9e0d4b2b4aa32da574f330bb32037918d07256b4
3
+ metadata.gz: 97fd28ae0c38ca2649dc1aa6caf64d93486148ab
4
+ data.tar.gz: 87c38785fbf478b9c4bb5bcb1aed5f983173da63
5
5
  SHA512:
6
- metadata.gz: 6925cab3a4ac82ce9dd82a163b6bce11a45d80b43b871e3e8190749700a403fdb8b1299ff88720343b38910ec905dd3561c8e23722da662f4798a8949e4f8599
7
- data.tar.gz: 7c68807eb08758aa8bb953cbf09a6c346681754e764ad647232a0b211676342631dcc9a19e4e6774c1098ad91694b99a0e97571fe025c1f970c0493cd5613f46
6
+ metadata.gz: 976de61620d4e124b53d5acfc26fcf711ff2fdcb0c9e936fbc85d2cd652262a94f9bf67078205ae6afb88d53fa34733373490617c2a0e7de166821710628e0c4
7
+ data.tar.gz: 6c2fe44b7f27e0bf7b40b1ce636c057bf3b0fe876c17b51b7e140a3d70ce6ce03c007603401b1332fa60d4238abd0ba1279670d7aea5a12545d6049c6e2a8ca8
@@ -1,10 +1,10 @@
1
- require 'net/http'
1
+ require 'httpclient'
2
2
  require 'json'
3
3
 
4
4
  module Kapacitor
5
5
  class Client
6
6
  # @return [URI] Kapacitor REST API URL
7
- attr_reader :uri
7
+ attr_reader :url
8
8
  # @return [Net::HTTP] HTTP client instance
9
9
  attr_reader :http
10
10
 
@@ -13,9 +13,9 @@ module Kapacitor
13
13
  # @param url [String] Kapacitor REST API's URL (defaults to `http://localhost:9092`)
14
14
  # @param version [Integer] API version (defaults to `v1preview`)
15
15
  #
16
- def initialize(url: 'http://localhost:9092', version: 'v1preview')
17
- @uri = URI.parse("#{url}/kapacitor/#{version}")
18
- @http = Net::HTTP.new(@uri.host, @uri.port)
16
+ def initialize(url: 'http://localhost:9092/kapacitor', version: 'v1preview')
17
+ @url = URI.join(url, version)
18
+ @http = HTTPClient.new
19
19
  end
20
20
 
21
21
  # Define a Kapacitor template
@@ -71,7 +71,7 @@ module Kapacitor
71
71
  ret = []
72
72
 
73
73
  loop do
74
- res = api_get(endpoint: "/templates?offset=#{offset}&limit=#{limit}")['templates']
74
+ res = api_get(endpoint: "templates?offset=#{offset}&limit=#{limit}")['templates']
75
75
  break unless res.size > 0
76
76
  ret += res
77
77
  offset += limit
@@ -162,11 +162,11 @@ module Kapacitor
162
162
  tasks = []
163
163
 
164
164
  loop do
165
- res = api_get(endpoint: "/tasks?fields=id&offset=#{offset}&limit=#{limit}")['tasks']
165
+ res = api_get(endpoint: "tasks?fields=id&offset=#{offset}&limit=#{limit}")['tasks']
166
166
  break unless res.size > 0
167
167
 
168
168
  res.each do |task|
169
- tasks << api_get(endpoint: "/tasks/#{task['id']}")
169
+ tasks << api_get(endpoint: "tasks/#{task['id']}")
170
170
  end
171
171
 
172
172
  offset += limit
@@ -227,28 +227,27 @@ module Kapacitor
227
227
  # @return [Array[Hash]] List of handlers
228
228
  #
229
229
  def topic_handlers(topic:)
230
- return api_get(endpoint: "/alerts/topics/#{topic}/handlers")['handlers']
230
+ api_get(endpoint: "alerts/topics/#{topic}/handlers")['handlers']
231
231
  end
232
232
 
233
233
  private
234
234
  # Perform a HTTP GET request
235
235
  #
236
236
  # @param endpoint [String] HTTP API endpoint
237
+ # @param query [String] HTTP query
237
238
  # @return [Array[Hash], Hash] API response
238
239
  #
239
- def api_get(endpoint:)
240
+ def api_get(endpoint:, query: nil)
240
241
  begin
241
- req = Net::HTTP::Get.new(self.uri.path + endpoint, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
242
- resp = self.http.request(req)
243
-
244
- if resp.code == '200'
242
+ resp = self.http.get([self.url, endpoint].join('/'), query, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
243
+ if resp.status == 200
245
244
  begin
246
245
  data = JSON.parse(resp.body)
247
246
  rescue JSON::ParserError
248
247
  raise Exception, "Failed to decode response message"
249
248
  end
250
249
  else
251
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
250
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
252
251
  end
253
252
  rescue
254
253
  raise Exception, "Failed to execute GET request to Kapacitor REST API (#{$!})"
@@ -264,18 +263,15 @@ private
264
263
  #
265
264
  def api_post(endpoint:, data:)
266
265
  begin
267
- req = Net::HTTP::Post.new(self.uri.path + endpoint, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
268
- req.body = data.to_json
269
- resp = self.http.request(req)
270
-
271
- if resp.code == '200'
266
+ resp = self.http.post([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
267
+ if resp.status == 200
272
268
  begin
273
269
  data = JSON.parse(resp.body)
274
270
  rescue JSON::ParserError
275
271
  raise Exception, "Failed to decode response message"
276
272
  end
277
273
  else
278
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
274
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
279
275
  end
280
276
  rescue
281
277
  raise Exception, "Failed to execute POST request to Kapacitor REST API (#{$!})"
@@ -290,10 +286,8 @@ private
290
286
  #
291
287
  def api_delete(endpoint:)
292
288
  begin
293
- req = Net::HTTP::Delete.new(self.uri.path + endpoint, {'Content-type' => 'application/json', 'Accept' => 'application/json'})
294
- resp = self.http.request(req)
295
-
296
- if resp.code == '204'
289
+ resp = self.http.delete([self.url, endpoint].join('/'), {'Content-type' => 'application/json', 'Accept' => 'application/json'})
290
+ if resp.status == 204
297
291
  if resp.body
298
292
  begin
299
293
  data = JSON.parse(resp.body)
@@ -302,7 +296,7 @@ private
302
296
  end
303
297
  end
304
298
  else
305
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
299
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
306
300
  end
307
301
  rescue
308
302
  raise Exception, "Failed to execute DELETE request to Kapacitor REST API (#{$!})"
@@ -318,18 +312,15 @@ private
318
312
  #
319
313
  def api_patch(endpoint:, data:)
320
314
  begin
321
- req = Net::HTTP::Patch.new(self.uri.path + endpoint, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
322
- req.body = data.to_json
323
- resp = self.http.request(req)
324
-
325
- if resp.code == '200'
315
+ resp = self.http.patch([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
316
+ if resp.status == 200
326
317
  begin
327
318
  data = JSON.parse(resp.body)
328
319
  rescue JSON::ParserError
329
320
  raise Exception, "Failed to decode response message"
330
321
  end
331
322
  else
332
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
323
+ raise Exception, "Query returned a non successful HTTP code (Code: #{resp.status}, Reason: #{resp.reason})"
333
324
  end
334
325
  rescue
335
326
  raise Exception, "Failed to execute PATCH request to Kapacitor REST API (#{$!})"
@@ -345,18 +336,15 @@ private
345
336
  #
346
337
  def api_put(endpoint:, data:)
347
338
  begin
348
- req = Net::HTTP::Put.new(self.uri.path + endpoint, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
349
- req.body = data.to_json
350
- resp = self.http.request(req)
351
-
352
- if resp.code == '200'
339
+ resp = self.http.put([self.url, endpoint].join('/'), data.to_json, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
340
+ if resp.status == 200
353
341
  begin
354
342
  data = JSON.parse(resp.body)
355
343
  rescue JSON::ParserError
356
344
  raise Exception, "Failed to decode response message"
357
345
  end
358
346
  else
359
- raise Exception, "Query returned a non successful HTTP code (Code: #{resp.code}, Error: #{resp.message})"
347
+ raise Exception, "Query returned a non successful HTTP code (Status: #{resp.status}, Reason: #{resp.reason})"
360
348
  end
361
349
  rescue
362
350
  raise Exception, "Failed to execute PUT request to Kapacitor REST API (#{$!})"
@@ -3,7 +3,7 @@
3
3
  #
4
4
 
5
5
  module Kapacitor
6
- VERSION = "1.0.1"
6
+ VERSION = "1.0.2"
7
7
 
8
8
  def self.version
9
9
  VERSION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kapacitor-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matteo Cerutti
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2017-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpclient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.4.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: json
15
29
  requirement: !ruby/object:Gem::Requirement