watchfor 0.17.0 → 0.17.2
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 +4 -4
- data/README.md +8 -2
- data/lib/watchfor/client.rb +32 -9
- data/lib/watchfor/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 54149ca58938489d3ce42a98e390e9a966d608b78760c41e28102b39109bc5ee
|
|
4
|
+
data.tar.gz: 165e61c5247b3ad06d6d20c92e698b91e17bf1a047fe0f189d7a2c4b0ab94788
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5b3a10eef3ea3b0bac1c0f238828fb8e7ae37e7c3b7c7b258c7e7cac387a30a81c0896a666c0668fdbc39f33ba63eb2078ef239f3cb465afc06a17262cf4b677
|
|
7
|
+
data.tar.gz: ee8ee1ae3c9f4ab35988f826a7608149d503ffdfa5e1f5d6d62660523436fd12fad9a2db96da21801f82cdd737f0ecd470845937334a3527745024161a1705f7
|
data/README.md
CHANGED
|
@@ -93,7 +93,8 @@ catalog = wf.diagnostics.list
|
|
|
93
93
|
# One check, optionally from a location you choose (plan-gated)
|
|
94
94
|
dns = wf.diagnostics.run("dns-lookup", "example.com",
|
|
95
95
|
options: { recordType: "A", resolver: "8.8.8.8" })
|
|
96
|
-
# A closed port
|
|
96
|
+
# A closed port, NXDOMAIN, expired certificate or unresolvable host does NOT
|
|
97
|
+
# raise — "success" is false and "error" says why ("result" keeps the raw answer):
|
|
97
98
|
puts "finding: #{dns['error']}" unless dns["success"]
|
|
98
99
|
puts "runs left this hour: #{dns['remaining']}"
|
|
99
100
|
|
|
@@ -125,7 +126,12 @@ Namespaces: `wf.organizations`, `wf.monitors`, `wf.alert_rules`, `wf.incidents`,
|
|
|
125
126
|
`wf.summary`, `wf.plan`, `wf.me`, `wf.locations`, `wf.monitor_types`,
|
|
126
127
|
`wf.incident_stats(period)`, `wf.report`, `wf.notifications`, `wf.activity`,
|
|
127
128
|
`wf.batch([...])`, `wf.metrics`. Errors raise
|
|
128
|
-
`Watchfor::Error` with `#status`, `#code`, `#message
|
|
129
|
+
`Watchfor::Error` with `#status`, `#code`, `#message` and `#retry_after`
|
|
130
|
+
(seconds, on a 429). A non-JSON answer (an HTML page from a wrong base URL
|
|
131
|
+
or a proxy) is a short `http_error` / `invalid_response` pointing at the
|
|
132
|
+
base URL. `base_url:` (and the CLI's `WATCHFOR_BASE_URL`) takes the origin
|
|
133
|
+
(`https://watchfor.io`) or the full base (`https://watchfor.io/api/v1`) —
|
|
134
|
+
the same value works for the npm and Python SDKs.
|
|
129
135
|
|
|
130
136
|
## Organizations
|
|
131
137
|
|
data/lib/watchfor/client.rb
CHANGED
|
@@ -12,15 +12,31 @@ module Watchfor
|
|
|
12
12
|
# Raised when the API returns a non-2xx response. Carries the HTTP
|
|
13
13
|
# +status+ and the API's structured +code+ and +message+.
|
|
14
14
|
class Error < StandardError
|
|
15
|
-
attr_reader :status, :code
|
|
15
|
+
attr_reader :status, :code, :retry_after
|
|
16
16
|
|
|
17
|
-
def initialize(status, code, message)
|
|
17
|
+
def initialize(status, code, message, retry_after = nil)
|
|
18
18
|
@status = status
|
|
19
19
|
@code = code
|
|
20
|
+
@retry_after = retry_after
|
|
20
21
|
super("[#{status}] #{code}: #{message}")
|
|
21
22
|
end
|
|
22
23
|
end
|
|
23
24
|
|
|
25
|
+
# The API base, whichever form was given: "https://watchfor.io" and
|
|
26
|
+
# "https://watchfor.io/api/v1" (with or without a trailing slash) both
|
|
27
|
+
# become "https://watchfor.io/api/v1" — the npm SDK takes the origin, so
|
|
28
|
+
# one WATCHFOR_BASE_URL works for all three SDKs.
|
|
29
|
+
def self.normalize_base_url(base_url)
|
|
30
|
+
base = base_url.to_s.strip.sub(%r{/+\z}, "")
|
|
31
|
+
base = "https://watchfor.io" if base.empty?
|
|
32
|
+
base.downcase.end_with?("/api/v1") ? base : "#{base}/api/v1"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.not_api_message(status, url)
|
|
36
|
+
"HTTP #{status} from #{url} — the response is not WatchFor API JSON. " \
|
|
37
|
+
"Is the base URL right? Use the origin, e.g. https://watchfor.io (WATCHFOR_BASE_URL)."
|
|
38
|
+
end
|
|
39
|
+
|
|
24
40
|
# Client for the WatchFor REST API (/api/v1). Zero dependencies.
|
|
25
41
|
#
|
|
26
42
|
# wf = Watchfor::Client.new(api_key: "wf_live_...")
|
|
@@ -44,7 +60,7 @@ module Watchfor
|
|
|
44
60
|
raise ArgumentError, "api_key is required" if api_key.nil? || api_key.empty?
|
|
45
61
|
|
|
46
62
|
@api_key = api_key
|
|
47
|
-
@base_url =
|
|
63
|
+
@base_url = Watchfor.normalize_base_url(base_url)
|
|
48
64
|
@timeout = timeout
|
|
49
65
|
org = organization.to_s.strip
|
|
50
66
|
@organization = org.empty? ? nil : org
|
|
@@ -103,15 +119,22 @@ module Watchfor
|
|
|
103
119
|
end
|
|
104
120
|
|
|
105
121
|
raw = res.body.to_s
|
|
106
|
-
|
|
122
|
+
status = res.code.to_i
|
|
123
|
+
return raw if text && status.between?(200, 299)
|
|
107
124
|
|
|
108
|
-
|
|
109
|
-
|
|
125
|
+
blank = raw.strip.empty?
|
|
126
|
+
parsed = blank ? nil : (JSON.parse(raw) rescue :not_json)
|
|
127
|
+
unless status.between?(200, 299)
|
|
110
128
|
err = parsed.is_a?(Hash) ? (parsed["error"] || {}) : {}
|
|
111
|
-
|
|
112
|
-
|
|
129
|
+
fallback = parsed == :not_json ? Watchfor.not_api_message(status, uri) : "HTTP #{res.code}"
|
|
130
|
+
retry_after = res["Retry-After"] ? Float(res["Retry-After"], exception: false) : nil
|
|
131
|
+
raise Error.new(status, err["code"] || "http_error", err["message"] || fallback, retry_after)
|
|
113
132
|
end
|
|
114
|
-
|
|
133
|
+
# An HTML page from a wrong base URL or a proxy: say so instead of
|
|
134
|
+
# returning nil.
|
|
135
|
+
raise Error.new(status, "invalid_response", Watchfor.not_api_message(status, uri)) if parsed == :not_json
|
|
136
|
+
|
|
137
|
+
parsed.nil? ? { "ok" => true, "status" => status } : parsed
|
|
115
138
|
end
|
|
116
139
|
|
|
117
140
|
# Top-level reads
|
data/lib/watchfor/version.rb
CHANGED