コード例 #1
0
 /// <summary>
 ///   Constructor. Initializes a new instance of ApiException class
 /// </summary>
 /// <param name="error"> Api error </param>
 /// <param name="httpStatus"> HTTP response status </param>
 /// <param name="inner"> Inner exception that triggered the exception </param>
 public ApiException(ApiError error, HttpStatusCode httpStatus, Exception inner)
     : base(error != null ? error.Reason : null, inner)
 {
     ApiError   = error;
     HttpStatus = httpStatus;
 }
コード例 #2
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("path");
            }

            if (_cacheManager != null && objectToRefresh == null)
            {
                object cachedObject = await GetObjectFromCacheAsync(path).ConfigureAwait(false);

                if (cachedObject != null)
                {
                    return(cachedObject);
                }
            }

            var handler = new HttpClientHandler();

            handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            using (var client = new HttpClient(handler))
            {
                var objectApiResponse = objectToRefresh as ApiResponse;
                // Set If-Modified-Since header
                if (objectApiResponse != null && path == objectApiResponse.Path && objectApiResponse.LastModifiedUtc != DateTime.MinValue)
                {
                    client.DefaultRequestHeaders.IfModifiedSince = objectApiResponse.LastModifiedUtc;
                }

                var uri             = SetupRequest(client, path);
                var responseMessage = await client.GetAsync(uri).ConfigureAwait(false);

                if (responseMessage.StatusCode == HttpStatusCode.NotModified)
                {
                    return(objectToRefresh);
                }
                else if (responseMessage.IsSuccessStatusCode)
                {
                    object obj;
                    if (typeof(ApiResponse).IsAssignableFrom(objectType))
                    {
                        obj = await DeserializeResponse(path, objectType, responseMessage).ConfigureAwait(false);
                    }
                    else
                    {
                        obj = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
                    }
                    if (_cacheManager != null)
                    {
                        try
                        {
                            await _cacheManager.AddDataAsync(Region.Name + "/" + Locale + "/" + path, obj).ConfigureAwait(false);
                        }
                        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).ConfigureAwait(false);
                    }
                    catch (JsonException)
                    {
                        // Failed to deserialize error
                        apiError = null;
                    }

                    throw new ApiException(apiError, responseMessage.StatusCode, null);
                }
            }
        }