예제 #1
0
        public async Task <GifResult> GifFetch(string searchTerm)
        {
            _logger.LogInformation($"GifFetch search for {searchTerm} at {DateTime.UtcNow.ToLongTimeString()}");
            if (string.IsNullOrEmpty(searchTerm))
            {
                throw new FormatException("query term is required");
            }
            SearchParameters searchParameters = new SearchParameters();

            searchParameters.ApiKey = _apiKey;
            searchParameters.Query  = searchTerm;

            var searchString = WebTools.ToKeyValueURL(searchParameters);

            var result = await _webTools.GetData(new Uri($"{_giphyUrl}{searchString}"));

            if (!result.IsSuccess)
            {
                string message = $"GifFetch Failed to get GIFs: {result.Result}";
                _logger.LogError(message);
                throw new WebException(message);
            }

            GifResult gifResult = JsonConvert.DeserializeObject <GifResult>(result.Result);

            return(gifResult);
        }
예제 #2
0
        public async Task <ActionResult <GiphyItem> > GetGiphyItem(string searchTerm)
        {
            GiphyItem giphyItem = new GiphyItem();

            try
            {
                //giphyItem = await _context.GiphyItems.FindAsync(searchTerm);
                if (!_cache.TryGetValue(searchTerm, out giphyItem))
                {
                    GifResult gifResult = await _giphyTools.GifFetch(searchTerm);

                    if (gifResult != null)
                    {
                        giphyItem = new GiphyItem()
                        {
                            SearchTerm = searchTerm,
                            Url        = gifResult.Data.First().Url
                        };

                        var cacheEntryOptions = new MemoryCacheEntryOptions();

                        _cache.Set(searchTerm, giphyItem, cacheEntryOptions);

                        _context.GiphyItems.Add(giphyItem);
                    }
                }
            }
            catch (Exception ex)
            {
                string exc = ex.Message;
            }

            return(giphyItem);
        }
예제 #3
0
        public async Task <GifResult> GifFetch(string searchTerm)
        {
            if (string.IsNullOrEmpty(searchTerm))
            {
                throw new FormatException("query term is required");
            }
            SearchParameters searchParameters = new SearchParameters();

            searchParameters.ApiKey = _apiKey;
            searchParameters.Query  = searchTerm;

            var searchString = WebTools.ToKeyValueURL(searchParameters);

            var result = await _webTools.GetData(new Uri($"{_giphyUrl}{searchString}"));

            if (!result.IsSuccess)
            {
                throw new WebException($"Failed to get GIFs: {result.Result}");
            }

            GifResult gifResult = JsonConvert.DeserializeObject <GifResult>(result.Result);

            return(gifResult);
        }
예제 #4
0
        public async Task <ActionResult <GiphyItem> > GetGiphyItem(string searchTerm)
        {
            _logger.LogInformation($"GetGiphyItem search for {searchTerm} at {DateTime.UtcNow.ToLongTimeString()}");

            GiphyItem giphyItem = new GiphyItem();

            try
            {
                if (!_cache.TryGetValue(searchTerm, out giphyItem))
                {
                    GifResult gifResult = await _giphyTools.GifFetch(searchTerm);

                    if (gifResult != null)
                    {
                        giphyItem = new GiphyItem()
                        {
                            SearchTerm = searchTerm,
                            Url        = gifResult.Data.First().Images.Original.Url
                        };

                        var cacheEntryOptions = new MemoryCacheEntryOptions();

                        _cache.Set(searchTerm, giphyItem, cacheEntryOptions);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"GetGiphyItem Exception: {ex.Message}");
                throw;
            }

            _logger.LogInformation($"GetGiphyItems find gif at url={giphyItem.Url}");

            return(giphyItem);
        }