添加 util_http.fetch, 支持超时和 netled 闪烁

This commit is contained in:
Mizore 2023-01-31 17:41:03 +08:00
parent 53e4120f73
commit 82ec5b030f
3 changed files with 56 additions and 7 deletions

View File

@ -54,7 +54,9 @@ gpio.setup(
gpio.PULLUP
)
-- 加载模块
config = require "config"
util_http = require "util_http"
util_netled = require "util_netled"
util_mobile = require "util_mobile"
util_location = require "util_location"

47
script/util_http.lua Normal file
View File

@ -0,0 +1,47 @@
local util_http = {}
-- 用于生成 http 请求的 id
local http_count = 0
-- 记录正在运行的 http 请求数量
local http_running_count = 0
--- 对 http.request 的封装
-- @param timeout 超时时间(单位: 毫秒)
-- @param method 请求方法
-- @param url 请求地址
-- @param headers 请求头
-- @param body 请求体
function util_http.fetch(timeout, method, url, headers, body)
timeout = timeout or 1000 * 25
local opts = {timeout = timeout}
http_count = http_count + 1
http_running_count = http_running_count + 1
local id = "http_" .. http_count
local res_code, res_headers, res_body = -99, {}, ""
util_netled.blink(50, 50)
sys.taskInit(
function()
log.debug("util_http.fetch", "开始请求", "id:", id)
res_code, res_headers, res_body = http.request(method, url, headers, body, opts).wait()
log.debug("util_http.fetch", "请求结束", "id:", id, "code:", res_code)
sys.publish(id)
end
)
local result = sys.waitUntil(id, timeout + 1000 * 25)
if not result or res_code == -8 then
log.warn("util_http.fetch", "请求超时", "id:", id)
end
http_running_count = http_running_count - 1
if http_running_count == 0 then
util_netled.blink()
end
return res_code, res_headers, res_body
end
return util_http

View File

@ -25,7 +25,7 @@ local function notifyToTelegram(msg)
}
log.info("util_notify.notifyToTelegram", "POST", config.TELEGRAM_PROXY_API)
return http.request("POST", config.TELEGRAM_PROXY_API, header, msg).wait()
return util_http.fetch(nil, "POST", config.TELEGRAM_PROXY_API, header, msg)
end
-- 发送到 pushdeer
@ -49,7 +49,7 @@ local function notifyToPushDeer(msg)
}
log.info("util_notify.notifyToPushDeer", "POST", config.PUSHDEER_API)
return http.request("POST", config.PUSHDEER_API, header, urlencodeTab(body)).wait()
return util_http.fetch(nil, "POST", config.PUSHDEER_API, header, urlencodeTab(body))
end
-- 发送到 bark
@ -72,7 +72,7 @@ local function notifyToBark(msg)
local url = config.BARK_API .. "/" .. config.BARK_KEY
log.info("util_notify.notifyToBark", "POST", url)
return http.request("POST", url, header, urlencodeTab(body)).wait()
return util_http.fetch(nil, "POST", url, header, urlencodeTab(body))
end
-- 发送到 dingtalk
@ -96,7 +96,7 @@ local function notifyToDingTalk(msg)
json_data = string.gsub(json_data, "\\b", "\\n")
log.info("util_notify.notifyToDingTalk", "POST", config.DINGTALK_WEBHOOK, json_data)
return http.request("POST", config.DINGTALK_WEBHOOK, header, json_data).wait()
return util_http.fetch(nil, "POST", config.DINGTALK_WEBHOOK, header, json_data)
end
-- 发送到 feishu
@ -120,7 +120,7 @@ local function notifyToFeishu(msg)
json_data = string.gsub(json_data, "\\b", "\\n")
log.info("util_notify.notifyToFeishu", "POST", config.FEISHU_WEBHOOK, json_data)
return http.request("POST", config.FEISHU_WEBHOOK, header, json_data).wait()
return util_http.fetch(nil, "POST", config.FEISHU_WEBHOOK, header, json_data)
end
-- 发送到 wecom
@ -144,7 +144,7 @@ local function notifyToWeCom(msg)
json_data = string.gsub(json_data, "\\b", "\\n")
log.info("util_notify.notifyToWeCom", "POST", config.WECOM_WEBHOOK, json_data)
return http.request("POST", config.WECOM_WEBHOOK, header, json_data).wait()
return util_http.fetch(nil, "POST", config.WECOM_WEBHOOK, header, json_data)
end
-- 发送到 next-smtp-proxy
@ -189,7 +189,7 @@ local function notifyToNextSmtpProxy(msg)
}
log.info("util_notify.notifyToNextSmtpProxy", "POST", config.NEXT_SMTP_PROXY_API, urlencodeTab(body))
return http.request("POST", config.NEXT_SMTP_PROXY_API, header, urlencodeTab(body)).wait()
return util_http.fetch(nil, "POST", config.NEXT_SMTP_PROXY_API, header, urlencodeTab(body))
end
function util_notify.send(msg)