From d400da457ea15c58319a9f200f49c6502787e17c Mon Sep 17 00:00:00 2001 From: Squibid Date: Sat, 3 Aug 2024 22:42:15 -0400 Subject: initial commit --- utils/curl.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 utils/curl.lua (limited to 'utils/curl.lua') diff --git a/utils/curl.lua b/utils/curl.lua new file mode 100644 index 0000000..532d0bd --- /dev/null +++ b/utils/curl.lua @@ -0,0 +1,41 @@ +local mp = require('mp') +local utils = require('mp.utils') +local msg = require('mp.msg') + +local M = {} + +--- run a curl request +---@param cmd table list of curl args +---@param method? string +---@return table? json, number? httpcode +function M.request(cmd, method) + if type(cmd) == "string" then + cmd = { cmd } + end + + table.insert(cmd, 1, '%{http_code}') + table.insert(cmd, 1, "-w") + if method then + table.insert(cmd, 1, method) + table.insert(cmd, 1, "-X") + end + table.insert(cmd, 1, "curl") + + local r = mp.command_native({ + name = "subprocess", + capture_stdout = true, + capture_stderr = true, + playback_only = false, + args = cmd + }) + + local json, err, code = utils.parse_json(r.stdout, true) + if not json then + msg.error(string.format("failed to parse json: %s", err)) + return + end + + return json, tonumber(code) +end + +return M -- cgit v1.2.1