/// <summary>
        /// Parses the BoxResponse with the provided converter
        /// </summary>
        /// <typeparam name="T">The return type of the Box response</typeparam>
        /// <param name="response">The response to parse</param>
        /// <param name="converter">The converter to use for the conversion</param>
        /// <returns></returns>
        internal static IBoxResponse <T> ParseResults <T>(this IBoxResponse <T> response, IBoxConverter converter)
            where T : class
        {
            switch (response.Status)
            {
            case ResponseStatus.Success:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    response.ResponseObject = converter.Parse <T>(response.ContentString);
                }
                break;

            case ResponseStatus.Error:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    response.Error = converter.Parse <BoxError>(response.ContentString);
                    if (response.Error != null && !string.IsNullOrWhiteSpace(response.Error.Name))
                    {
                        throw new BoxException(string.Format("{0}: {1}", response.Error.Name, response.Error.Description));
                    }
                    throw new BoxException(response.ContentString);
                }
                break;
            }
            return(response);
        }
예제 #2
0
        /// <summary>
        /// Parses the BoxResponse with the provided converter
        /// </summary>
        /// <typeparam name="T">The return type of the Box response</typeparam>
        /// <param name="response">The response to parse</param>
        /// <param name="converter">The converter to use for the conversion</param>
        /// <returns></returns>
        public static IBoxResponse <T> ParseResults <T>(this IBoxResponse <T> response, IBoxConverter converter)
            where T : class
        {
            BoxException exToThrow = null;

            switch (response.Status)
            {
            case ResponseStatus.Success:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    response.ResponseObject = converter.Parse <T>(response.ContentString);
                }
                break;

            default:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    try
                    {
                        switch (response.StatusCode)
                        {
                        case System.Net.HttpStatusCode.Conflict:
                            BoxConflictError <T> error = converter.Parse <BoxConflictError <T> >(response.ContentString);
                            exToThrow = new BoxConflictException <T>(response.ContentString, error);
                            break;

                        default:
                            response.Error = converter.Parse <BoxError>(response.ContentString);
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        Debug.WriteLine(string.Format("Unable to parse error message: {0}", response.ContentString));
                    }

                    throw exToThrow == null ?
                          new BoxException(response.ContentString, response.Error)
                          {
                              StatusCode = response.StatusCode
                          } :
                          exToThrow;
                }
                throw new BoxException(response.ContentString)
                      {
                          StatusCode = response.StatusCode
                      };
            }
            return(response);
        }
예제 #3
0
        public async Task QueueTask_MultipleThreads_OrderedResponse()
        {
            /*** Arrange ***/
            var numTasks = 1000;

            var count = 0;

            // Increments the access token each time a call is made to the API
            _handler.Setup(h => h.ExecuteAsync <OAuthSession>(It.IsAny <IBoxRequest>()))
            .Returns(() => Task.FromResult <IBoxResponse <OAuthSession> >(new BoxResponse <OAuthSession>()
            {
                Status        = ResponseStatus.Success,
                ContentString = "{\"access_token\": \"" + count + "\",\"expires_in\": 3600,\"token_type\": \"bearer\",\"refresh_token\": \"J7rxTiWOHMoSC1isKZKBZWizoRXjkQzig5C6jFgCVJ9bUnsUfGMinKBDLZWP9BgR\"}"
            })).Callback(() => System.Threading.Interlocked.Increment(ref count));

            /*** Act ***/
            IBoxRequest request = new BoxRequest(new Uri("http://box.com"), "folders");

            var tasks = new List <Task <IBoxResponse <OAuthSession> > >();

            for (var i = 0; i < numTasks; i++)
            {
                tasks.Add(_service.EnqueueAsync <OAuthSession>(request));
            }

            await Task.WhenAll(tasks);

            /*** Assert ***/
            for (var i = 0; i < numTasks; i++)
            {
                OAuthSession session = _converter.Parse <OAuthSession>(tasks[i].Result.ContentString);
                Assert.AreEqual(session.AccessToken, i.ToString());
            }
        }
