示例#1
0
        public static async Task <Quote> GetQuoteOfTheDayAsync(string category = null)
        {
            string response = null;

            if (string.IsNullOrWhiteSpace(category))
            {
                response = await _http.GetStringAsync(_url).ConfigureAwait(false);
            }
            else
            {
                response = await _http.GetStringAsync($"{_url}?category={WebUtility.UrlEncode(category)}").ConfigureAwait(false);
            }

            QuoteApiResponse data = JsonConvert.DeserializeObject <QuoteApiResponse>(response);

            return(data?.Contents?.Quotes?.FirstOrDefault());
        }
示例#2
0
        public static async Task <Quote?> GetQuoteOfTheDayAsync(string?category = null)
        {
            if (_cache.TryGetValue("qotd", out Quote quote))
            {
                return(quote);
            }

            try {
                string?response = string.IsNullOrWhiteSpace(category)
                    ? await _http.GetStringAsync(QuoteUrl).ConfigureAwait(false)
                    : await _http.GetStringAsync($"{QuoteUrl}?category={WebUtility.UrlEncode(category)}").ConfigureAwait(false);

                QuoteApiResponse data = JsonConvert.DeserializeObject <QuoteApiResponse>(response);
                Quote?           q    = data?.Contents?.Quotes?.FirstOrDefault();
                if (q is { })
                {
                    _cache.Set("qotd", q, TimeSpan.FromMinutes(10));
                }
                return(q);
            } catch (Exception e) {