/
api_plugin.go
90 lines (77 loc) · 2.26 KB
/
api_plugin.go
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
package node
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/momentum-xyz/ubercontroller/types/entry"
"github.com/momentum-xyz/ubercontroller/universe/logic/api"
"github.com/momentum-xyz/ubercontroller/utils"
"github.com/momentum-xyz/ubercontroller/utils/umid"
)
// @Summary Get plugins list
// @Description Returns plugins list
// @Tags plugins
// @Security Bearer
// @Param query query node.apiGetPluginsList.InQuery true "query params"
// @Success 200 {array} entry.Plugin
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /api/v4/plugins [get]
func (n *Node) apiGetPluginsList(c *gin.Context) {
type InQuery struct {
PluginID *string `form:"pluginId" json:"pluginId"`
Text *string `form:"text" json:"text"`
}
var inQuery InQuery
if err := c.ShouldBindQuery(&inQuery); err != nil {
err = errors.WithMessage(err, "Node: apiGetPluginsList: failed to bind query parameters")
api.AbortRequest(c, http.StatusBadRequest, "invalid_request_query", err, n.log)
return
}
var pluginID *umid.UMID
if inQuery.PluginID != nil {
id, err := umid.Parse(*inQuery.PluginID)
if err != nil {
err = errors.WithMessage(err, "Node: apiGetPluginsList: failed to parse pluginId")
api.AbortRequest(c, http.StatusBadRequest, "invalid_request_query", err, n.log)
return
}
pluginID = &id
}
plugins, err := n.db.GetPluginsDB().GetPlugins(c)
if err != nil {
err = errors.WithMessage(err, "Node: apiGetPluginsList: failed to get plugins")
api.AbortRequest(c, http.StatusInternalServerError, "internal_error", err, n.log)
return
}
filtered := make([]*entry.Plugin, 0)
id := pluginID
text := inQuery.Text
for _, plugin := range plugins {
if text == nil {
if id == nil {
filtered = append(filtered, plugin)
}
if id != nil {
if *id == plugin.PluginID {
filtered = append(filtered, plugin)
}
}
}
if text != nil {
name := utils.GetFromAnyMap(plugin.Meta, "name", "")
if id != nil {
if *id == plugin.PluginID && strings.Contains(name, *text) {
filtered = append(filtered, plugin)
}
}
if id == nil {
if strings.Contains(name, *text) {
filtered = append(filtered, plugin)
}
}
}
}
c.JSON(http.StatusOK, filtered)
}