rename
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
local curl = require("plenary.curl")
|
||||
local M = {}
|
||||
|
||||
---@class eta.Session
|
||||
---@field auth string
|
||||
---@field base_url string
|
||||
|
||||
---@param method `get` | `post`
|
||||
---@param session eta.Session
|
||||
---@param endpoint string
|
||||
---@param params {[string]: string}
|
||||
---@returns table | nil
|
||||
M.request = function(method, session, endpoint, params)
|
||||
local param_list = {}
|
||||
for param_name, param_value in pairs(params) do
|
||||
table.insert(param_list, param_name .. "=" .. param_value)
|
||||
end
|
||||
local url = session.base_url .. endpoint
|
||||
if #param_list then
|
||||
url = url .. "?" .. table.concat(param_list, "&")
|
||||
end
|
||||
local resp = nil
|
||||
if method == "get" then
|
||||
resp = curl.get(url, {headers = {
|
||||
-- ['PRIVATE-TOKEN'] = session.auth,
|
||||
['Authorization'] = session.auth,
|
||||
["content-type"] = "application/json",
|
||||
}})
|
||||
elseif method == "post" then
|
||||
resp = curl.post(url, {headers = {
|
||||
-- ['PRIVATE-TOKEN'] = session.auth,
|
||||
['Authorization'] = session.auth,
|
||||
["content-type"] = "application/json",
|
||||
}})
|
||||
else
|
||||
return nil
|
||||
end
|
||||
|
||||
if resp.status == 200 then
|
||||
local prjs = vim.json.decode(resp.body)
|
||||
for _, prj in ipairs(prjs) do
|
||||
local fcmd = "echo '" .. vim.json.encode(prj) .. "' | jq"
|
||||
local fhandle, _ = io.popen(fcmd, 'r')
|
||||
if not fhandle then goto continue end
|
||||
local formatted = fhandle:read("*a")
|
||||
fhandle:close()
|
||||
|
||||
prj.text = prj.path_with_namespace
|
||||
prj.preview = {
|
||||
ft = "json",
|
||||
text = formatted
|
||||
}
|
||||
|
||||
::continue::
|
||||
end
|
||||
return prjs
|
||||
end
|
||||
|
||||
print("failed http request: " .. tostring(resp.status) .. " (" .. resp.body .. ", " .. url .. ")")
|
||||
return nil
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user