pager_duty-connection 2.2.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pager_duty/connection/version.rb +1 -1
- data/lib/pager_duty/connection.rb +85 -14
- data/pager_duty-connection.gemspec +1 -2
- metadata +12 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b054f9ef807fb4106210b76a8a04db02b52599a4c617df4d43f06b3a5f63c67c
|
4
|
+
data.tar.gz: 76f316fd0c10badb260c3353926f9ceb540c5af3b0ae33b597641e0f482ea2a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99dc2a3a2ba5eedb69ac4ac1f758df7bb54a7a632adb9af8bc4a3093bcc8b4638d637f63dd49221838b81ccd7badb51ba68d9f6cdc12ad1a0c0ed5a9aedcc558
|
7
|
+
data.tar.gz: '08a739a697b4f429e42d584ece06a5d7176fcb2099be85c6d1cdc0c79763bbaca2ff6bfc04ce5a17fe9fcc1102545bec6fb54c7a93d39f948a7ed62359d48373'
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'faraday'
|
2
|
-
require '
|
2
|
+
require 'hashie'
|
3
3
|
require 'active_support'
|
4
4
|
require 'active_support/core_ext'
|
5
5
|
require 'active_support/time_with_zone'
|
@@ -12,15 +12,39 @@ module PagerDuty
|
|
12
12
|
API_VERSION = 2
|
13
13
|
API_PREFIX = "https://api.pagerduty.com/"
|
14
14
|
|
15
|
-
class FileNotFoundError < RuntimeError
|
16
|
-
|
15
|
+
class FileNotFoundError < RuntimeError; end
|
16
|
+
|
17
|
+
class ApiError < RuntimeError; end
|
18
|
+
|
19
|
+
class RateLimitError < RuntimeError; end
|
20
|
+
|
21
|
+
class UnauthorizedError < RuntimeError; end
|
22
|
+
|
23
|
+
class ForbiddenError < RuntimeError; end
|
17
24
|
|
18
|
-
class
|
25
|
+
class RaiseUnauthorizedOn401 < Faraday::Middleware
|
26
|
+
def call(env)
|
27
|
+
response = @app.call(env)
|
28
|
+
if response.status == 401
|
29
|
+
raise PagerDuty::Connection::UnauthorizedError, response.env[:url].to_s
|
30
|
+
else
|
31
|
+
response
|
32
|
+
end
|
33
|
+
end
|
19
34
|
end
|
20
35
|
|
21
|
-
class
|
36
|
+
class RaiseForbiddenOn403 < Faraday::Middleware
|
37
|
+
def call(env)
|
38
|
+
response = @app.call(env)
|
39
|
+
if response.status == 403
|
40
|
+
raise PagerDuty::Connection::ForbiddenError, response.env[:url].to_s
|
41
|
+
else
|
42
|
+
response
|
43
|
+
end
|
44
|
+
end
|
22
45
|
end
|
23
46
|
|
47
|
+
|
24
48
|
class RaiseFileNotFoundOn404 < Faraday::Middleware
|
25
49
|
def call(env)
|
26
50
|
response = @app.call env
|
@@ -40,10 +64,13 @@ module PagerDuty
|
|
40
64
|
message = "Got HTTP #{response.status}: #{response.reason_phrase}\nFrom #{url}"
|
41
65
|
|
42
66
|
if error = response.body
|
43
|
-
|
44
|
-
|
67
|
+
begin
|
68
|
+
# TODO May Need to check error.errors too
|
69
|
+
message += "\n#{JSON.parse(error)}"
|
70
|
+
rescue JSON::ParserError
|
71
|
+
message += "\n#{error}"
|
72
|
+
end
|
45
73
|
end
|
46
|
-
|
47
74
|
raise ApiError, message
|
48
75
|
else
|
49
76
|
response
|
@@ -67,9 +94,11 @@ module PagerDuty
|
|
67
94
|
def call(env)
|
68
95
|
|
69
96
|
body = env[:body]
|
70
|
-
|
71
|
-
|
72
|
-
|
97
|
+
unless body.nil?
|
98
|
+
TIME_KEYS.each do |key|
|
99
|
+
if body.has_key?(key)
|
100
|
+
body[key] = body[key].iso8601 if body[key].respond_to?(:iso8601)
|
101
|
+
end
|
73
102
|
end
|
74
103
|
end
|
75
104
|
|
@@ -77,7 +106,7 @@ module PagerDuty
|
|
77
106
|
end
|
78
107
|
end
|
79
108
|
|
80
|
-
class ParseTimeStrings < Faraday::
|
109
|
+
class ParseTimeStrings < Faraday::Middleware
|
81
110
|
TIME_KEYS = %w(
|
82
111
|
at
|
83
112
|
created_at
|
@@ -108,6 +137,10 @@ module PagerDuty
|
|
108
137
|
pending_actions
|
109
138
|
)
|
110
139
|
|
140
|
+
def on_complete(env)
|
141
|
+
parse(env[:body])
|
142
|
+
end
|
143
|
+
|
111
144
|
def parse(body)
|
112
145
|
case body
|
113
146
|
when Hash, ::Hashie::Mash
|
@@ -150,13 +183,34 @@ module PagerDuty
|
|
150
183
|
end
|
151
184
|
end
|
152
185
|
|
186
|
+
class Mashify < Faraday::Middleware
|
187
|
+
def on_complete(env)
|
188
|
+
env[:body] = parse(env[:body])
|
189
|
+
end
|
190
|
+
|
191
|
+
def parse(body)
|
192
|
+
case body
|
193
|
+
when Hash
|
194
|
+
::Hashie::Mash.new(body)
|
195
|
+
when Array
|
196
|
+
body.map { |item| parse(item) }
|
197
|
+
else
|
198
|
+
body
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
153
203
|
def initialize(token, token_type: :Token, url: API_PREFIX, debug: false)
|
154
204
|
@connection = Faraday.new do |conn|
|
155
205
|
conn.url_prefix = url
|
156
206
|
|
157
207
|
case token_type
|
158
208
|
when :Token
|
159
|
-
|
209
|
+
if faraday_v1?
|
210
|
+
conn.request :token_auth, token
|
211
|
+
else
|
212
|
+
conn.request :authorization, 'Token', token
|
213
|
+
end
|
160
214
|
when :Bearer
|
161
215
|
conn.request :authorization, 'Bearer', token
|
162
216
|
else raise ArgumentError, "invalid token_type: #{token_type.inspect}"
|
@@ -170,7 +224,7 @@ module PagerDuty
|
|
170
224
|
|
171
225
|
# json back, mashify it
|
172
226
|
conn.use ParseTimeStrings
|
173
|
-
conn.
|
227
|
+
conn.use Mashify
|
174
228
|
conn.response :json
|
175
229
|
conn.response :logger, ::Logger.new(STDOUT), bodies: true if debug
|
176
230
|
|
@@ -180,12 +234,20 @@ module PagerDuty
|
|
180
234
|
conn.use RaiseApiErrorOnNon200
|
181
235
|
conn.use RaiseFileNotFoundOn404
|
182
236
|
conn.use RaiseRateLimitOn429
|
237
|
+
conn.use RaiseForbiddenOn403
|
238
|
+
conn.use RaiseUnauthorizedOn401
|
183
239
|
|
184
240
|
conn.adapter Faraday.default_adapter
|
185
241
|
end
|
186
242
|
end
|
187
243
|
|
188
244
|
def get(path, request = {})
|
245
|
+
# The run_request() method body argument defaults to {}, which is incorrect for GET requests
|
246
|
+
# https://github.com/technicalpickles/pager_duty-connection/issues/56
|
247
|
+
# NOTE: PagerDuty support discourages GET requests with bodies, but not throwing an ArgumentError to prevent breaking
|
248
|
+
# corner-case implementations.
|
249
|
+
request[:body] = nil if !request[:body]
|
250
|
+
|
189
251
|
# paginate anything being 'get'ed, because the offset/limit isn't intuitive
|
190
252
|
request[:query_params] = {} if !request[:query_params]
|
191
253
|
page = request[:query_params].fetch(:page, 1).to_i
|
@@ -193,6 +255,7 @@ module PagerDuty
|
|
193
255
|
offset = (page - 1) * limit
|
194
256
|
|
195
257
|
query_params = request[:query_params].merge(offset: offset, limit: limit)
|
258
|
+
query_params.delete(:page)
|
196
259
|
|
197
260
|
run_request(:get, path, **request.merge(query_params: query_params))
|
198
261
|
end
|
@@ -211,6 +274,14 @@ module PagerDuty
|
|
211
274
|
|
212
275
|
private
|
213
276
|
|
277
|
+
def faraday_v1?
|
278
|
+
faraday_version < Gem::Version.new("2")
|
279
|
+
end
|
280
|
+
|
281
|
+
def faraday_version
|
282
|
+
@faraday_version ||= Gem.loaded_specs["faraday"].version
|
283
|
+
end
|
284
|
+
|
214
285
|
def run_request(method, path, body: {}, headers: {}, query_params: {})
|
215
286
|
path = path.gsub(/^\//, '') # strip leading slash, to make sure relative things happen on the connection
|
216
287
|
|
@@ -17,8 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
|
20
|
-
gem.add_dependency "faraday", "
|
21
|
-
gem.add_dependency "faraday_middleware", "~> 1.0"
|
20
|
+
gem.add_dependency "faraday", ">= 1.10", "< 3"
|
22
21
|
gem.add_dependency "activesupport", ">= 3.2", "< 8.0"
|
23
22
|
gem.add_dependency "hashie", ">= 1.2"
|
24
23
|
|
metadata
CHANGED
@@ -1,43 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pager_duty-connection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Nichols
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
17
|
+
- - ">="
|
25
18
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
27
|
-
-
|
28
|
-
name: faraday_middleware
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
19
|
+
version: '1.10'
|
20
|
+
- - "<"
|
32
21
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
22
|
+
version: '3'
|
34
23
|
type: :runtime
|
35
24
|
prerelease: false
|
36
25
|
version_requirements: !ruby/object:Gem::Requirement
|
37
26
|
requirements:
|
38
|
-
- - "
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.10'
|
30
|
+
- - "<"
|
39
31
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
32
|
+
version: '3'
|
41
33
|
- !ruby/object:Gem::Dependency
|
42
34
|
name: activesupport
|
43
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -124,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
116
|
- !ruby/object:Gem::Version
|
125
117
|
version: '0'
|
126
118
|
requirements: []
|
127
|
-
rubygems_version: 3.4.
|
119
|
+
rubygems_version: 3.4.18
|
128
120
|
signing_key:
|
129
121
|
specification_version: 4
|
130
122
|
summary: Written with the power of faraday, pager_duty-connection tries to be a simple
|