ecoportal-api 0.3.4 → 0.3.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: 70979654188cd5e24e5456f4f2a668e5327faac08574388d9336123cca93ee6a
4
- data.tar.gz: e04b913a265037f1e57b7d52d606fd662eed8352459f2751b5c9a74f0dadccf6
3
+ metadata.gz: 4d25d42002423c85609bc9c4e436c614934239e62ae3b086f39c2073c0df925d
4
+ data.tar.gz: 53574533c9fe73a72c2eb2d8f5dd48f854e143482d3ce30a4acf2b1ebcdad63d
5
5
  SHA512:
6
- metadata.gz: 3dc34740e68615fb0da65fe0c691b60af1a9c66f0b695d283645bb25757cbfecaef38b0f02dd19b9de280249b7e5e8e7ff62895fefe59cf6b8af0446844c68bb
7
- data.tar.gz: 66a418368f6de049326e0fb7c3db9405acb57ecbbc8c2e3cc47b57aaf9212f8f0ec71be3958c2a03f2ce5607c306c8ed9ed2ec67b9709bdaf7b4a3f5f1c20563
6
+ metadata.gz: 9640f58bc1712869b3b2bacf7f6d3ec61afe439cdcf194ea8dacf29fba676f7828d2e2a636cea1ca919f0bf63a5687a3293c3e5148a8e77e9c32b66f2c0dcec9
7
+ data.tar.gz: 19059c77e63997094ab84d1b9f2f9ca14784001b37ebba753c7ed9bbad61dfab02e724bd38560add9a8e801f816af6cae0e20030e4a09f11186bf0cd78af4cfd
@@ -5,10 +5,11 @@ module Ecoportal
5
5
  class BatchOperation
6
6
  include Common::DocHelpers
7
7
 
8
- def initialize(base_path, wrapper)
8
+ def initialize(base_path, wrapper, logger: nil)
9
9
  @base_path = base_path
10
10
  @wrapper = wrapper
11
11
  @operations = []
12
+ @logger = logger
12
13
  end
13
14
 
14
15
  def as_json
@@ -20,21 +21,27 @@ module Ecoportal
20
21
  end
21
22
 
22
23
  def process_response(response)
23
- raise "Total failure in batch operation" unless response.success?
24
+ unless response.success?
25
+ log(:error) { "Total failure in batch operation." }
26
+ raise "Total failure in batch operation"
27
+ end
24
28
 
25
- response.body.each.with_index do |subresponse, idx|
26
- next unless callback = @operations[idx][:callback]
29
+ log(:info) { "Processing batch responses" }
27
30
 
28
- status = subresponse["status"]
29
- body = subresponse["response"]
30
- method = @operations[idx][:method]
31
+ response.body.each.with_index do |subresponse, idx|
32
+ callback = @operations[idx][:callback]
33
+ status = subresponse["status"]
34
+ body = subresponse["response"]
35
+ method = @operations[idx][:method]
31
36
 
32
37
  if status == 200 && method == "GET"
33
38
  batch_response = BatchResponse.new(status, body, @wrapper.new(body))
34
- callback.call batch_response, batch_response.result
39
+ log_batch_response(@operations[idx], batch_response)
40
+ callback && callback.call(batch_response, batch_response.result)
35
41
  else
36
42
  batch_response = BatchResponse.new(status, body)
37
- callback.call batch_response
43
+ log_batch_response(@operations[idx], batch_response)
44
+ callback && callback.call(batch_response)
38
45
  end
39
46
  end
40
47
  end
@@ -88,6 +95,19 @@ module Ecoportal
88
95
  callback: block_given? && Proc.new
89
96
  }
90
97
  end
98
+
99
+ private
100
+
101
+ def log_batch_response(operation, response)
102
+ level = response.success?? :debug : :warn
103
+ log(:info) { "BATCH #{operation[:method]} #{operation[:path]}" }
104
+ log(:info) { "Status #{response.status}" }
105
+ log(level) { "Response: #{JSON.pretty_generate(response.body)}" }
106
+ end
107
+
108
+ def log(level, &block)
109
+ @logger.send(level, &block) if @logger
110
+ end
91
111
  end
92
112
  end
93
113
  end
@@ -4,12 +4,12 @@ module Ecoportal
4
4
  class BatchResponse
5
5
  attr_reader :status, :body, :result
6
6
  def initialize(status, body, result = nil)
7
- @status = status
7
+ @status = HTTP::Response::Status.new(status)
8
8
  @body = body
9
9
  @result = result
10
10
  end
11
11
  def success?
12
- (0..299).include?(status)
12
+ status.success?
13
13
  end
14
14
  def each
15
15
  [*@result].each do |doc|
@@ -3,6 +3,8 @@ module Ecoportal
3
3
  module API
4
4
  module Common
5
5
  class Client
6
+ attr_accessor :logger
7
+
6
8
  def initialize(api_key:, version: "v1", host: "live.ecoportal.com", logger: nil)
7
9
  @version = version
8
10
  @api_key = api_key
@@ -13,7 +15,10 @@ module Ecoportal
13
15
  @base_uri = "https://#{host}/api/"
14
16
  end
15
17
  log(:info) { "#{version} client initialized pointing at #{host}" }
16
- log(:info) { "Api-key: #{@api_key}" }
18
+ if @api_key.nil? || @api_key.match(/\A\W*\z/)
19
+ log(:error) { "Api-key missing!" }
20
+ end
21
+ @response_logging_enabled = true
17
22
  end
18
23
 
19
24
  def log(level, &block)
@@ -68,6 +73,15 @@ module Ecoportal
68
73
  @base_uri+@version+path
69
74
  end
70
75
 
76
+ def without_response_logging(&block)
77
+ begin
78
+ @response_logging_enabled = false
79
+ yield self
80
+ ensure
81
+ @response_logging_enabled = true
82
+ end
83
+ end
84
+
71
85
  private
72
86
 
73
87
  def instrument(method, path, data = nil)
@@ -81,7 +95,7 @@ module Ecoportal
81
95
  end
82
96
  log(result.success?? :debug : :warn) do
83
97
  "Response: #{JSON.pretty_generate(result.body)}"
84
- end
98
+ end if @response_logging_enabled
85
99
  end
86
100
  end
87
101
  end
@@ -53,10 +53,13 @@ module Ecoportal
53
53
  end
54
54
 
55
55
  def batch
56
- operation = Common::BatchOperation.new("/people", person_class)
56
+ operation = Common::BatchOperation.new("/people", person_class, logger: @client.logger)
57
57
  yield operation
58
- @client.post("/people/batch", data: operation.as_json).tap do |response|
59
- operation.process_response(response)
58
+ # The batch operation is responsible for logging the output
59
+ @client.without_response_logging do
60
+ @client.post("/people/batch", data: operation.as_json).tap do |response|
61
+ operation.process_response(response)
62
+ end
60
63
  end
61
64
  end
62
65
 
@@ -1,5 +1,5 @@
1
1
  module Ecoportal
2
2
  module API
3
- VERSION = "0.3.4"
3
+ VERSION = "0.3.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecoportal-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tapio Saarinen