Example #1
0
        public async Task QueueTask_MultipleThreads_OrderedResponse()
        {
            /*** Arrange ***/
            int numTasks = 1000;

            int count = 0;

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

            /*** Act ***/
            IApiRequest request = new ApiRequest(new Uri("http://example.com"), "folders");

            List <Task <IApiResponse <OAuth2Session> > > tasks = new List <Task <IApiResponse <OAuth2Session> > >();

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

            await Task.WhenAll(tasks);

            /*** Assert ***/
            for (int i = 0; i < numTasks; i++)
            {
                OAuth2Session session = _converter.Parse <OAuth2Session>(tasks[i].Result.ContentString);
                Assert.AreEqual(session.AccessToken, i.ToString());
            }
        }
Example #2
0
        public void ProcessPictureQuery()
        {
            var json = GetPictureQueryItemJson();
            var picturesQueryJson  = _jsonConverter.Parse(json.Result);
            var picturesQueryItems = _jsonConverter.Convert(picturesQueryJson);

            mPictureQueryItems = _pictureTransfer.GetPictures(picturesQueryItems).Result;
        }
Example #3
0
        /// <summary>
        /// Parses the ApiResponse 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 IApiResponse <T> ParseResults <T>(this IApiResponse <T> response, IJsonConverter 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 <ApiError>(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.ErrorReportId))
                    {
                        throw new CanvasException(string.Join("; ", response.Error.Errors.Select(e => e.Message)))
                              {
                                  StatusCode = response.StatusCode
                              }
                    }
                    ;
                    // Throw error with full response if error object not available
                    throw new CanvasException(response.ContentString)
                          {
                              StatusCode = response.StatusCode
                          };
                }
                throw new CanvasException()
                      {
                          StatusCode = response.StatusCode
                      };
            }
            return(response);
        }