Get rid of curl
This commit is contained in:
		| @@ -12,36 +12,63 @@ function Request.new(url, payload) | |||||||
| end | end | ||||||
|  |  | ||||||
| function Request:execute(callback) | function Request:execute(callback) | ||||||
|     local headers = { |     local client = vim.uv.new_tcp() | ||||||
|         ['Content-Type'] = 'application/json', |     local headers = table.concat({ | ||||||
|     } |         string.format("POST %s HTTP/1.1", self.url), | ||||||
|  |         "Host: generativelanguage.googleapis.com", | ||||||
|  |         "Content-Type: application/json", | ||||||
|  |         string.format("Content-Length: %d", #self.payload), | ||||||
|  |         "Connection: close", | ||||||
|  |         "", | ||||||
|  |         self.payload | ||||||
|  |     }, "\r\n") | ||||||
|  |  | ||||||
|     vim.loop.http_request({ |     local response_data = "" | ||||||
|         url = self.url, |      | ||||||
|         method = 'POST', |     client:connect("generativelanguage.googleapis.com", 443, function(err) | ||||||
|         headers = headers, |  | ||||||
|         body = self.payload |  | ||||||
|     }, function(err, response) |  | ||||||
|         if err then |         if err then | ||||||
|             vim.schedule(function() |             vim.schedule(function() | ||||||
|                 callback(nil, "HTTP request failed: " .. err) |                 callback(nil, "Connection failed: " .. err) | ||||||
|             end) |             end) | ||||||
|  |             client:close() | ||||||
|             return |             return | ||||||
|         end |         end | ||||||
|  |  | ||||||
|         if response.status ~= 200 then |         client:write(headers) | ||||||
|  |          | ||||||
|  |         client:read_start(function(err, chunk) | ||||||
|  |             if err then | ||||||
|                 vim.schedule(function() |                 vim.schedule(function() | ||||||
|                 callback(nil, "HTTP request failed with status " .. response.status) |                     callback(nil, "Read error: " .. err) | ||||||
|                 end) |                 end) | ||||||
|  |                 client:close() | ||||||
|                 return |                 return | ||||||
|             end |             end | ||||||
|  |  | ||||||
|         local success, decoded = pcall(vim.json.decode, response.body) |             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() |                     vim.schedule(function() | ||||||
|                         if success then |                         if success then | ||||||
|                             callback(decoded) |                             callback(decoded) | ||||||
|                         else |                         else | ||||||
|                 callback(nil, "JSON decode error: " .. response.body) |                             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) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user