mirror of
https://github.com/macocianradu/nvim-http.git
synced 2026-07-16 02:48:46 +00:00
53 lines
1.4 KiB
Lua
53 lines
1.4 KiB
Lua
local M = {}
|
|
|
|
local output_fields = {"http_code", "time_total"}
|
|
|
|
function M.get_current_line()
|
|
return vim.api.nvim_get_current_line()
|
|
end
|
|
|
|
function M.parse_line(line)
|
|
local method, url = line:match"^%s*(%S+)%s+(.+)"
|
|
return method, url
|
|
end
|
|
|
|
function M.execute(method, url, fields)
|
|
local outFormat = "\\n--metadata--\\n"
|
|
for _, field in pairs(fields) do
|
|
outFormat = outFormat .. field .. ":%{" .. field .. "}\\n"
|
|
end
|
|
local result = vim.system({"curl", url, "-X", method, "-w", outFormat}, {
|
|
text = true,
|
|
}):wait()
|
|
return result
|
|
end
|
|
|
|
function M.parse_output(response, fields)
|
|
local body, metadata = response:match"(.-)%s*\n%-%-metadata%-%-\n(.*)"
|
|
local result = {
|
|
body = body or ""
|
|
}
|
|
|
|
for _, field in pairs(fields) do
|
|
local value = metadata:match(field .. ":([^\n]*)")
|
|
if value then
|
|
result[field] = value
|
|
end
|
|
end
|
|
|
|
return result
|
|
end
|
|
|
|
function M.run_http_under_cursor()
|
|
local line = M.get_current_line()
|
|
vim.notify("Current line: " .. line, vim.log.levels.TRACE)
|
|
local method, url = M.parse_line(line)
|
|
vim.notify("Parsed - Method/Command: " .. tostring(method) .. ", URL/Args: " .. tostring(url), vim.log.levels.TRACE)
|
|
local response = M.execute(method, url, output_fields)
|
|
vim.notify("Response: " .. response.stdout, vim.log.levels.TRACE)
|
|
local result = M.parse_output(response.stdout, output_fields)
|
|
return result
|
|
end
|
|
|
|
return M
|