The schema defines the absence of state->open to represent the unknown state. Previously it was not possible to explicitly set this state via the HTTP endpoints. types.SpaceAPIState.Open is now an enum which is either unknown, closed, or open. Custom IsZero() and MarshalJSON() methods on the type ensure marshaling the value yields a boolean for open/closed and an empty byte slice for unknown (resulting in the field being absent from the final response). The handlers.StateOpenDELETE() function has been added to expose this behavior. Switching to encoding/json/v2 required raising the minimum Go version in go.mod to 1.27. Closes: CCCHH/spaceapid!37 Fixes: CCCHH/spaceapid#38
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.hamburg.ccc.de/ccchh/spaceapid/config"
|
|
"git.hamburg.ccc.de/ccchh/spaceapid/types"
|
|
)
|
|
|
|
func StateOpenPUT(
|
|
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
|
|
resp *types.SpaceAPIState,
|
|
) func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
body, err := updateEndpointValidator(authDB, validCredentials, w, r)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
// Parse request body
|
|
doorNowOpen, err := strconv.ParseBool(string(body))
|
|
if err != nil {
|
|
log.Println("Failed to parse request body from", r.RemoteAddr, "with error:", err)
|
|
http.Error(w, "HTTP request body should either be true or false", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Set SpaceAPI response values
|
|
if doorNowOpen {
|
|
resp.Open = types.SpaceAPIStateOpenIsOpen
|
|
} else {
|
|
resp.Open = types.SpaceAPIStateOpenIsClosed
|
|
}
|
|
resp.LastChange = time.Now().Unix()
|
|
}
|
|
}
|
|
|
|
func StateOpenDELETE(
|
|
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
|
|
resp *types.SpaceAPIState,
|
|
) func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
_, err := updateEndpointValidator(authDB, validCredentials, w, r)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
// Set SpaceAPI response values
|
|
resp.Open = types.SpaceAPIStateOpenIsUnknown
|
|
resp.LastChange = time.Now().Unix()
|
|
}
|
|
}
|
|
|
|
func StateMessagePUT(
|
|
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
|
|
resp *types.SpaceAPIState,
|
|
) func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
body, err := updateEndpointValidator(authDB, validCredentials, w, r)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
// Set SpaceAPI response values
|
|
resp.Message = string(body)
|
|
resp.LastChange = time.Now().Unix()
|
|
}
|
|
}
|
|
|
|
func StateMessageDELETE(
|
|
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
|
|
resp *types.SpaceAPIState,
|
|
) func(http.ResponseWriter, *http.Request) {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
_, err := updateEndpointValidator(authDB, validCredentials, w, r)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
// Set SpaceAPI response values
|
|
resp.Message = ""
|
|
resp.LastChange = time.Now().Unix()
|
|
}
|
|
}
|