2016-11-11 14:14:38 +01:00
|
|
|
package endpoints
|
2016-10-23 10:42:06 +02:00
|
|
|
|
2018-07-27 16:21:27 +02:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/alice-lg/birdwatcher/bird"
|
|
|
|
)
|
|
|
|
|
2016-10-23 10:42:06 +02:00
|
|
|
type TimeInfo struct {
|
2018-07-27 16:21:27 +02:00
|
|
|
Date time.Time `json:"date"`
|
|
|
|
TimezoneType string `json:"timezone_type"`
|
|
|
|
Timezone string `json:"timezone"`
|
2016-10-23 10:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type CacheStatus struct {
|
|
|
|
CachedAt TimeInfo `json:"cached_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type APIInfo struct {
|
|
|
|
Version string
|
|
|
|
ResultFromCache bool `json:"result_from_cache"`
|
|
|
|
CacheStatus CacheStatus `json:"cache_status"`
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:12:02 +01:00
|
|
|
// go generate does not work in subdirectories. Beautious.
|
|
|
|
var VERSION string
|
|
|
|
|
2018-07-27 16:21:27 +02:00
|
|
|
func GetApiInfo(res *bird.Parsed, from_cache bool) *APIInfo {
|
2016-10-23 10:42:06 +02:00
|
|
|
ai := &APIInfo{}
|
|
|
|
|
2017-01-26 12:12:02 +01:00
|
|
|
ai.Version = VERSION
|
2016-11-11 17:47:51 +01:00
|
|
|
ai.ResultFromCache = from_cache
|
2016-10-23 10:42:06 +02:00
|
|
|
|
2018-07-27 16:21:27 +02:00
|
|
|
api := *res
|
|
|
|
|
|
|
|
// Derive cache status from TTL
|
|
|
|
cachedAt, ok := api["cached_at"].(time.Time)
|
|
|
|
if !ok {
|
|
|
|
cachedAt = time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// tbh. I have no clue what the difference between
|
|
|
|
// timezone type and timezone actually is.
|
|
|
|
// I could trace back the timezonetype to the symphony framework
|
|
|
|
// Barry was using; the docs say it accepts timezones like
|
|
|
|
// "America/New_York", however nothing about UTC could be found.
|
|
|
|
//
|
|
|
|
// As we convert everything to UTC and let the client
|
|
|
|
// render it in local time, it is safe to set this to a fixed
|
|
|
|
// value.
|
|
|
|
|
|
|
|
cacheInfo := CacheStatus{
|
|
|
|
CachedAt: TimeInfo{
|
|
|
|
Date: cachedAt,
|
|
|
|
TimezoneType: "UTC",
|
|
|
|
Timezone: "UTC",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ai.CacheStatus = cacheInfo
|
|
|
|
|
2016-10-23 10:42:06 +02:00
|
|
|
return ai
|
|
|
|
}
|