private void HappyOrSadPathTruths(BulkSyncResult result, BulkHubSpotContact[] hubSpotContacts, int expectedBatchCount, int successCount, int failureCount) { HappyOrSadPathTruths(result, hubSpotContacts.Length, expectedBatchCount, successCount, failureCount); // assert data result.BatchCount.Should().Be(expectedBatchCount); // assert behavior _httpMock.Verify(http => http.Post(It.IsAny <string>(), It.IsAny <BulkHubSpotContact[]>()), Times.Exactly(expectedBatchCount)); }
/// <summary> /// https://developers.hubspot.com/docs/methods/contacts/batch_create_or_update /// </summary> /// <param name="hubSpotContacts">Contacts to create/update in HubSpot.</param> /// <param name="batchSize">Number of contacts to send to HubSpot per request.</param> public BulkSyncResult BulkSync(BulkHubSpotContact[] hubSpotContacts, int batchSize = DefaultBatchSize) { var run = new BulkSyncResult(_clock.UtcNow) { TotalContacts = hubSpotContacts.Length, BatchCount = CalculateNumberOfBatches(hubSpotContacts, batchSize) }; try { for (int currentBatchNumber = 0; currentBatchNumber < run.BatchCount; currentBatchNumber++) { var contactBatch = hubSpotContacts.Skip(currentBatchNumber * batchSize).Take(batchSize).ToArray(); // extract the relevant group of contacts var response = _http.Post($"contacts/v1/contact/batch?hapikey={_hubSpotApiKey}", contactBatch); switch (response.StatusCode) { case HttpStatusCode.Accepted: // 202; deemed successful by HubSpot -- all in the batch were accepted run.SuccessCount += contactBatch.Length; _logger.LogInformation($"ACCEPTED: contact batch {currentBatchNumber + 1} of {run.BatchCount}"); break; default: // 400, 429, etc; something went awry and NONE of the contacts were accepted run.FailureCount += contactBatch.Length; run.FailedBatches.Add(new BulkSyncFailure { Count = contactBatch.Length, BatchNumber = currentBatchNumber + 1, HttpStatusCode = response.StatusCode, Exception = _http.GetResponseContent <HubSpotException>(response), HubSpotContacts = contactBatch }); // cast to print out the HTTP status code, just in case what's returned isn't // defined in the https://stackoverflow.com/a/22645395 _logger.LogWarning($@"REJECTED: contact batch {currentBatchNumber + 1} of {run.BatchCount} httpstatuscode: {(int) response.StatusCode} More details will be available in the serial processing logs."); break; } PumpTheBreaksEveryNRequestsToAvoid429Exceptions(currentBatchNumber + 1); } return(run); } finally { run.Execution.FinishUtc = _clock.UtcNow; } }