Beispiel #1
0
        public async Task <ResponseBase> MakeRequestAsync(RequestBase request)
        {
            if (string.IsNullOrEmpty(DataDragonVersion))
            {
                throw new Exception("Data dragon version must be set before making requests");
            }

            ResponseBase cacheResponse;

            // Check cache first, return response if found
            try
            {
                cacheResponse = _cacheClient.CheckImageCache(request);
            }
            catch (OutOfMemoryException ex)
            {
                return(new ResponseBase()
                {
                    Exception = ex,
                    IsFaulted = true
                });
            }

            if (!cacheResponse.IsFaulted)
            {
                return(cacheResponse);
            }

            // Does not exist in cache, make download request
            try
            {
                return(await _downloadClient.DownloadIconImageAsync(request, DataDragonVersion));
            }
            catch (Exception ex)
            {
                return(new ResponseBase()
                {
                    Exception = ex,
                    IsFaulted = true
                });
            }
        }
Beispiel #2
0
        public async Task <ResponseBase> MakeRequestAsync(RequestBase request)
        {
            // This acts as the key to tell if a download is in progress
            string requestId = GetRequestIdentifier(request);

            if (requestId == "0" || requestId is null)
            {
                _log.Warning($"Invalid requestId: {requestId}");
                return(new ResponseBase()
                {
                    Exception = new Exception($"requestId is not valid: {requestId}"),
                    Request = request,
                    IsFaulted = true
                });
            }

            // 1. If a download is in progress, use the same task to get the result
            if (_inProgressTasks.ContainsKey(requestId))
            {
                // Get the matching in progress task
                Task <ResponseBase> responseTask = _inProgressTasks[requestId];

                // If the task is complete, remove it
                if (responseTask.IsCompleted)
                {
                    if (!_inProgressTasks.TryRemove(requestId, out _))
                    {
                        _log.Warning($"Failed to remove in progress task {requestId}");
                    }
                }

                // Get the result of the task and return it
                ResponseBase result = await responseTask.ConfigureAwait(true);

                return(result);
            }

            // 2. A download is not in progress, is it cached?
            ResponseBase cacheResponse = _cacheClient.CheckImageCache(request);

            // Fault occurs if cache is unable to find the file, or if the file is corrupted
            if (!cacheResponse.IsFaulted)
            {
                return(cacheResponse);
            }

            // 3. Does not exist in cache, make download request
            try
            {
                Task <ResponseBase> responseTask = _downloadClient.DownloadIconImageAsync(request);
                if (!_inProgressTasks.TryAdd(requestId, responseTask))
                {
                    _log.Warning($"Failed to add in progress task {requestId}");
                }

                ResponseBase result = await responseTask.ConfigureAwait(true);

                return(result);
            }
            catch (Exception ex)
            {
                _log.Error($"Failed to download {requestId}. Ex: {ex}");
                return(new ResponseBase()
                {
                    Exception = ex,
                    Request = request,
                    IsFaulted = true
                });
            }
        }