private async Task <IBoxResponse <T> > ExecuteRequest <T>(IBoxRequest request, bool queueRequest) where T : class { var response = queueRequest ? await _service.EnqueueAsync <T>(request).ConfigureAwait(false) : await _service.ToResponseAsync <T>(request).ConfigureAwait(false); switch (response.Status) { // Refresh the access token if the status is "Unauthorized" (HTTP Status Code 401: Unauthorized) // This will only be attempted once as refresh tokens are single use case ResponseStatus.Unauthorized: response = await RetryExpiredTokenRequest <T>(request).ConfigureAwait(false); break; // Continue to retry the request if the status is "Pending" (HTTP Status Code 202: Approved) // this will occur if a preview/thumbnail is not ready yet case ResponseStatus.Pending: response = await ExecuteRequest <T>(request, queueRequest).ConfigureAwait(false); break; } return(response); }
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()); } }
private async Task <IBoxResponse <T> > ExecuteRequest <T>(IBoxRequest request, bool queueRequest) where T : class { var response = queueRequest ? await _service.EnqueueAsync <T>(request).ConfigureAwait(false) : await _service.ToResponseAsync <T>(request).ConfigureAwait(false); if (response.Status == ResponseStatus.Unauthorized) { // Refresh the access token if the status is "Unauthorized" (HTTP Status Code 401: Unauthorized) // This will only be attempted once as refresh tokens are single use response = await RetryExpiredTokenRequest <T>(request).ConfigureAwait(false); } return(response); }
private async Task <IBoxResponse <T> > ExecuteRequest <T>(IBoxRequest request, bool queueRequest) where T : class { var response = queueRequest ? await _service.EnqueueAsync <T>(request).ConfigureAwait(false) : await _service.ToResponseAsync <T>(request).ConfigureAwait(false); switch (response.Status) { // Continue to retry the request if the status is "Pending" (HTTP Status Code 202: Approved) // this will occur if a preview/thumbnail is not ready yet case ResponseStatus.Pending: response = await ExecuteRequest <T>(request, queueRequest).ConfigureAwait(false); break; } return(response); }