-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
http.lua
148 lines (126 loc) · 4.32 KB
/
http.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local config = require("codecompanion.config")
local curl = require("plenary.curl")
local log = require("codecompanion.utils.log")
local schema = require("codecompanion.schema")
local util = require("codecompanion.utils.util")
---@class CodeCompanion.Client
---@field adapter CodeCompanion.Adapter
---@field static table
---@field opts nil|table
---@field user_args nil|table
local Client = {}
Client.static = {}
-- This makes it easier to mock during testing
Client.static.opts = {
post = { default = curl.post },
get = { default = curl.get },
encode = { default = vim.json.encode },
schedule = { default = vim.schedule_wrap },
}
---@class CodeCompanion.ClientArgs
---@field adapter CodeCompanion.Adapter
---@field opts nil|table
---@field user_args nil|table
---@param args CodeCompanion.ClientArgs
---@return CodeCompanion.Client
function Client.new(args)
args = args or {}
return setmetatable({
adapter = args.adapter,
opts = args.opts or schema.get_default(Client.static.opts, args.opts),
user_args = args.user_args or {},
}, { __index = Client })
end
---@class CodeCompanion.Adapter.RequestActions
---@field callback fun(err: nil|string, chunk: nil|table) Callback function, executed when the request has finished or is called multiple times if the request is streaming
---@field done? fun() Function to run when the request is complete
---@param payload table The payload to be sent to the endpoint
---@param actions CodeCompanion.Adapter.RequestActions
---@param opts? table Options that can be passed to the request
---@return table|nil The Plenary job
function Client:request(payload, actions, opts)
opts = opts or {}
local cb = log:wrap_cb(actions.callback, "Response error: %s")
local adapter = self.adapter
local handlers = adapter.handlers
if handlers and handlers.setup then
local ok = handlers.setup(adapter)
if not ok then
return
end
end
adapter:get_env_vars()
local body = self.opts.encode(
vim.tbl_extend(
"keep",
handlers.form_parameters and handlers.form_parameters(adapter, adapter:set_env_vars(adapter.parameters), payload)
or {},
handlers.form_messages and handlers.form_messages(adapter, payload) or {},
handlers.set_body and handlers.set_body(adapter, payload) or {}
)
)
local request_opts = {
url = adapter:set_env_vars(adapter.url),
headers = adapter:set_env_vars(adapter.headers),
insecure = config.adapters.opts.allow_insecure,
proxy = config.adapters.opts.proxy,
raw = adapter.raw or { "--no-buffer" },
body = body or "",
-- This is called when the request is finished. It will only ever be called
-- once, even if the endpoint is streaming.
callback = function(data)
vim.schedule(function()
if (not adapter.opts.stream) and data and data ~= "" then
log:trace("Output data:\n%s", data)
cb(nil, data)
end
if handlers and handlers.on_exit then
handlers.on_exit(adapter, data)
end
if handlers and handlers.teardown then
handlers.teardown(adapter)
end
if actions.done and type(actions.done) == "function" then
actions.done()
end
opts["status"] = "success"
if data.status >= 400 then
opts["status"] = "error"
end
util.fire("RequestFinished", opts)
if self.user_args.event then
util.fire("RequestFinished" .. (self.user_args.event or ""), opts)
end
end)
end,
on_error = function(err)
vim.schedule(function()
cb(err, nil)
return util.fire("RequestFinished", opts)
end)
end,
}
if adapter.opts and adapter.opts.stream then
-- This will be called multiple times until the stream is finished
request_opts["stream"] = self.opts.schedule(function(_, data)
if data and data ~= "" then
log:trace("Output data:\n%s", data)
end
cb(nil, data)
end)
end
local request = "post"
if adapter.opts and adapter.opts.method then
request = adapter.opts.method:lower()
end
local job = self.opts[request](request_opts)
util.fire("RequestStarted", opts)
if job and job.args then
log:debug("Request:\n%s", job.args)
end
if self.user_args.event then
util.fire("RequestStarted" .. (self.user_args.event or ""), opts)
end
return job
end
return Client