9
0
mirror of https://github.com/Lomanic/fuz-spaceapi synced 2024-11-22 08:17:30 +00:00

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

23
main.go
View File

@ -18,13 +18,14 @@ type Config struct {
type SpaceAPI struct { type SpaceAPI struct {
API string `json:"api"` API string `json:"api"`
APICompatibility []string `json:"api_compatibility,omitempty"`
Space string `json:"space"` Space string `json:"space"`
Logo string `json:"logo"` Logo string `json:"logo"`
URL string `json:"url"` URL string `json:"url"`
Location struct { 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,25 +89,21 @@ 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)
fmt.Fprintf(w, "err: unable to request presence API: %v", err)
return
}
defer resp.Body.Close() defer resp.Body.Close()
var status Status var status Status
err = json.NewDecoder(resp.Body).Decode(&status) err = json.NewDecoder(resp.Body).Decode(&status)
if err != nil { if err == nil {
w.WriteHeader(http.StatusServiceUnavailable) spaceAPI.State.Open = &status.FuzIsOpen
fmt.Fprintf(w, "err: bad json from presence API: %v", err)
return
}
spaceAPI.State.Open = status.FuzIsOpen
if status.FuzIsOpen { if status.FuzIsOpen {
spaceAPI.State.LastChange = status.LastOpened.Unix() spaceAPI.State.LastChange = status.LastOpened.Unix()
} else { } else {
spaceAPI.State.LastChange = status.LastClosed.Unix() spaceAPI.State.LastChange = status.LastClosed.Unix()
} }
}
} else {
spaceAPI.State.Open = nil
}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Origin", "*")