Esempio n. 1
0
        /// <summary>
        /// Generic response handler for content lists
        /// </summary>
        /// <typeparam name="T">The type to return</typeparam>
        /// <param name="rawResult">The response</param>
        /// <param name="itemsName">The json list name</param>
        /// <param name="converter">The object creation method to use</param>
        /// <param name="callback">The client callback</param>
        internal void ListItemResponseHandler <T>(Response <JObject> rawResult, string itemsName, JTokenConversionDelegate <T> converter, Action <ListResponse <T> > callback)
        {
            ListResponse <T> response = null;

            // Parse the result if we got one...
            if (rawResult.StatusCode.HasValue)
            {
                DebugLogger.Instance.WriteVerboseInfo("Parsing list response. Status Code: {0}", rawResult.StatusCode);
                switch (rawResult.StatusCode.Value)
                {
                case HttpStatusCode.OK:
                case HttpStatusCode.Accepted:
                case HttpStatusCode.Created:
                    if (this.IsValidContentType(rawResult))
                    {
                        DebugLogger.Instance.WriteVerboseInfo("Valid content type. Parsing...");
                        List <T> results      = this.JsonProcessor.ParseList(rawResult.Result, itemsName, converter);
                        int?     totalResults = null;
                        int?     startIndex   = null;
                        int?     itemsPerPage = null;

                        JToken paging = rawResult.Result["paging"];
                        if (paging != null)
                        {
                            totalResults = paging.Value <int>(MusicClientCommand.PagingTotal);
                            startIndex   = paging.Value <int>(MusicClientCommand.PagingStartIndex);
                            itemsPerPage = paging.Value <int>(MusicClientCommand.PagingItemsPerPage);
                        }

                        response = new ListResponse <T>(rawResult.StatusCode, results, startIndex, itemsPerPage, totalResults, RequestId);
                    }

                    break;

                case HttpStatusCode.NotFound:
                    if (this.ClientSettings.CountryCodeBasedOnRegionInfo)
                    {
                        response = new ListResponse <T>(rawResult.StatusCode, new ApiNotAvailableException(), rawResult.ErrorResponseBody, RequestId);
                    }

                    break;

                case HttpStatusCode.Unauthorized:
                case HttpStatusCode.Forbidden:
                    response = new ListResponse <T>(rawResult.StatusCode, new InvalidApiCredentialsException(), rawResult.ErrorResponseBody, RequestId);
                    break;
                }
            }

            if (response == null)
            {
                var ex = new ApiCallFailedException(rawResult.StatusCode);
                response = new ListResponse <T>(rawResult.StatusCode, ex, rawResult.ErrorResponseBody, RequestId);
            }

            callback(response);
        }
Esempio n. 2
0
        /// <summary>
        /// Generic response handler for single item content
        /// </summary>
        /// <typeparam name="T">The type to return</typeparam>
        /// <param name="rawResult">The response</param>
        /// <param name="converter">The object creation method to use</param>
        /// <param name="callback">The client callback</param>
        internal void ItemResponseHandler <T>(Response <JObject> rawResult, JTokenConversionDelegate <T> converter, Action <Response <T> > callback)
        {
            Response <T> response = null;

            // Parse the result if we got one...
            if (rawResult.StatusCode.HasValue)
            {
                switch (rawResult.StatusCode.Value)
                {
                case HttpStatusCode.OK:
                case HttpStatusCode.Accepted:
                case HttpStatusCode.Created:
                    if (this.IsValidContentType(rawResult))
                    {
                        T result = converter(rawResult.Result);
                        response = new Response <T>(rawResult.StatusCode, result, RequestId);
                    }

                    break;

                case HttpStatusCode.NotFound:
                    if (this.ClientSettings.CountryCodeBasedOnRegionInfo)
                    {
                        response = new Response <T>(rawResult.StatusCode, new ApiNotAvailableException(), rawResult.ErrorResponseBody, RequestId);
                    }

                    break;

                case HttpStatusCode.Forbidden:
                    response = new Response <T>(rawResult.StatusCode, new InvalidApiCredentialsException(), rawResult.ErrorResponseBody, RequestId);
                    break;
                }
            }

            if (response == null)
            {
                var ex = new ApiCallFailedException(rawResult.StatusCode);
                response = new Response <T>(rawResult.StatusCode, ex, rawResult.ErrorResponseBody, RequestId);
            }

            callback(response);
        }
Esempio n. 3
0
        public ErrorResponse CreateErrorResponse()
        {
            ErrorResponse returnValue = new ErrorResponse();

            Exception ex = _exceptionHandler.Error;

            if (ex as AggregateException != null)
            {
                AggregateException aggregateException = ex as AggregateException;
                returnValue.AddErrors(ProcessAggregateException(aggregateException));
            }

            if (ex as ApiCallFailedException != null)
            {
                ApiCallFailedException apiCallFailedException = ex as ApiCallFailedException;
                returnValue.AddError(ProcessApiCallFailedException(apiCallFailedException));
            }

            returnValue.AddError(_httpContext.TraceIdentifier, 500, ex.Message);

            return(returnValue);
        }
Esempio n. 4
0
        private ErrorDetails ProcessApiCallFailedException(ApiCallFailedException apiCallFailedException)
        {
            ErrorDetails returnValue = new ErrorDetails(_httpContext.TraceIdentifier, apiCallFailedException.StatusCode, apiCallFailedException.Message);

            return(returnValue);
        }