11-- This module is responsible for making API calls to the Go server and
22-- running the callbacks associated with those jobs when the JSON is returned
3- local Job = require (" plenary.job" )
43local u = require (" gitlab.utils" )
54local M = {}
65
6+ --- Send a request to the Go server
7+ --- @param endpoint string The endpoint path on the server.
8+ --- @param method string The HTTP rquest method.
9+ --- @param callback fun ( data : table ) The function to run on the decoded JSON response data if the response contains no error details.
10+ --- @param on_error_callback ? fun ( data : table ) The function to run on the decoded JSON response data in case the response contains error details.
711M .run_job = function (endpoint , method , body , callback , on_error_callback )
812 local state = require (" gitlab.state" )
913 local port = state .settings .server and state .settings .server .port
10- local args =
11- { " --noproxy" , " localhost" , " -s" , " -X" , (method or " POST" ), string.format (" localhost:%s%s" , port , endpoint ) }
14+ local cmd = {
15+ " curl" ,
16+ " --noproxy" ,
17+ " localhost" ,
18+ " -s" ,
19+ " -X" ,
20+ (method or " POST" ),
21+ string.format (" localhost:%s%s" , port , endpoint ),
22+ }
1223
1324 if body ~= nil then
1425 local encoded_body = vim .json .encode (body )
15- table.insert (args , 1 , " -d" )
16- table.insert (args , 2 , encoded_body )
26+ table.insert (cmd , 2 , " -d" )
27+ table.insert (cmd , 3 , encoded_body )
1728 end
1829
1930 -- This handler will handle all responses from the Go server. Anything with a successful
2031 -- status will call the callback (if it is supplied for the job). Otherwise, it will print out the
2132 -- success message or error message and details from the Go server and run the on_error_callback
2233 -- (if supplied for the job).
23- local stderr = {}
24- Job :new ({
25- command = " curl" ,
26- args = args ,
27- on_stdout = function (_ , output )
28- vim .defer_fn (function ()
29- if output == nil then
30- return
31- end
32- local data_ok , data = pcall (vim .json .decode , output )
34+ vim .system (cmd , { text = true }, function (out )
35+ vim .schedule (function ()
36+ if out .code ~= 0 then
37+ u .notify (string.format (" Go server exited with non-zero code: %d" , out .code ), vim .log .levels .ERROR )
38+ end
3339
40+ if out .stderr ~= " " then
41+ u .notify (string.format (" Could not run command `%s`! Stderr was:" , table.concat (cmd , " " )), vim .log .levels .ERROR )
42+ u .notify (vim .trim (out .stderr ), vim .log .levels .ERROR )
43+ end
44+
45+ if out .stdout ~= " " then
46+ local data_ok , data = pcall (vim .json .decode , out .stdout )
3447 -- Failing to unmarshal JSON
3548 if not data_ok then
3649 local msg = string.format (" Failed to parse JSON from %s endpoint" , endpoint )
37- if type (output ) == " string" then
38- msg = string.format (msg .. " , got: '%s'" , output )
50+ if type (out . stdout ) == " string" then
51+ msg = string.format (msg .. " , got: '%s'" , out . stdout )
3952 end
40- u .notify (string.format (msg , endpoint , output ), vim .log .levels .WARN )
53+ u .notify (string.format (msg , endpoint , out . stdout ), vim .log .levels .WARN )
4154 return
4255 end
4356
@@ -60,28 +73,9 @@ M.run_job = function(endpoint, method, body, callback, on_error_callback)
6073 on_error_callback (data )
6174 end
6275 end
63- end , 0 )
64- end ,
65- on_stderr = function (_ , data )
66- if data then
67- table.insert (stderr , data )
6876 end
69- end ,
70- on_exit = function (code , status )
71- vim .defer_fn (function ()
72- if # stderr ~= 0 then
73- u .notify (
74- string.format (" Could not run command `%s %s`! Stderr was:" , code .command , table.concat (code .args , " " )),
75- vim .log .levels .ERROR
76- )
77- vim .notify (string.format (" %s" , table.concat (stderr , " \n " )), vim .log .levels .ERROR )
78- end
79- if status ~= 0 then
80- u .notify (string.format (" Go server exited with non-zero code: %d" , status ), vim .log .levels .ERROR )
81- end
82- end , 0 )
83- end ,
84- }):start ()
77+ end )
78+ end )
8579end
8680
8781return M
0 commit comments