private async Task <BatchRequestResponse> SendBatchRequestAsync(Func <Task> executeRequests) { BatchApiRequest.StartNewBatchRequest(); try { bool isBatchSuccess = true; var data = new List <object>(); await executeRequests(); List <BatchApiResponse> batchResponses = await BatchApiRequest.SendBatchApiRequest(); for (int i = 0; i < batchResponses.Count; i++) { BatchApiResponse batchResponse = batchResponses[i]; if (batchResponse.status_code != 200) { isBatchSuccess = false; BatchApiRequest.AbortBatchRequest(); break; } var body = batchResponses[i].GetBody <object>(); data.Add(body); } var response = new BatchRequestResponse(isBatchSuccess, "200", data.ToJson()); return(response); } catch (AggregateException ex) { BatchApiRequest.AbortBatchRequest(); return(new BatchRequestResponse(false, "500", null, ex.ToString())); } catch (InvalidOperationException ex) { BatchApiRequest.AbortBatchRequest(); return(new BatchRequestResponse(false, "500", null, ex.ToString())); } }
/// <summary> /// Validates an individual batch API call response from the list that was received in the batch API call /// </summary> /// <typeparam name="T">The type to convert the BatchApiResponse's body to (the type that the individual API call returns)</typeparam> /// <param name="response">The individual batch API call</param> private void ValidateIndividualBatchedApiCallResponse <T>(BatchApiResponse response) { var responseBody = response.GetBody <T>(); SendwithusClientTest.ValidateResponse(responseBody); }
public GitLfsResult GetBatchApiResponse(string urlScheme, string urlAuthority, string requestApplicationPath, string repositoryName, string[] acceptTypes, BatchApiRequest requestObj, Guid userId) { // Validate the request. if (!acceptTypes .Select(at => at.Split(new[] { ';' })) .SelectMany(list => list) .Any(at => at.Equals("application/vnd.git-lfs+json"))) { return(GitLfsResult.From( Newtonsoft.Json.JsonConvert.SerializeObject( new BatchApiErrorResponse() { Message = "Invalid ContentType." }), 406, GitLfsConsts.GIT_LFS_CONTENT_TYPE)); } // Check permissions RepositoryAccessLevel accessLevelRequested; if (requestObj.Operation.Equals(LfsOperationNames.DOWNLOAD)) { accessLevelRequested = RepositoryAccessLevel.Pull; } else if (requestObj.Operation.Equals(LfsOperationNames.UPLOAD)) { accessLevelRequested = RepositoryAccessLevel.Push; } else { accessLevelRequested = RepositoryAccessLevel.Administer; } bool authorized = RepositoryPermissionService.HasPermission(userId, RepositoryRepository.GetRepository(repositoryName).Id, accessLevelRequested); if (!authorized) { return(GitLfsResult.From( Newtonsoft.Json.JsonConvert.SerializeObject( new BatchApiErrorResponse() { Message = "You do not have the required permissions." }), 403, GitLfsConsts.GIT_LFS_CONTENT_TYPE)); } if (requestObj == null) { return(GitLfsResult.From( Newtonsoft.Json.JsonConvert.SerializeObject( new BatchApiErrorResponse() { Message = "Cannot parse request body." }), 400, GitLfsConsts.GIT_LFS_CONTENT_TYPE)); } // Process the request. var requestedTransferAdapters = requestObj.Transfers ?? (new string[] { LfsTransferProviderNames.BASIC }); string firstSupportedTransferAdapter = requestedTransferAdapters.FirstOrDefault(t => t.Equals(LfsTransferProviderNames.BASIC)); if (firstSupportedTransferAdapter != null) { string transferAdapterToUse = firstSupportedTransferAdapter; var requiredSpace = DetermineRequiredDiskSpace(requestObj); if (StorageProvider.SufficientSpace(requiredSpace)) { BatchApiResponse responseObj = new BatchApiResponse() { Transfer = transferAdapterToUse, Objects = requestObj.Objects .Select(ro => new { ro, ActionFactory = new LfsActionFactory() }) .Select(x => new BatchApiResponse.BatchApiObject() { Oid = x.ro.Oid, Size = x.ro.Size, Authenticated = true, Actions = x.ActionFactory.CreateBatchApiObjectActions(urlScheme, urlAuthority, requestApplicationPath, requestObj.Operation, x.ro, repositoryName, StorageProvider) }) .ToArray() }; return(GitLfsResult.From(responseObj, 200, GitLfsConsts.GIT_LFS_CONTENT_TYPE)); } else { return(GitLfsResult.From(new BatchApiErrorResponse() { Message = "Insufficient storage space." }, 507, GitLfsConsts.GIT_LFS_CONTENT_TYPE)); } } else { // None of the requested transfer adapters are supported. return(GitLfsResult.From(new BatchApiErrorResponse() { Message = $"None of the requested transfer adapters are supported." }, 400, GitLfsConsts.GIT_LFS_CONTENT_TYPE)); } }