From 89580c8941bbaa07c6e9eb46213ff933cc266cc9 Mon Sep 17 00:00:00 2001 From: Matthias Hannig Date: Fri, 27 Jul 2018 17:06:05 +0200 Subject: [PATCH] include cache status from API --- backend/sources/birdwatcher/parsers.go | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/backend/sources/birdwatcher/parsers.go b/backend/sources/birdwatcher/parsers.go index 759419b..f04114c 100644 --- a/backend/sources/birdwatcher/parsers.go +++ b/backend/sources/birdwatcher/parsers.go @@ -53,6 +53,7 @@ func parseApiStatus(bird ClientResponse, config Config) (api.ApiStatus, error) { return status, fmt.Errorf(birdErr) } + // Parse TTL ttl, err := parseServerTime( bird["ttl"], config.ServerTime, @@ -62,10 +63,42 @@ func parseApiStatus(bird ClientResponse, config Config) (api.ApiStatus, error) { return api.ApiStatus{}, err } + // Parse Cache Status + cacheStatus, err := parseCacheStatus(birdApi, config) + if err != nil { + return api.ApiStatus{}, err + } + status := api.ApiStatus{ Version: birdApi["Version"].(string), ResultFromCache: birdApi["result_from_cache"].(bool), Ttl: ttl, + CacheStatus: cacheStatus, + } + + return status, nil +} + +// Parse cache status from api response +func parseCacheStatus(cacheStatus map[string]interface{}, config Config) (api.CacheStatus, error) { + cache, ok := cacheStatus["cache_status"].(map[string]interface{}) + if !ok { + return api.CacheStatus{}, fmt.Errorf("Invalid Cache Status") + } + + cachedAt, ok := cache["cached_at"].(map[string]interface{}) + if !ok { + return api.CacheStatus{}, fmt.Errorf("Invalid Cache Status") + } + + cachedAtTime, err := parseServerTime(cachedAt["date"], config.ServerTime, config.Timezone) + if err != nil { + return api.CacheStatus{}, err + } + + status := api.CacheStatus{ + CachedAt: cachedAtTime, + // We ommit OrigTTL for now... } return status, nil