Skip to content

Commit

Permalink
Refactor post
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed May 26, 2024
1 parent 42177e3 commit 2e8e566
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
8 changes: 6 additions & 2 deletions lua/fittencode/client/fitten_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ function M:login(username, password, on_success, on_error)
password = password,
}
Promise:new(function(resolve, reject)
self.rest:post(URL.LOGIN, data, function(response)
self.rest:post(URL.LOGIN, {
'Content-Type: application/json',
}, data, function(response)
resolve(response)
end, function()
schedule(on_error)
Expand Down Expand Up @@ -77,7 +79,9 @@ function M:generate_one_stage(api_key, params, on_success, on_error)
local data = params

Promise:new(function(resolve, _)
self.rest:post(url, data, function(response)
self.rest:post(url, {
'Content-Type: application/json',
}, data, function(response)
resolve(response)
end, function()
schedule(on_error, NetworkError:new())
Expand Down
18 changes: 11 additions & 7 deletions lua/fittencode/rest/backend/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function M:get(url, headers, data, on_success, on_error)
_spawn(args, on_success, on_error)
end

local function post_largedata(url, encoded_data, on_success, on_error)
local function post_largedata(url, headers, encoded_data, on_success, on_error)
Promise:new(function(resolve, reject)
FS.write_temp_file(encoded_data, function(_, path)
resolve(path)
Expand All @@ -77,38 +77,42 @@ local function post_largedata(url, encoded_data, on_success, on_error)
local args = {
'-X',
'POST',
'-H',
'Content-Type: application/json',
'-d',
'@' .. path,
url,
}
for _, v in ipairs(headers) do
table.insert(args, '-H')
table.insert(args, v)
end
vim.list_extend(args, DEFAULT_ARGS)
_spawn(args, on_success, on_error, function()
FS.delete(path)
end)
end)
end

function M:post(url, data, on_success, on_error)
function M:post(url, headers, data, on_success, on_error)
local success, encoded_data = pcall(fn.json_encode, data)
if not success then
Log.error('Failed to encode data: {}', data)
schedule(on_error)
return
end
if #encoded_data > 200 then
return post_largedata(url, encoded_data, on_success, on_error)
return post_largedata(url, headers, encoded_data, on_success, on_error)
end
local args = {
'-X',
'POST',
'-H',
'Content-Type: application/json',
'-d',
encoded_data,
url,
}
for _, v in ipairs(headers) do
table.insert(args, '-H')
table.insert(args, v)
end
vim.list_extend(args, DEFAULT_ARGS)
_spawn(args, on_success, on_error)
end
Expand Down
2 changes: 1 addition & 1 deletion lua/fittencode/rest/rest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ end
function M:get(url, headers, data, on_success, on_error)
end

function M:post(url, data, on_success, on_error)
function M:post(url, headers, data, on_success, on_error)
end

return M

0 comments on commit 2e8e566

Please sign in to comment.