-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.go
More file actions
285 lines (239 loc) · 6.49 KB
/
plugins.go
File metadata and controls
285 lines (239 loc) · 6.49 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package ccatapi
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
)
// pluginsClient is a sub-client for the Plugins API.
type pluginsClient struct {
config clientConfig
}
// newPluginsClient creates a new Plugins sub-client with the provided config.
func newPluginsClient(config clientConfig) *pluginsClient {
client := &pluginsClient{
config: config,
}
WithBaseURL(fmt.Sprintf("%s/%s", client.config.baseURL, "plugins"))(&client.config)
return client
}
// PluginsResponse contains the response data about a plugin.
type PluginsResponse struct {
Filters pluginResponseFilters `json:"filters"`
Installed []InstalledPlugin `json:"installed"`
Registry []RegistryPlugin `json:"registry"`
}
type pluginResponseFilters struct {
Query *string `json:"query,omitempty"`
}
type plugin struct {
Name string `json:"name"`
Version string `json:"version"`
Description string `json:"description"`
AuthorName string `json:"author_name"`
AuthorURL string `json:"author_url"`
PluginURL string `json:"plugin_url"`
Tags string `json:"tags"`
Thumb string `json:"thumb"`
}
// InstalledPlugin contains the data about an installed plugin.
type InstalledPlugin struct {
plugin
ID string `json:"id"`
Active bool `json:"active"`
Upgrade *string `json:"upgrade"`
Hooks []InstalledPluginHookData `json:"hooks"`
Tools []InstalledPluginToolData `json:"tools"`
}
// InstalledPluginHookData contains the data about an installed plugin hook.
type InstalledPluginHookData struct {
Name string `json:"name"`
Priority int `json:"priority"`
}
// InstalledPluginToolData contains the data about an installed plugin tool.
type InstalledPluginToolData struct {
Name string `json:"name"`
}
// RegistryPlugin contains the data about a registry plugin.
type RegistryPlugin struct {
plugin
URL string `json:"url"`
}
// GetPlugins returns all available plugins, optionally filtered by a search query.
func (client *pluginsClient) GetPlugins() (*PluginsResponse, error) {
resp, err := doAPIRequest[any, PluginsResponse](
client.config,
http.MethodGet,
"",
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
// UploadPluginResponse contains the information about the uploaded plugin.
type UploadPluginResponse struct {
FileName string `json:"file_name"`
ContentType string `json:"content_type"`
Info string `json:"info"`
}
// UploadPlugin uploads a plugin.
func (client *pluginsClient) UploadPlugin(zipFileReader *os.File) (*UploadPluginResponse, error) {
if zipFileReader == nil {
return nil, ErrUploadMissingFile
}
var requestBodyBuffer bytes.Buffer
multipartWriter := multipart.NewWriter(&requestBodyBuffer)
formFieldWriter, err := multipartWriter.CreateFormFile("file", zipFileReader.Name())
if err != nil {
return nil, err
}
_, err = io.Copy(formFieldWriter, zipFileReader)
if err != nil {
return nil, err
}
multipartWriter.Close()
resp, err := doHTTPRequest[UploadPluginResponse](
client.config,
multipartWriter.FormDataContentType(),
http.MethodPost,
"upload",
nil,
&requestBodyBuffer,
)
if err != nil {
return nil, err
}
return resp, nil
}
type UploadPluginFromRegistryPayload struct {
URL string `json:"url"`
}
// UploadPluginFromRegistry uploads a plugin from a registry url.
func (client *pluginsClient) UploadPluginFromRegistry(payload UploadPluginFromRegistryPayload) (*UploadPluginResponse, error) {
resp, err := doAPIRequest[UploadPluginFromRegistryPayload, UploadPluginResponse](
client.config,
http.MethodPost,
"upload/registry",
nil,
&payload,
)
if err != nil {
return nil, err
}
return resp, nil
}
type TogglePluginResponse struct {
Info string `json:"info"`
}
// TogglePlugin enables or disables a single plugin.
func (client *pluginsClient) TogglePlugin(pluginID string) (*TogglePluginResponse, error) {
pathParams := fmt.Sprintf("toggle/%s", pluginID)
resp, err := doAPIRequest[any, TogglePluginResponse](
client.config,
http.MethodPost,
pathParams,
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
type GetPluginsSettingsResponse struct {
Settings []PluginSetting `json:"settings"`
}
// PluginSetting contains the data about a single plugin setting.
type PluginSetting struct {
Name string `json:"name"`
Value map[string]any `json:"value"`
Schema PluginSettingSchema `json:"schema"`
}
// PluginSettingSchema contains the JSON schema about a single plugin setting.
type PluginSettingSchema struct {
settingSchema
}
// GetPluginsSettings returns the settings for all plugins.
func (client *pluginsClient) GetPluginsSettings() (*GetPluginsSettingsResponse, error) {
resp, err := doAPIRequest[any, GetPluginsSettingsResponse](
client.config,
http.MethodGet,
"settings",
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
// GetPluginSettings returns the settings for a single plugin.
func (client *pluginsClient) GetPluginSettings(pluginID string) (*PluginSetting, error) {
pathParams := fmt.Sprintf("settings/%s", pluginID)
resp, err := doAPIRequest[any, PluginSetting](
client.config,
http.MethodGet,
pathParams,
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
// UpsertPluginSettingsValue upserts the settings for a single plugin.
func (client *pluginsClient) UpsertPluginSettingsValue(pluginID string, value map[string]any) (*PluginSetting, error) {
pathParams := fmt.Sprintf("settings/%s", pluginID)
resp, err := doAPIRequest[map[string]any, PluginSetting](
client.config,
http.MethodPut,
pathParams,
nil,
&value,
)
if err != nil {
return nil, err
}
return resp, nil
}
type Plugin struct {
}
// GetPluginDetail returns the details for a single installed plugin.
func (client *pluginsClient) GetPluginDetail(pluginID string) (*InstalledPlugin, error) {
resp, err := doAPIRequest[any, InstalledPlugin](
client.config,
http.MethodGet,
pluginID,
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}
// DeletePluginResponse contains the information about the deleted plugin.
type DeletePluginResponse struct {
// The name of the deleted plugin.
Deleted string `json:"deleted"`
}
// DeletePlugin deletes a single plugin.
func (client *pluginsClient) DeletePlugin(pluginID string) (*DeletePluginResponse, error) {
resp, err := doAPIRequest[any, DeletePluginResponse](
client.config,
http.MethodDelete,
pluginID,
nil,
nil,
)
if err != nil {
return nil, err
}
return resp, nil
}