diff options
author | Squibid <me@zacharyscheiman.com> | 2024-08-03 22:42:15 -0400 |
---|---|---|
committer | Squibid <me@zacharyscheiman.com> | 2024-08-03 22:42:15 -0400 |
commit | d400da457ea15c58319a9f200f49c6502787e17c (patch) | |
tree | 2685cc46ab516ef3c086ca8fd8aa526b11a27618 /utils/curl.lua | |
download | jellies-and-jams-d400da457ea15c58319a9f200f49c6502787e17c.tar.gz jellies-and-jams-d400da457ea15c58319a9f200f49c6502787e17c.tar.bz2 jellies-and-jams-d400da457ea15c58319a9f200f49c6502787e17c.zip |
initial commit
Diffstat (limited to '')
-rw-r--r-- | utils/curl.lua | 41 |
1 files changed, 41 insertions, 0 deletions
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 |