Software /
code /
prosody
Diff
net/http.lua @ 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 |
parent | 10464:8d3acf16c404 |
child | 10995:e18a913aed2d |
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