aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2020-05-06 18:03:20 +0200
committerKim Alvefur <zash@zash.se>2020-05-06 18:03:20 +0200
commit7d924c49ac817353980f9505ed83845ce0b8377c (patch)
tree78d46d438d2d93ae48e5e09cac96abdf9ceae681
parent92c97b036113d8b608fd3c85e7deb8712c7004e6 (diff)
downloadprosody-7d924c49ac817353980f9505ed83845ce0b8377c.tar.gz
prosody-7d924c49ac817353980f9505ed83845ce0b8377c.zip
net.http: Return a Promise if no callback is given
-rw-r--r--net/http.lua18
1 files changed, 17 insertions, 1 deletions
diff --git a/net/http.lua b/net/http.lua
index 8ca30db2..5fbed4fe 100644
--- a/net/http.lua
+++ b/net/http.lua
@@ -12,6 +12,8 @@ local httpstream_new = require "net.http.parser".new;
local util_http = require "util.http";
local events = require "util.events";
local verify_identity = require"util.x509".verify_identity;
+local promise = require "util.promise";
+local errors = require "util.error";
local basic_resolver = require "net.resolvers.basic";
local connect = require "net.connect".connect;
@@ -270,7 +272,21 @@ end
local function new(options)
local http = {
options = options;
- request = request;
+ request = function (self, u, ex, callback)
+ if callback ~= nil then
+ return request(self, u, ex, callback);
+ else
+ return promise.new(function (resolve, reject)
+ request(self, u, ex, function (body, code, a, b)
+ if code == 0 then
+ reject(errors.new(body, { request = a }));
+ else
+ resolve({ request = b, response = a });
+ end
+ end);
+ end);
+ end
+ end;
new = options and function (new_options)
local final_options = {};
for k, v in pairs(options) do final_options[k] = v; end