-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.lua
More file actions
203 lines (172 loc) · 4.58 KB
/
Copy pathapi.lua
File metadata and controls
203 lines (172 loc) · 4.58 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
local M = {}
local config = require "stackgen.config"
local Job = require "plenary.job"
---@class stackgen_module
---@field id string
---@field name string
---@field resourceType string
---@field cloudProvider string
---@field files table<string, string>
local function handle_response(output)
local status_code = tonumber(output[#output])
if status_code == nil then
vim.notify("API request failed: no status code received", vim.log.levels.ERROR)
return nil
end
table.remove(output, #output)
local body = table.concat(output, "\n")
if status_code >= 400 then
vim.notify("API request failed: status code " .. status_code .. "\n" .. body, vim.log.levels.ERROR)
return nil
end
local success, data = pcall(vim.json.decode, body)
if not success then
vim.notify("Failed to parse JSON response: " .. data, vim.log.levels.ERROR)
return nil
end
return data
end
local list_modules_path = "/tf-module/v1/modules"
function M.list_modules(callback)
local list_modules_endpoint = config.options.url .. list_modules_path
---@diagnostic disable-next-line: missing-fields
Job:new({
command = "curl",
args = {
"-s",
"-w",
"\n%{http_code}",
"-H",
"Authorization" .. ": Bearer " .. config.get_token(),
list_modules_endpoint,
},
on_exit = function(job, _)
vim.schedule(function()
local body = handle_response(job:result())
if not body then
return
end
callback(body)
end)
end,
}):start()
end
local get_module_path = "/tf-module/v1/modules/%s"
function M.get_module(id, callback)
local get_module_endpoint = string.format(config.options.url .. get_module_path, id)
---@diagnostic disable-next-line: missing-fields
Job:new({
command = "curl",
args = {
"-s",
"-w",
"\n%{http_code}",
"-H",
"Authorization" .. ": Bearer " .. config.get_token(),
get_module_endpoint,
},
on_exit = function(job, _)
vim.schedule(function()
local body = handle_response(job:result())
if not body then
return
end
callback(body)
end)
end,
}):start()
end
local sync_module_path = "/tf-module/v1/modules/%s/files"
--- Syncs a module with the given ID and content
--- @param id string ID of the module to sync
--- @param content table<string, string> content to sync with the module
function M.sync_module(id, content, callback)
local sync_module_endpoint = string.format(config.options.url .. sync_module_path, id)
local payload = {
files = content,
}
local payload_json = vim.fn.json_encode(payload)
---@diagnostic disable-next-line: missing-fields
Job:new({
command = "curl",
args = {
"-X",
"PUT",
"-s",
"-w",
"\n%{http_code}",
"-H",
"Authorization" .. ": Bearer " .. config.get_token(),
"--data-raw",
payload_json,
sync_module_endpoint,
},
on_exit = function(job, _)
vim.schedule(function()
local body = handle_response(job:result())
if not body then
return
end
callback(body)
end)
end,
}):start()
end
local publish_module_path = "/tf-module/v1/modules/%s/publish"
function M.publish_module(id, version, callback)
local publish_module_endpoint = string.format(config.options.url .. publish_module_path, id)
local payload = {
version = version,
}
local payload_json = vim.fn.json_encode(payload)
Job:new({
command = "curl",
args = {
"-X",
"POST",
"-s",
"-w",
"\n%{http_code}",
"-H",
"Authorization" .. ": Bearer " .. config.get_token(),
"--data-raw",
payload_json,
publish_module_endpoint,
},
on_exit = function(job, _)
vim.schedule(function()
local body = handle_response(job:result())
if not body then
return
end
callback(body)
end)
end,
}):start()
end
local module_service_health_path = "/tf-module/v1/health"
function M.check_module_service_health(callback)
local health_endpoint = config.options.url .. module_service_health_path
---@diagnostic disable-next-line: missing-fields
Job:new({
command = "curl",
args = {
"-s",
"-w",
"\n%{http_code}",
"-H",
"Authorization" .. ": Bearer " .. config.get_token(),
health_endpoint,
},
on_exit = function(job, _)
vim.schedule(function()
local body = handle_response(job:result())
if not body then
return
end
callback(body)
end)
end,
}):start()
end
return M