Skip to content

Commit

Permalink
Refactor merge_args
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed May 27, 2024
1 parent 96ebb4d commit 9edae37
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions lua/fittencode/rest/backend/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,20 @@ local function _spawn(args, on_success, on_error, on_exit)
end)
end

function M:get(url, headers, data, on_success, on_error)
local args = {
url,
}
-- NOTE: This mutates dst!
local function merge_args(args, headers, extra_args)
for _, v in ipairs(headers) do
table.insert(args, '-H')
table.insert(args, v)
end
vim.list_extend(args, DEFAULT_ARGS)
vim.list_extend(args, extra_args)
end

function M:get(url, headers, data, on_success, on_error)
local args = {
url,
}
merge_args(args, headers, DEFAULT_ARGS)
_spawn(args, on_success, on_error)
end

Expand All @@ -81,11 +86,7 @@ local function post_largedata(url, headers, encoded_data, on_success, on_error)
'@' .. path,
url,
}
for _, v in ipairs(headers) do
table.insert(args, '-H')
table.insert(args, v)
end
vim.list_extend(args, DEFAULT_ARGS)
merge_args(args, headers, DEFAULT_ARGS)
_spawn(args, on_success, on_error, function()
FS.delete(path)
end)
Expand All @@ -109,11 +110,7 @@ function M:post(url, headers, data, on_success, on_error)
encoded_data,
url,
}
for _, v in ipairs(headers) do
table.insert(args, '-H')
table.insert(args, v)
end
vim.list_extend(args, DEFAULT_ARGS)
merge_args(args, headers, DEFAULT_ARGS)
_spawn(args, on_success, on_error)
end

Expand Down

0 comments on commit 9edae37

Please sign in to comment.