Software /
code /
prosody
Comparison
util/prosodyctl/check.lua @ 11826:e1c4cc5d0ef8
prosodyctl: Use HTTP client in promise mode for connectivity check
Feels a bit cleaner to hide away the async.waiter() and return value
handling. Also line count reduction!
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 30 Sep 2021 17:33:49 +0200 |
parent | 11807:f5295e59ca78 |
child | 11827:2359519260ec |
comparison
equal
deleted
inserted
replaced
11823:36a7a3137d41 | 11826:e1c4cc5d0ef8 |
---|---|
5 local dependencies = require "util.dependencies"; | 5 local dependencies = require "util.dependencies"; |
6 local socket = require "socket"; | 6 local socket = require "socket"; |
7 local jid_split = require "util.jid".prepped_split; | 7 local jid_split = require "util.jid".prepped_split; |
8 local modulemanager = require "core.modulemanager"; | 8 local modulemanager = require "core.modulemanager"; |
9 | 9 |
10 local function check_api(check_type, target_host) | 10 local function check_ojn(check_type, target_host) |
11 local async = require "util.async"; | |
12 local wait, done = async.waiter(); | |
13 local http = require "net.http"; -- .new({}); | 11 local http = require "net.http"; -- .new({}); |
14 local urlencode = require "util.http".urlencode; | 12 local urlencode = require "util.http".urlencode; |
15 local json = require "util.json"; | 13 local json = require "util.json"; |
16 | 14 |
17 local ok = false; | 15 local response, err = async.wait_for(http.request( |
18 local err = nil; | |
19 local decoded_body = nil; | |
20 | |
21 http.request( | |
22 ("https://observe.jabber.network/api/v1/check/%s"):format(urlencode(check_type)), | 16 ("https://observe.jabber.network/api/v1/check/%s"):format(urlencode(check_type)), |
23 { | 17 { |
24 method="POST", | 18 method="POST", |
25 headers={["Accept"] = "application/json"; ["Content-Type"] = "application/json"}, | 19 headers={["Accept"] = "application/json"; ["Content-Type"] = "application/json"}, |
26 body=json.encode({target=target_host}), | 20 body=json.encode({target=target_host}), |
27 }, | 21 })); |
28 function (body, code) | 22 |
29 if code ~= 200 then | 23 if not response then |
30 err = ("API replied with non-200 code: %d"):format(code) | 24 return false, err; |
31 else | 25 end |
32 decoded_body, err = json.decode(body); | 26 |
33 if decoded_body == nil then | 27 if response.code ~= 200 then |
34 err = ("Failed to parse API JSON: %s"):format(err) | 28 return false, ("API replied with non-200 code: %d"):format(response.code); |
35 else | 29 end |
36 ok = true | 30 |
37 end | 31 local decoded_body, err = json.decode(response.body); |
38 end | 32 if decoded_body == nil then |
39 done(); | 33 return false, ("Failed to parse API JSON: %s"):format(err) |
40 end | |
41 ); | |
42 wait(); | |
43 | |
44 if not ok then | |
45 return false, err | |
46 end | 34 end |
47 | 35 |
48 local success = decoded_body["success"]; | 36 local success = decoded_body["success"]; |
49 return success == true, nil; | 37 return success == true, nil; |
50 end | 38 end |