private async Task <Json.CreateRecordResponse> GetResponseUsingBatches(CreateRecordRequest request, Func <PrivateConnection> connectionFactory, Json.CreateRecordRequest model) { Json.CreateRecordResponse responseObject = default(Json.CreateRecordResponse); var batchSizePerResource = BatchSize / model.Data.Count; for (var i = 0; i < model.Data.Max(x => x.Entries.Count); i += batchSizePerResource) { Json.CreateRecordRequest partialRequest = CreateAndInitializePartialRequest(model, batchSizePerResource, i); using (var connection = connectionFactory().ApiName(ApiName).Method(PrivateConnectionMethod.Create)) { try { responseObject = await SendPartialRequestAndUpdateResults(responseObject, partialRequest, connection); } catch (ConnectionResponseException e) { throw e.ToRequestException("Could not Create Record", request.WithoutFieldIdResolver); } } } return(responseObject); }
private Json.CreateRecordRequest CreateAndInitializePartialRequest(Json.CreateRecordRequest model, int batchSizePerResource, int i) { var partialRequest = new Json.CreateRecordRequest { Data = new List <CreateRecordRequestData>() }; foreach (var resourceItem in model.Data) { if (i >= resourceItem.Entries.Count) { continue; } var currentBatchSize = Math.Min(batchSizePerResource, resourceItem.Entries.Count - i); partialRequest.Data.Add(new CreateRecordRequestData { ResourceId = resourceItem.ResourceId, Entries = resourceItem.Entries.Skip(i).Take(currentBatchSize).ToList() }); } return(partialRequest); }
private async Task <Json.CreateRecordResponse> SendPartialRequestAndUpdateResults(Json.CreateRecordResponse responseObject, Json.CreateRecordRequest partialRequest, PrivateConnection connection) { using (var response = await connection.SendAsync(partialRequest)) { if (responseObject.ResultIds == null) { responseObject = await response.ReadAsync <Json.CreateRecordResponse>(); } else { var newResponse = await response.ReadAsync <Json.CreateRecordResponse>(); for (var j = 0; j < responseObject.ResultIds.Count; j++) { responseObject.ResultIds[j].AddRange(newResponse.ResultIds[j]); } } } return(responseObject); }