Simplify api json handling and fix duplicate status header
Some checks failed
Build / Build (push) Failing after 1m36s
Build / Build UI (push) Failing after 2m11s
Build / Publish :latest (push) Has been skipped
Build / Publish :release (push) Has been skipped
Build / Release (push) Has been skipped

This commit is contained in:
andig 2021-11-10 18:05:15 +01:00
parent 91be08e48c
commit 2c1d727880

View file

@ -17,11 +17,6 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
const (
result = "result"
failure = "error"
)
func indexHandler(site site.API) http.HandlerFunc { func indexHandler(site site.API) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8") w.Header().Set("Content-Type", "text/html; charset=UTF-8")
@ -55,13 +50,22 @@ func jsonHandler(h http.Handler) http.Handler {
}) })
} }
func jsonResponse(w http.ResponseWriter, r *http.Request, content interface{}) { func jsonWrite(w http.ResponseWriter, content interface{}) {
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(content); err != nil { if err := json.NewEncoder(w).Encode(content); err != nil {
log.ERROR.Printf("httpd: failed to encode JSON: %v", err) log.ERROR.Printf("httpd: failed to encode JSON: %v", err)
} }
} }
func jsonResult(w http.ResponseWriter, res interface{}) {
w.WriteHeader(http.StatusOK)
jsonWrite(w, map[string]interface{}{"result": res})
}
func jsonError(w http.ResponseWriter, status int, err error) {
w.WriteHeader(status)
jsonWrite(w, map[string]interface{}{"error": err.Error()})
}
// healthHandler returns current charge mode // healthHandler returns current charge mode
func healthHandler(site site.API) http.HandlerFunc { func healthHandler(site site.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
@ -82,7 +86,7 @@ func stateHandler(cache *util.Cache) http.HandlerFunc {
for _, k := range []string{"availableVersion", "releaseNotes"} { for _, k := range []string{"availableVersion", "releaseNotes"} {
delete(res, k) delete(res, k)
} }
jsonResponse(w, r, res) jsonResult(w, res)
} }
} }
@ -93,14 +97,13 @@ func chargeModeHandler(lp loadpoint.API) http.HandlerFunc {
mode, err := api.ChargeModeString(vars["value"]) mode, err := api.ChargeModeString(vars["value"])
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) jsonError(w, http.StatusBadRequest, err)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
return return
} }
lp.SetMode(mode) lp.SetMode(mode)
jsonResponse(w, r, map[string]interface{}{result: lp.GetMode()}) jsonResult(w, lp.GetMode())
} }
} }
@ -115,12 +118,11 @@ func targetSoCHandler(lp loadpoint.API) http.HandlerFunc {
} }
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) jsonError(w, http.StatusBadRequest, err)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
return return
} }
jsonResponse(w, r, map[string]interface{}{result: lp.GetTargetSoC()}) jsonResult(w, lp.GetTargetSoC())
} }
} }
@ -135,12 +137,11 @@ func minSoCHandler(lp loadpoint.API) http.HandlerFunc {
} }
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) jsonError(w, http.StatusBadRequest, err)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
return return
} }
jsonResponse(w, r, map[string]interface{}{result: lp.GetMinSoC()}) jsonResult(w, lp.GetMinSoC())
} }
} }
@ -153,12 +154,11 @@ func minCurrentHandler(lp loadpoint.API) http.HandlerFunc {
if err == nil { if err == nil {
lp.SetMinCurrent(current) lp.SetMinCurrent(current)
} else { } else {
w.WriteHeader(http.StatusBadRequest) jsonError(w, http.StatusBadRequest, err)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
return return
} }
jsonResponse(w, r, map[string]interface{}{result: lp.GetMinCurrent()}) jsonResult(w, lp.GetMinCurrent())
} }
} }
@ -171,12 +171,11 @@ func maxCurrentHandler(lp loadpoint.API) http.HandlerFunc {
if err == nil { if err == nil {
lp.SetMaxCurrent(current) lp.SetMaxCurrent(current)
} else { } else {
w.WriteHeader(http.StatusBadRequest) jsonError(w, http.StatusBadRequest, err)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
return return
} }
jsonResponse(w, r, map[string]interface{}{result: lp.GetMaxCurrent()}) jsonResult(w, lp.GetMaxCurrent())
} }
} }
@ -191,12 +190,11 @@ func phasesHandler(lp loadpoint.API) http.HandlerFunc {
} }
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) jsonError(w, http.StatusBadRequest, err)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
return return
} }
jsonResponse(w, r, map[string]interface{}{result: lp.GetPhases()}) jsonResult(w, lp.GetPhases())
} }
} }
@ -205,18 +203,10 @@ func remoteDemandHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
demandS, ok := vars["demand"] source := vars["source"]
demand, err := loadpoint.RemoteDemandString(vars["demand"])
var source string if err != nil {
if ok { jsonError(w, http.StatusBadRequest, err)
source, ok = vars["source"]
}
demand, err := loadpoint.RemoteDemandString(demandS)
if !ok || err != nil {
w.WriteHeader(http.StatusBadRequest)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
return return
} }
@ -230,7 +220,7 @@ func remoteDemandHandler(lp loadpoint.API) http.HandlerFunc {
Demand: demand, Demand: demand,
} }
jsonResponse(w, r, res) jsonResult(w, res)
} }
} }
@ -261,8 +251,7 @@ func targetChargeHandler(loadpoint loadpoint.API) http.HandlerFunc {
timeV, err := time.ParseInLocation("2006-01-02T15:04:05", timeS, timezone()) timeV, err := time.ParseInLocation("2006-01-02T15:04:05", timeS, timezone())
if !ok || err != nil { if !ok || err != nil {
w.WriteHeader(http.StatusBadRequest) jsonError(w, http.StatusBadRequest, err)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
return return
} }
@ -276,7 +265,7 @@ func targetChargeHandler(loadpoint loadpoint.API) http.HandlerFunc {
Time: timeV, Time: timeV,
} }
jsonResponse(w, r, res) jsonResult(w, res)
} }
} }