escobar 0.3.17 → 0.3.18

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
  SHA1:
3
- metadata.gz: d1eec142e7d9cc90816ab3da141a0f7b9d85743e
4
- data.tar.gz: 58f817772c53002d6c1fae98ffae6b8101b3dca8
3
+ metadata.gz: 24611335d033aad7733d4318d652f3738bb4dcad
4
+ data.tar.gz: f7c820a2e3a5941255d6ff0d94c1a9b87a7dc9c4
5
5
  SHA512:
6
- metadata.gz: b1e17a256feb2a81744d11f6bc236b6b629afa063ae71ed3c1ce0c80b0a716621f0053cf1335600e8f2d5750598768cdc30965f90ba3bc0cc3f897372430978e
7
- data.tar.gz: 298854043def94824ccca0c8831674a84d4189f0255891faba751920cf69526bc263dd396ace99d05f1c89f2644a698635d7fd26481a25b0a8ad86747394e5c2
6
+ metadata.gz: f23958ca6ab4c751494409806e3f315f7e5839d4ad2e0465068f9b675ab1b2f82cc0ad0537bc2384c74c8ac662a39be3609561d2914d09bc0e636198b14d2afb
7
+ data.tar.gz: 92328c38318c6cf546beda7b3d81c6adf0edd9c0dd5d251c400773145c83b3aca37d8e6d54681ff01330979ff21a809f4ed1395d41cf429a2e83c9747b31eac6
@@ -1,15 +1,28 @@
1
1
  module Escobar
2
2
  # Top-level client for heroku
3
3
  class Client
4
+ # TimeoutError class when API timeouts
5
+ class TimeoutError < StandardError
6
+ def self.wrap(err)
7
+ new(err)
8
+ end
9
+
10
+ attr_reader :cause
11
+ def initialize(err)
12
+ @cause = err
13
+ self.set_backtrace(err.backtrace)
14
+ end
15
+ end
16
+
4
17
  # Class for returning API errors to escobar clients
5
18
  class HTTPError < StandardError
6
19
  attr_accessor :body, :headers, :status
7
- def self.from_response(err, response)
20
+ def self.from_response(err)
8
21
  error = new("Error from Heroku API")
9
22
 
10
- error.body = response.body
11
- error.status = response.status
12
- error.headers = response.headers
23
+ error.body = err.response[:body]
24
+ error.headers = err.response[:headers]
25
+ error.status = err.response[:status]
13
26
 
14
27
  error.set_backtrace(err.backtrace)
15
28
  error
@@ -78,8 +78,6 @@ module Escobar
78
78
  def get(path)
79
79
  response = http_method(:get, path)
80
80
  JSON.parse(response.body)
81
- rescue StandardError => e
82
- raise Escobar::Client::HTTPError.from_response(e, response)
83
81
  end
84
82
 
85
83
  def accept_headers
@@ -87,36 +85,46 @@ module Escobar
87
85
  end
88
86
 
89
87
  def http_method(verb, path)
90
- client.send(verb) do |request|
91
- request.url path
92
- request.headers["Accept"] = accept_headers
93
- request.headers["Content-Type"] = "application/json"
94
- request.headers["Authorization"] = "token #{token}"
95
- request.options.timeout = Escobar.http_timeout
96
- request.options.open_timeout = Escobar.http_open_timeout
88
+ with_error_handling do
89
+ client.send(verb) do |request|
90
+ request.url path
91
+ request.headers["Accept"] = accept_headers
92
+ request.headers["Content-Type"] = "application/json"
93
+ request.headers["Authorization"] = "token #{token}"
94
+ request.options.timeout = Escobar.http_timeout
95
+ request.options.open_timeout = Escobar.http_open_timeout
96
+ end
97
97
  end
98
98
  end
99
99
 
100
100
  # rubocop:disable Metrics/AbcSize
101
101
  def post(path, body)
102
- response = client.post do |request|
103
- request.url path
104
- request.headers["Accept"] = accept_headers
105
- request.headers["Content-Type"] = "application/json"
106
- request.headers["Authorization"] = "token #{token}"
107
- request.options.timeout = Escobar.http_timeout
108
- request.options.open_timeout = Escobar.http_open_timeout
109
- request.body = body.to_json
102
+ with_error_handling do
103
+ response = client.post do |request|
104
+ request.url path
105
+ request.headers["Accept"] = accept_headers
106
+ request.headers["Content-Type"] = "application/json"
107
+ request.headers["Authorization"] = "token #{token}"
108
+ request.options.timeout = Escobar.http_timeout
109
+ request.options.open_timeout = Escobar.http_open_timeout
110
+ request.body = body.to_json
111
+ end
112
+
113
+ JSON.parse(response.body)
110
114
  end
111
-
112
- JSON.parse(response.body)
113
- rescue StandardError => e
114
- raise Escobar::Client::HTTPError.from_response(e, response)
115
115
  end
116
116
  # rubocop:enable Metrics/AbcSize
117
117
 
118
118
  private
119
119
 
120
+ def with_error_handling
121
+ yield
122
+ rescue Net::OpenTimeout, Faraday::TimeoutError => e
123
+ raise Escobar::Client::TimeoutError.wrap(e)
124
+ rescue Faraday::Error::ClientError => e
125
+ raise Escobar::Client::HTTPError.from_response(e)
126
+ end
127
+
120
128
  def client
121
129
  @client ||= Escobar.zipkin_enabled? ? zipkin_client : default_client
122
130
  end
@@ -3,7 +3,7 @@ module Escobar
3
3
  module Heroku
4
4
  # Top-level client for interacting with Heroku API
5
5
  class Client
6
- attr_reader :client, :token
6
+ attr_reader :token
7
7
  def initialize(token)
8
8
  @token = token
9
9
  end
@@ -20,56 +20,68 @@ module Escobar
20
20
  end
21
21
 
22
22
  def get(path, version = 3)
23
- response = client.get do |request|
24
- request.url path
25
- request_defaults(request, version)
26
- end
23
+ with_error_handling do
24
+ response = client.get do |request|
25
+ request.url path
26
+ request_defaults(request, version)
27
+ end
27
28
 
28
- JSON.parse(response.body)
29
- rescue StandardError => e
30
- raise Escobar::Client::HTTPError.from_response(e, response)
29
+ JSON.parse(response.body)
30
+ end
31
31
  end
32
32
 
33
33
  def get_range(path, range, version = 3)
34
- response = client.get do |request|
35
- request.url path
36
- request_defaults(request, version)
37
- request.headers["Range"] = range
38
- end
34
+ with_error_handling do
35
+ response = client.get do |request|
36
+ request.url path
37
+ request_defaults(request, version)
38
+ request.headers["Range"] = range
39
+ end
39
40
 
40
- JSON.parse(response.body)
41
- rescue StandardError => e
42
- raise Escobar::Client::HTTPError.from_response(e, response)
41
+ JSON.parse(response.body)
42
+ end
43
43
  end
44
44
 
45
45
  def post(path, body)
46
- response = client.post do |request|
47
- request.url path
48
- request_defaults(request)
49
- request.body = body.to_json
50
- end
46
+ with_error_handling do
47
+ response = client.post do |request|
48
+ request.url path
49
+ request_defaults(request)
50
+ request.body = body.to_json
51
+ end
51
52
 
52
- JSON.parse(response.body)
53
- rescue StandardError => e
54
- raise Escobar::Client::HTTPError.from_response(e, response)
53
+ JSON.parse(response.body)
54
+ end
55
55
  end
56
56
 
57
57
  def put(path, second_factor = nil)
58
- response = client.put do |request|
59
- request.url path
60
- request_defaults(request)
61
- if second_factor
62
- request.headers["Heroku-Two-Factor-Code"] = second_factor
58
+ with_error_handling do
59
+ response = client.put do |request|
60
+ request.url path
61
+ request_defaults(request)
62
+ if second_factor
63
+ request.headers["Heroku-Two-Factor-Code"] = second_factor
64
+ end
63
65
  end
64
- end
65
66
 
66
- JSON.parse(response.body)
67
- rescue StandardError => e
68
- raise Escobar::Client::HTTPError.from_response(e, response)
67
+ JSON.parse(response.body)
68
+ end
69
69
  end
70
70
 
71
71
  private
72
72
 
73
+ def with_error_handling
74
+ yield
75
+ rescue Net::OpenTimeout, Faraday::TimeoutError => e
76
+ raise Escobar::Client::TimeoutError.wrap(e)
77
+ rescue Faraday::Error::ClientError => e
78
+ raise Escobar::Client::HTTPError.from_response(e)
79
+ end
80
+
81
+ def client
82
+ @client ||= Escobar.zipkin_enabled? ? zipkin_client : default_client
83
+ end
84
+
73
85
  def request_defaults(request, version = 3)
74
86
  request.headers["Accept"] = heroku_accept_header(version)
75
87
  request.headers["Accept-Encoding"] = ""
@@ -85,10 +97,6 @@ module Escobar
85
97
  "application/vnd.heroku+json; version=#{version}"
86
98
  end
87
99
 
88
- def client
89
- @client ||= Escobar.zipkin_enabled? ? zipkin_client : default_client
90
- end
91
-
92
100
  def zipkin_client
93
101
  Faraday.new(url: "https://api.heroku.com") do |c|
94
102
  c.use :instrumentation
@@ -1,3 +1,3 @@
1
1
  module Escobar
2
- VERSION = "0.3.17".freeze
2
+ VERSION = "0.3.18".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: escobar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.17
4
+ version: 0.3.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Donohoe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-24 00:00:00.000000000 Z
11
+ date: 2017-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday