Get rid of curl

This commit is contained in:
Jonas Widen 2025-03-16 19:06:26 +01:00
parent 39147fb21e
commit 2fbcf6c33e

View File

@ -24,52 +24,72 @@ function Request:execute(callback)
}, "\r\n") }, "\r\n")
local response_data = "" 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 if err then
vim.schedule(function() vim.schedule(function()
callback(nil, "Connection failed: " .. err) callback(nil, "DNS resolution failed: " .. err)
end) end)
client:close()
return return
end end
client:write(headers) if not res or #res == 0 then
vim.schedule(function()
client:read_start(function(err, chunk) callback(nil, "Could not resolve hostname")
end)
return
end
client:connect(res[1].addr, res[1].port, function(err)
if err then if err then
vim.schedule(function() vim.schedule(function()
callback(nil, "Read error: " .. err) callback(nil, "Connection failed: " .. err)
end) end)
client:close() client:close()
return return
end end
if chunk then client:write(headers)
response_data = response_data .. chunk
else client:read_start(function(err, chunk)
-- End of response if err then
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() vim.schedule(function()
if success then callback(nil, "Read error: " .. err)
callback(decoded)
else
callback(nil, "JSON decode error: " .. body)
end
end)
else
vim.schedule(function()
callback(nil, "Invalid response format")
end) end)
client:close()
return
end 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) end)
end end