Software /
code /
prosody
Changeset
10803:71d04bd6cadd
net.http: Return a Promise if no callback is given
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 06 May 2020 18:03:20 +0200 |
parents | 10802:c11f9cd6c761 |
children | 10804:4dc57789f51b |
files | net/http.lua |
diffstat | 1 files changed, 17 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/net/http.lua Wed May 06 12:48:09 2020 +0200 +++ b/net/http.lua Wed May 06 18:03:20 2020 +0200 @@ -12,6 +12,8 @@ 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 @@ 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