forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_config_metadata_handler.go
95 lines (79 loc) · 2 KB
/
http_config_metadata_handler.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
91
92
93
94
95
package server
import (
"net/http"
"slices"
"strings"
"github.com/evcc-io/evcc/util/templates"
"github.com/gorilla/mux"
)
// templatesHandler returns the list of templates by class
func templatesHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
class, err := templates.ClassString(vars["class"])
if err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
lang := r.URL.Query().Get("lang")
templates.EncoderLanguage(lang)
if name := r.URL.Query().Get("name"); name != "" {
res, err := templates.ByName(class, name)
if err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
jsonResult(w, res)
return
}
// filter deprecated properties
res := make([]templates.Template, 0)
for _, t := range templates.ByClass(class) {
params := make([]templates.Param, 0, len(t.Params))
for _, p := range t.Params {
if p.Deprecated == nil || !*p.Deprecated {
params = append(params, p)
}
}
t.Params = params
res = append(res, t)
}
jsonResult(w, res)
}
// productsHandler returns the list of products by class
func productsHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
class, err := templates.ClassString(vars["class"])
if err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
tmpl := templates.ByClass(class)
lang := r.URL.Query().Get("lang")
usage := r.URL.Query().Get("usage")
res := make(products, 0)
for _, t := range tmpl {
// if usage filter is specified, only include templates with matching usage
includeUsage := usage == ""
if !includeUsage {
for _, u := range t.Usages() {
if u == usage {
includeUsage = true
break
}
}
}
if includeUsage {
for _, p := range t.Products {
res = append(res, product{
Name: p.Title(lang),
Template: t.TemplateDefinition.Template,
Group: t.Group,
})
}
}
}
slices.SortFunc(res, func(a, b product) int {
return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
})
jsonResult(w, res)
}