feat: Allow setting state->open to unknown via DELETE

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
This commit is contained in:
Bendodroid 2026-10-08 23:58:10 +02:00
commit 11046cb501
7 changed files with 110 additions and 38 deletions

View file

@ -10,7 +10,7 @@ import (
"git.hamburg.ccc.de/ccchh/spaceapid/types"
)
func StateOpen(
func StateOpenPUT(
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
resp *types.SpaceAPIState,
) func(http.ResponseWriter, *http.Request) {
@ -22,7 +22,7 @@ func StateOpen(
}
// Parse request body
newState, err := strconv.ParseBool(string(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)
@ -30,7 +30,28 @@ func StateOpen(
}
// Set SpaceAPI response values
resp.Open = newState
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()
}
}