From 58ff794d9252d3e7368e6254ed784dce9f2e6ffe Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Tue, 28 Jun 2016 23:44:20 +0300 Subject: [PATCH] Improve loading of HTTP client factories This introduces a get_http_factory() function which will try to load a HTTP client library according to the following logic: 1. If the MATRIX_API_HTTP_CLIENT environment variable is defined, it is taken as the name of a HTTP client library. 2. Otherwise, if the function paramter is non-nil it is used as the name of the HTTP client library to load. 3. Otherwise, the available HTTP client libraries are tried in order, and the first one which can be require()'d successfully will be used. For now this has only the "chttp" client. --- matrix/api.lua | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/matrix/api.lua b/matrix/api.lua index aaab7c4..fe7cb35 100644 --- a/matrix/api.lua +++ b/matrix/api.lua @@ -25,6 +25,33 @@ local function get_debug_log_function() end end +local get_http_factory = function (http_factory) + -- The environment variable has precedence, as it is used to aid debugging. + do + local env_value = os.getenv("MATRIX_API_HTTP_CLIENT") + if env_value and #env_value > 0 then + http_factory = env_value + end + end + -- Try to import supplied HTTP client libraries, in order of preference. + local tries = http_factory and { http_factory } or { "chttp" } + local errors = {} + for i, http_factory in ipairs(tries) do + local ok, factory = pcall(require, "matrix.factory." .. http_factory) + if ok then + get_http_factory = function () return factory end + return get_http_factory() + end + errors[i] = factory + end + local errmsg = { "Could not load any HTTP client library:" } + for i, name in pairs(tries) do + errmsg[#errmsg + 1] = "--- Loading '" .. name .. "'" + errmsg[#errmsg + 1] = errors[i] + end + error(table.concat(errmsg, "\n")) +end + local API = {} API.__name = "matrix.api" @@ -37,7 +64,7 @@ setmetatable(API, { __call = function (self, base_url, token, http_factory) txn_id = 0, api_path = "/_matrix/client/r0", -- TODO: De-hardcode _log = get_debug_log_function(), - _http = require("matrix.factory." .. (http_factory or "chttp"))(), + _http = get_http_factory(http_factory)(), }, API) end })