예제 #1
0
        /// <summary>
        /// Performs Http Get request asynchronously
        /// </summary>
        /// <param name="path">relative URL of the object to get</param>
        /// <param name="objectType">object type</param>
        /// <param name="objectToRefresh">object to refresh</param>
        /// <returns>A task object for the async HTTP get</returns>
        internal async Task <object> GetAsync(string path, Type objectType, object objectToRefresh)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (_cacheManager != null && objectToRefresh == null)
            {
                var cachedObject = await GetObjectFromCacheAsync(path);

                if (cachedObject != null)
                {
                    return(cachedObject);
                }
            }
            var handler = new HttpClientHandler
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };

            using (var client = new HttpClient(handler))
            {
                var objectApiResponse = objectToRefresh as ApiResponse;
                if (objectApiResponse != null && path == objectApiResponse.Path &&
                    objectApiResponse.LastModifiedUtc != DateTime.MinValue)
                {
                    client.DefaultRequestHeaders.IfModifiedSince = objectApiResponse.LastModifiedUtc;
                }
                string uri;
                if (path.EndsWith(".json"))
                {
                    uri = path;
                }
                else
                {
                    uri = "https://" + Region.Host + path;
                }
                var responseMessage = await client.GetAsync(uri);

                if (responseMessage.StatusCode == HttpStatusCode.NotModified)
                {
                    return(objectToRefresh);
                }
                else if (responseMessage.IsSuccessStatusCode)
                {
                    object obj;
                    if (typeof(ApiResponse).IsAssignableFrom(objectType))
                    {
                        obj = await DeserializeResponse(path, objectType, responseMessage);
                    }
                    else
                    {
                        obj = await responseMessage.Content.ReadAsByteArrayAsync();
                    }
                    if (_cacheManager != null)
                    {
                        try
                        {
                            await _cacheManager.AddDataAsync(Region.Name
                                                             + "/" + Locale + "/" + path, obj);
                        }
                        catch (CacheManagerException)
                        {
                            // if we failed to add item to cache, swallow and return normally
                        }
                    }
                    return(obj);
                }
                else
                {
                    ApiError apiError = null;
                    try
                    {
                        apiError = (ApiError) await DeserializeResponse(path, typeof(ApiError), responseMessage);
                    }
                    catch (JsonException)
                    {
                        // Failed to deserialize error
                        apiError = null;
                    }
                    throw new ApiException(apiError, responseMessage.StatusCode, null);
                }
            }
        }