Exemplo n.º 1
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>
        /// <returns>
        /// A response
        /// </returns>
        internal Response <T> ItemResponseHandler <T>(Response <JObject> rawResult, JTokenConversionDelegate <T> converter)
        {
            // Parse the result if we got one...
            if (rawResult.Succeeded && 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, this.ClientSettings);
                        return(new Response <T>(rawResult.StatusCode, result, RequestId));
                    }

                    break;

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

                    break;

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

            return(this.ItemErrorResponseHandler <T>(rawResult));
        }
Exemplo 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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Searches for items
        /// </summary>
        /// <typeparam name="T">The type to return</typeparam>
        /// <param name="searchTerm">The search term.</param>
        /// <param name="genreId">The genre to filter the results by.</param>
        /// <param name="id">An artist or product id.</param>
        /// <param name="category">The category to filter the results by.</param>
        /// <param name="location">The location to filter the results by.</param>
        /// <param name="maxdistance">The max distance from the location to to filter the results by.</param>
        /// <param name="orderBy">The field to sort the items by.</param>
        /// <param name="sortOrder">The sort order of the items to fetch.</param>
        /// <param name="startIndex">The zero-based start index to fetch items from (e.g. to get the second page of 10 items, pass in 10).</param>
        /// <param name="itemsPerPage">The number of items to fetch.</param>
        /// <param name="converter">The object creation method to use</param>
        /// <param name="callback">The callback to use when the API call has completed</param>
        protected void InternalSearch <T>(string searchTerm, string genreId, string id, Category?category, string location, string maxdistance, OrderBy?orderBy, SortOrder?sortOrder, int startIndex, int itemsPerPage, JTokenConversionDelegate <T> converter, Action <ListResponse <T> > callback)
        {
            // Build querystring parameters...
            var parameters = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>(PagingStartIndex, startIndex.ToString(CultureInfo.InvariantCulture)),
                new KeyValuePair <string, string>(PagingItemsPerPage, itemsPerPage.ToString(CultureInfo.InvariantCulture))
            };

            if (!string.IsNullOrEmpty(searchTerm))
            {
                parameters.Add(new KeyValuePair <string, string>(ParamSearchTerm, searchTerm));
            }

            if (!string.IsNullOrEmpty(genreId))
            {
                parameters.Add(new KeyValuePair <string, string>(ParamGenre, genreId));
            }

            if (!string.IsNullOrEmpty(id))
            {
                parameters.Add(new KeyValuePair <string, string>(ParamId, id));
            }

            if (category != null && category.Value != Types.Category.Unknown)
            {
                parameters.Add(new KeyValuePair <string, string>(ParamCategory, category.Value.ToString().ToLowerInvariant()));
            }

            if (orderBy != null && orderBy.HasValue)
            {
                parameters.Add(new KeyValuePair <string, string>(ParamOrderBy, orderBy.Value.ToString().ToLowerInvariant()));
            }

            if (sortOrder != null && sortOrder.HasValue)
            {
                parameters.Add(new KeyValuePair <string, string>(ParamSortOrder, sortOrder.Value.ToString().ToLowerInvariant()));
            }

            if (!string.IsNullOrEmpty(location))
            {
                parameters.Add(new KeyValuePair <string, string>(ParamLocation, location));
            }

            if (!string.IsNullOrEmpty(maxdistance))
            {
                parameters.Add(new KeyValuePair <string, string>(ParamMaxDistance, maxdistance));
            }

            this.RequestHandler.SendRequestAsync(
                this,
                this.ClientSettings,
                parameters,
                new JsonResponseCallback(rawResult => this.ListItemResponseHandler <T>(rawResult, ArrayNameItems, converter, callback)));
        }