Get rid of curl
This commit is contained in:
parent
39147fb21e
commit
2fbcf6c33e
@ -24,52 +24,72 @@ function Request:execute(callback)
|
||||
}, "\r\n")
|
||||
|
||||
local response_data = ""
|
||||
|
||||
client:connect("generativelanguage.googleapis.com", 443, function(err)
|
||||
|
||||
vim.loop.getaddrinfo("generativelanguage.googleapis.com", "443", {
|
||||
family = "inet",
|
||||
socktype = "stream",
|
||||
protocol = "tcp"
|
||||
}, function(err, res)
|
||||
if err then
|
||||
vim.schedule(function()
|
||||
callback(nil, "Connection failed: " .. err)
|
||||
callback(nil, "DNS resolution failed: " .. err)
|
||||
end)
|
||||
client:close()
|
||||
return
|
||||
end
|
||||
|
||||
client:write(headers)
|
||||
|
||||
client:read_start(function(err, chunk)
|
||||
if not res or #res == 0 then
|
||||
vim.schedule(function()
|
||||
callback(nil, "Could not resolve hostname")
|
||||
end)
|
||||
return
|
||||
end
|
||||
|
||||
client:connect(res[1].addr, res[1].port, function(err)
|
||||
if err then
|
||||
vim.schedule(function()
|
||||
callback(nil, "Read error: " .. err)
|
||||
callback(nil, "Connection failed: " .. err)
|
||||
end)
|
||||
client:close()
|
||||
return
|
||||
end
|
||||
|
||||
if chunk then
|
||||
response_data = response_data .. chunk
|
||||
else
|
||||
-- End of response
|
||||
client:close()
|
||||
|
||||
-- Parse response
|
||||
local headers_end = response_data:find("\r\n\r\n")
|
||||
if headers_end then
|
||||
local body = response_data:sub(headers_end + 4)
|
||||
local success, decoded = pcall(vim.json.decode, body)
|
||||
|
||||
client:write(headers)
|
||||
|
||||
client:read_start(function(err, chunk)
|
||||
if err then
|
||||
vim.schedule(function()
|
||||
if success then
|
||||
callback(decoded)
|
||||
else
|
||||
callback(nil, "JSON decode error: " .. body)
|
||||
end
|
||||
end)
|
||||
else
|
||||
vim.schedule(function()
|
||||
callback(nil, "Invalid response format")
|
||||
callback(nil, "Read error: " .. err)
|
||||
end)
|
||||
client:close()
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if chunk then
|
||||
response_data = response_data .. chunk
|
||||
else
|
||||
-- End of response
|
||||
client:close()
|
||||
|
||||
-- Parse response
|
||||
local headers_end = response_data:find("\r\n\r\n")
|
||||
if headers_end then
|
||||
local body = response_data:sub(headers_end + 4)
|
||||
local success, decoded = pcall(vim.json.decode, body)
|
||||
|
||||
vim.schedule(function()
|
||||
if success then
|
||||
callback(decoded)
|
||||
else
|
||||
callback(nil, "JSON decode error: " .. body)
|
||||
end
|
||||
end)
|
||||
else
|
||||
vim.schedule(function()
|
||||
callback(nil, "Invalid response format")
|
||||
end)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user