예제 #1
0
        public async Task <BundesligaModel> GetCurrentPlayDayAsync()
        {
            MatchResults = await _soccerService.GetCurrentPlayDayAsync();

            RaiseOnChangeEvent();

            return(MatchResults);
        }
예제 #2
0
        public async Task <BundesligaModel> GetCurrentPlayDayAsync()
        {
            if (_cache.TryGetValue(SOCCER_CACHE_KEY, out BundesligaModel cachedModel))
            {
                _logger.LogInformation("[CACHE] Got soccer results from cache");
                return(cachedModel);
            }

            string baseUrl = "https://www.openligadb.de/api/getmatchdata/bl1";
            HttpResponseMessage response = await _httpClient.GetAsync(baseUrl);

            if (!response.IsSuccessStatusCode)
            {
                _logger.LogError("Error getting match data: {statusCode}", response.StatusCode);
                return(new BundesligaModel());
            }

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

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

            if (playDayResponse == null)
            {
                _logger.LogError("Error getting match data, playDayResponse is null.");
                return(new BundesligaModel());
            }

            _cache.Set(SOCCER_CACHE_KEY, playDayResponse, new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1)
            });

            _logger.LogInformation("Got playday results");
            return(playDayResponse);
        }