Allow filtering /api/state using jq like ?jq=.result (#10577)
This commit is contained in:
parent
70fdc88c60
commit
f5bc2abd2f
2 changed files with 31 additions and 1 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
|
|
@ -16,7 +17,9 @@ import (
|
|||
"github.com/evcc-io/evcc/core/site"
|
||||
"github.com/evcc-io/evcc/server/assets"
|
||||
"github.com/evcc-io/evcc/util"
|
||||
"github.com/evcc-io/evcc/util/jq"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/itchyny/gojq"
|
||||
)
|
||||
|
||||
var ignoreState = []string{"releaseNotes"} // excessive size
|
||||
|
|
@ -164,7 +167,34 @@ func stateHandler(cache *util.Cache) http.HandlerFunc {
|
|||
for _, k := range ignoreState {
|
||||
delete(res, k)
|
||||
}
|
||||
|
||||
encodeFloats(res)
|
||||
|
||||
if q := r.URL.Query().Get("jq"); q != "" {
|
||||
q = strings.TrimPrefix(q, ".result")
|
||||
|
||||
query, err := gojq.Parse(q)
|
||||
if err != nil {
|
||||
jsonError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
b, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
jsonError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := jq.Query(query, b)
|
||||
if err != nil {
|
||||
jsonError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
jsonWrite(w, res)
|
||||
return
|
||||
}
|
||||
|
||||
jsonResult(w, res)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import (
|
|||
"github.com/itchyny/gojq"
|
||||
)
|
||||
|
||||
// Query executes a compiled jq query against given input. It expects a single result only.
|
||||
// Query executes a compiled jq query against given json. It expects a single result only.
|
||||
func Query(query *gojq.Query, input []byte) (interface{}, error) {
|
||||
var j interface{}
|
||||
if err := json.Unmarshal(input, &j); err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue