Skip to content

Commit

Permalink
Simplify api json handling and fix duplicate status header
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored and dontbyte committed Aug 2, 2022
1 parent 28e9223 commit ca859ee
Showing 1 changed file with 31 additions and 42 deletions.
73 changes: 31 additions & 42 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ import (
"github.com/gorilla/mux"
)

const (
result = "result"
failure = "error"
)

func indexHandler(site site.API) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
Expand Down Expand Up @@ -55,13 +50,22 @@ func jsonHandler(h http.Handler) http.Handler {
})
}

func jsonResponse(w http.ResponseWriter, r *http.Request, content interface{}) {
w.WriteHeader(http.StatusOK)
func jsonWrite(w http.ResponseWriter, content interface{}) {
if err := json.NewEncoder(w).Encode(content); err != nil {
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
func healthHandler(site site.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -82,7 +86,7 @@ func stateHandler(cache *util.Cache) http.HandlerFunc {
for _, k := range []string{"availableVersion", "releaseNotes"} {
delete(res, k)
}
jsonResponse(w, r, res)
jsonResult(w, res)
}
}

Expand All @@ -93,14 +97,13 @@ func chargeModeHandler(lp loadpoint.API) http.HandlerFunc {

mode, err := api.ChargeModeString(vars["value"])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
jsonError(w, http.StatusBadRequest, err)
return
}

lp.SetMode(mode)

jsonResponse(w, r, map[string]interface{}{result: lp.GetMode()})
jsonResult(w, lp.GetMode())
}
}

Expand All @@ -115,12 +118,11 @@ func targetSoCHandler(lp loadpoint.API) http.HandlerFunc {
}

if err != nil {
w.WriteHeader(http.StatusBadRequest)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
jsonError(w, http.StatusBadRequest, err)
return
}

jsonResponse(w, r, map[string]interface{}{result: lp.GetTargetSoC()})
jsonResult(w, lp.GetTargetSoC())
}
}

Expand All @@ -135,12 +137,11 @@ func minSoCHandler(lp loadpoint.API) http.HandlerFunc {
}

if err != nil {
w.WriteHeader(http.StatusBadRequest)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
jsonError(w, http.StatusBadRequest, err)
return
}

jsonResponse(w, r, map[string]interface{}{result: lp.GetMinSoC()})
jsonResult(w, lp.GetMinSoC())
}
}

Expand All @@ -153,12 +154,11 @@ func minCurrentHandler(lp loadpoint.API) http.HandlerFunc {
if err == nil {
lp.SetMinCurrent(current)
} else {
w.WriteHeader(http.StatusBadRequest)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
jsonError(w, http.StatusBadRequest, err)
return
}

jsonResponse(w, r, map[string]interface{}{result: lp.GetMinCurrent()})
jsonResult(w, lp.GetMinCurrent())
}
}

Expand All @@ -171,12 +171,11 @@ func maxCurrentHandler(lp loadpoint.API) http.HandlerFunc {
if err == nil {
lp.SetMaxCurrent(current)
} else {
w.WriteHeader(http.StatusBadRequest)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
jsonError(w, http.StatusBadRequest, err)
return
}

jsonResponse(w, r, map[string]interface{}{result: lp.GetMaxCurrent()})
jsonResult(w, lp.GetMaxCurrent())
}
}

Expand All @@ -191,12 +190,11 @@ func phasesHandler(lp loadpoint.API) http.HandlerFunc {
}

if err != nil {
w.WriteHeader(http.StatusBadRequest)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
jsonError(w, http.StatusBadRequest, err)
return
}

jsonResponse(w, r, map[string]interface{}{result: lp.GetPhases()})
jsonResult(w, lp.GetPhases())
}
}

Expand All @@ -205,18 +203,10 @@ func remoteDemandHandler(lp loadpoint.API) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

demandS, ok := vars["demand"]

var source string
if ok {
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()})
source := vars["source"]
demand, err := loadpoint.RemoteDemandString(vars["demand"])
if err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}

Expand All @@ -230,7 +220,7 @@ func remoteDemandHandler(lp loadpoint.API) http.HandlerFunc {
Demand: demand,
}

jsonResponse(w, r, res)
jsonResult(w, res)
}
}

Expand Down Expand Up @@ -261,8 +251,7 @@ func targetChargeHandler(loadpoint loadpoint.API) http.HandlerFunc {
timeV, err := time.ParseInLocation("2006-01-02T15:04:05", timeS, timezone())

if !ok || err != nil {
w.WriteHeader(http.StatusBadRequest)
jsonResponse(w, r, map[string]interface{}{failure: err.Error()})
jsonError(w, http.StatusBadRequest, err)
return
}

Expand All @@ -276,7 +265,7 @@ func targetChargeHandler(loadpoint loadpoint.API) http.HandlerFunc {
Time: timeV,
}

jsonResponse(w, r, res)
jsonResult(w, res)
}
}

Expand Down

0 comments on commit ca859ee

Please sign in to comment.