Skip to content

Commit

Permalink
Merge pull request #73 from luozhiya/rest2
Browse files Browse the repository at this point in the history
Refactor `Rest`
  • Loading branch information
luozhiya authored May 26, 2024
2 parents 786a39c + 2e8e566 commit 96ebb4d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 51 deletions.
12 changes: 9 additions & 3 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 All @@ -54,7 +56,9 @@ function M:login(username, password, on_success, on_error)
end)
end):forward(function(token)
return Promise:new(function(resolve, reject)
self.rest:authorize(URL.GET_FT_TOKEN, token, function(response)
self.rest:get(URL.GET_FT_TOKEN, {
'Authorization: Bearer ' .. token,
}, nil, function(response)
resolve(response)
end, function()
schedule(on_error)
Expand All @@ -75,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
84 changes: 38 additions & 46 deletions lua/fittencode/rest/backend/curl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local M = Rest:new('RestCurlBackend')
local CURL = 'curl'
local TIMEOUT = 5 -- 5 seconds
local DEFAULT_ARGS = {
'-s',
'--connect-timeout',
TIMEOUT,
'--show-error',
Expand All @@ -39,14 +40,7 @@ local function on_curl_signal(signal)
Log.error('cURL failed due to signal: {}', signal)
end

function M:authorize(url, token, on_success, on_error)
local args = {
'-s',
'-H',
'Authorization: Bearer ' .. token,
url,
}
vim.list_extend(args, DEFAULT_ARGS)
local function _spawn(args, on_success, on_error, on_exit)
Process.spawn({
cmd = CURL,
args = args,
Expand All @@ -55,74 +49,72 @@ function M:authorize(url, token, on_success, on_error)
end, function(signal)
on_curl_signal(signal)
schedule(on_error)
end, function()
schedule(on_exit)
end)
end

local function post_largedata(url, encoded_data, on_success, on_error)
function M:get(url, headers, data, on_success, on_error)
local args = {
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

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)
end, function(e_tmpfile)
schedule(on_error, e_tmpfile)
end)
end):forward(function(path)
return Promise:new(function(resolve, reject)
local args = {
'-s',
'-X',
'POST',
'-H',
'Content-Type: application/json',
'-d',
'@' .. path,
url,
}
vim.list_extend(args, DEFAULT_ARGS)
Process.spawn({
cmd = CURL,
args = args,
}, function(exit_code, response, error)
on_curl_exitcode(exit_code, response, error, on_success, on_error)
end, function(signal)
on_curl_signal(signal)
schedule(on_error)
end, function()
FS.delete(path)
end)
local args = {
'-X',
'POST',
'-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 = {
'-s',
'-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)
Process.spawn({
cmd = CURL,
args = args,
}, function(exit_code, response, error)
on_curl_exitcode(exit_code, response, error, on_success, on_error)
end, function(signal)
on_curl_signal(signal)
schedule(on_error)
end)
_spawn(args, on_success, on_error)
end

return M
4 changes: 2 additions & 2 deletions lua/fittencode/rest/rest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ function M:get_implementation_name()
return self.impl_name
end

function M:authorize(url, token, on_success, on_error)
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 96ebb4d

Please sign in to comment.