예제 #4
0
        /// <summary>
        /// Parses the BoxResponse with the provided converter
        /// </summary>
        /// <typeparam name="T">The return type of the Box response</typeparam>
        /// <param name="response">The response to parse</param>
        /// <param name="converter">The converter to use for the conversion</param>
        /// <returns></returns>
        internal static IBoxResponse <T> ParseResults <T>(this IBoxResponse <T> response, IBoxConverter converter)
            where T : class
        {
            switch (response.Status)
            {
            case ResponseStatus.Success:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    response.ResponseObject = converter.Parse <T>(response.ContentString);
                }
                break;

            case ResponseStatus.Error:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    try
                    {
                        response.Error = converter.Parse <BoxError>(response.ContentString);
                    }
                    catch (Exception)
                    {
                        Debug.WriteLine(string.Format("Unable to parse error message: {0}", response.ContentString));
                    }

                    // Throw formatted error if available
                    if (response.Error != null && !string.IsNullOrWhiteSpace(response.Error.Name))
                    {
                        throw new BoxException(string.Format("{0}: {1}", response.Error.Name, response.Error.Description))
                              {
                                  StatusCode = response.StatusCode
                              }
                    }
                    ;
                    // Throw error with full response if error object not available
                    throw new BoxException(response.ContentString)
                          {
                              StatusCode = response.StatusCode
                          };
                }
                throw new BoxException()
                      {
                          StatusCode = response.StatusCode
                      };
            }
            return(response);
        }
        /// <summary>
        /// Parses the BoxResponse with the provided converter
        /// </summary>
        /// <typeparam name="T">The return type of the Box response</typeparam>
        /// <param name="response">The response to parse</param>
        /// <param name="converter">The converter to use for the conversion</param>
        /// <returns></returns>
        public static IBoxResponse <T> ParseResults <T>(this IBoxResponse <T> response, IBoxConverter converter)
            where T : class
        {
            BoxException exToThrow = null;

            switch (response.Status)
            {
            case ResponseStatus.Success:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    response.ResponseObject = converter.Parse <T>(response.ContentString);
                }
                break;

            case ResponseStatus.Forbidden:
                var errorMsg = response.Headers.WwwAuthenticate.FirstOrDefault();
                if (errorMsg != null)
                {
                    var err = new BoxError()
                    {
                        Code = response.StatusCode.ToString(), Description = "Forbidden", Message = errorMsg.ToString()
                    };
                    throw new BoxException(err.Message, err);
                }
                else if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    response.Error = converter.Parse <BoxError>(response.ContentString);
                    throw new BoxException(response.ContentString, response.Error)
                          {
                              StatusCode = response.StatusCode
                          };
                }
                else
                {
                    throw new BoxException("Forbidden");
                }

            default:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    try
                    {
                        switch (response.StatusCode)
                        {
                        case System.Net.HttpStatusCode.Conflict:
                            if (response is IBoxResponse <BoxPreflightCheck> )
                            {
                                BoxPreflightCheckConflictError <BoxFile> err = converter.Parse <BoxPreflightCheckConflictError <BoxFile> >(response.ContentString);
                                exToThrow = new BoxPreflightCheckConflictException <BoxFile>(response.ContentString, err);
                            }
                            else
                            {
                                BoxConflictError <T> error = converter.Parse <BoxConflictError <T> >(response.ContentString);
                                exToThrow = new BoxConflictException <T>(response.ContentString, error);
                            }

                            break;

                        default:
                            response.Error = converter.Parse <BoxError>(response.ContentString);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(string.Format("Unable to parse error message: {0}", response.ContentString));
                    }

                    throw exToThrow == null ?
                          new BoxException(response.ContentString, response.Error)
                          {
                              StatusCode = response.StatusCode
                          } :
                          exToThrow;
                }
                throw new BoxException(response.ContentString)
                      {
                          StatusCode = response.StatusCode
                      };
            }
            return(response);
        }
        /// <summary>
        /// Parses the BoxResponse with the provided converter
        /// </summary>
        /// <typeparam name="T">The return type of the Box response</typeparam>
        /// <param name="response">The response to parse</param>
        /// <param name="converter">The converter to use for the conversion</param>
        /// <returns></returns>
        internal static IBoxResponse <T> ParseResults <T>(this IBoxResponse <T> response, IBoxConverter converter)
            where T : class
        {
            switch (response.Status)
            {
            case ResponseStatus.Success:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    response.ResponseObject = converter.Parse <T>(response.ContentString);
                }
                break;

            case ResponseStatus.RateLimitReached:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    int retryAfter = int.Parse(response.Headers
                                               .Where(x => x.Key == "Retry-After")
                                               .SelectMany(x => x.Value)
                                               .FirstOrDefault() ?? "20");

                    response.Error = converter.Parse <BoxError>(response.ContentString);
                    if (response.Error != null && !string.IsNullOrWhiteSpace(response.Error.Name))
                    {
                        throw new BoxRateLimitingException(string.Format("{0}: {1}", response.Error.Name, response.Error.Description), retryAfter);
                    }
                    throw new BoxRateLimitingException(response.ContentString, retryAfter);
                }
                break;

            case ResponseStatus.Unauthorized:
            {
                response.Error = converter.Parse <BoxError>(response.ContentString);
                if (response.Error != null && !string.IsNullOrWhiteSpace(response.Error.Name))
                {
                    throw new AccessTokenExpiredException(string.Format("{0}: {1}", response.Error.Name, response.Error.Description));
                }
                throw new AccessTokenExpiredException(response.ContentString);
            }

            case ResponseStatus.Error:
                if (!string.IsNullOrWhiteSpace(response.ContentString))
                {
                    try
                    {
                        response.Error = converter.Parse <BoxError>(response.ContentString);
                    }
                    catch (Exception)
                    {
                        Debug.WriteLine(string.Format("Unable to parse error message: {0}", response.ContentString));
                    }

                    // Throw formatted error if available
                    if (response.Error != null && !string.IsNullOrWhiteSpace(response.Error.Name))
                    {
                        throw new BoxException(string.Format("{0}: {1}", response.Error.Name, response.Error.Description))
                              {
                                  StatusCode = response.StatusCode
                              }
                    }
                    ;
                    // Throw error with full response if error object not available
                    throw new BoxException(response.ContentString)
                          {
                              StatusCode = response.StatusCode
                          };
                }
                throw new BoxException()
                      {
                          StatusCode = response.StatusCode
                      };
            }
            return(response);
        }