Make the API compliant with the 0.14 spec

This commit is contained in:
Lomanic 2022-09-27 01:30:28 +02:00
parent 63e7112680
commit 399f5718f6
1 changed files with 21 additions and 24 deletions

45
main.go
View File

@ -17,14 +17,15 @@ type Config struct {
} }
type SpaceAPI struct { type SpaceAPI struct {
API string `json:"api"` API string `json:"api"`
Space string `json:"space"` APICompatibility []string `json:"api_compatibility,omitempty"`
Logo string `json:"logo"` Space string `json:"space"`
URL string `json:"url"` Logo string `json:"logo"`
Location struct { URL string `json:"url"`
Location struct {
Address string `json:"address,omitempty"` Address string `json:"address,omitempty"`
Lon float64 `json:"lon"`
Lat float64 `json:"lat"` Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
} `json:"location"` } `json:"location"`
Contact struct { Contact struct {
Email string `json:"email,omitempty"` Email string `json:"email,omitempty"`
@ -39,7 +40,7 @@ type SpaceAPI struct {
Open string `json:"open"` Open string `json:"open"`
Closed string `json:"closed"` Closed string `json:"closed"`
} `json:"icon,omitempty"` } `json:"icon,omitempty"`
Open bool `json:"open"` Open *bool `json:"open"`
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
LastChange int64 `json:"lastchange,omitempty"` LastChange int64 `json:"lastchange,omitempty"`
} `json:"state"` } `json:"state"`
@ -88,24 +89,20 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
resp, err := http.Get(config.PRESENCEAPI) resp, err := http.Get(config.PRESENCEAPI)
if err != nil { if err == nil {
w.WriteHeader(http.StatusServiceUnavailable) defer resp.Body.Close()
fmt.Fprintf(w, "err: unable to request presence API: %v", err) var status Status
return err = json.NewDecoder(resp.Body).Decode(&status)
} if err == nil {
defer resp.Body.Close() spaceAPI.State.Open = &status.FuzIsOpen
var status Status if status.FuzIsOpen {
err = json.NewDecoder(resp.Body).Decode(&status) spaceAPI.State.LastChange = status.LastOpened.Unix()
if err != nil { } else {
w.WriteHeader(http.StatusServiceUnavailable) spaceAPI.State.LastChange = status.LastClosed.Unix()
fmt.Fprintf(w, "err: bad json from presence API: %v", err) }
return }
}
spaceAPI.State.Open = status.FuzIsOpen
if status.FuzIsOpen {
spaceAPI.State.LastChange = status.LastOpened.Unix()
} else { } else {
spaceAPI.State.LastChange = status.LastClosed.Unix() spaceAPI.State.Open = nil
} }
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")