Templates: add http service for parameter choices (#32951)

This commit is contained in:
andig 2026-08-18 15:22:48 +02:00 • committed by GitHub
parent e7ab19d7ca
commit 0ae85295dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 81 additions and 5 deletions

75
util/service/http.go Normal file
View file

@ -0,0 +1,75 @@
package service
import (
"errors"
"net/http"
"github.com/evcc-io/evcc/server/service"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/jq"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"github.com/itchyny/gojq"
"github.com/spf13/cast"
)
func init() {
mux := http.NewServeMux()
mux.HandleFunc("GET /get", httpGet)
service.Register("http", mux)
}
// httpGet reads uri and extracts the choices using the jq query, driving
// selection of remote resources in templates. The config UI polls while the
// form is filled, hence a rejected request yields an empty list.
func httpGet(w http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
uri := q.Get("uri")
if uri == "" {
jsonError(w, http.StatusBadRequest, errors.New("missing uri"))
return
}
expr := q.Get("jq")
if expr == "" {
expr = "."
}
query, err := gojq.Parse(expr)
if err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
log := util.NewLogger("http")
client := request.NewHelper(log)
if token := q.Get("token"); token != "" {
log.Redact(token)
client.Transport = transport.BearerAuth(token, client.Transport)
}
res := []string{}
defer func() { jsonWrite(w, res) }()
body, err := client.GetBody(uri)
if err != nil {
return
}
val, err := jq.Query(query, body)
if err != nil {
return
}
if slice, ok := val.([]any); ok {
for _, v := range slice {
res = append(res, cast.ToString(v))
}
return
}
res = append(res, cast.ToString(val))
}