Ejemplo n.º 1
0
 private void AddToDict(string key, ActiveMinutes activeMinutes)
 {
     if (ActiveMinutes.ContainsKey(key))
     {
         ActiveMinutes[key] = activeMinutes;
     }
     else
     {
         ActiveMinutes.Add(key, activeMinutes);
     }
 }
Ejemplo n.º 2
0
        public async Task <ActiveMinutes> GetActiveMinutes(string type, string token, string date = "today", string time = "7d")
        {
            if (_cache.TryGetValue("fitbit_active_" + type, out ActiveMinutes active))
            {
                _logger.LogInformation($"[CACHE] Got fitbit_active_{type} from cache");
                return(active);
            }

            using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, $"https://api.fitbit.com/1/user/-/activities/{type}/date/{date}/{time}.json");
            request.Headers.Add("Authorization", $"Bearer {token}");

            HttpResponseMessage response = await _httpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                _logger.LogError("Error getting fitbit active data ({statusCode})", response.StatusCode);
                return(null);
            }

            string stringResponse = await response.Content.ReadAsStringAsync();

            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                PropertyNameCaseInsensitive = true
            };
            ActiveMinutes activeMinutesResponse = JsonSerializer.Deserialize <ActiveMinutes>(stringResponse, options);

            if (activeMinutesResponse != null)
            {
                _cache.Set("fitbit_active_" + type, activeMinutesResponse, new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)
                });
            }

            return(activeMinutesResponse);
        }