waitForJobCompletion method
Waits for a bulk validation job to complete and returns the results.
This is a convenience method that polls the job status at regular intervals until the job completes or fails. It automatically handles the polling logic and returns the results when ready.
jobId The ID of the job to wait for.
pollInterval How often to check the job status (default: 5 seconds).
timeout Maximum time to wait before giving up (default: 5 minutes).
Returns a BulkValidationResult when the job completes successfully.
Throws an ApiException if:
- The job fails
- The timeout is exceeded
- The API key is invalid (401)
- Network or server errors occur
Example:
try {
final results = await client.waitForJobCompletion(
job.id,
pollInterval: Duration(seconds: 3),
timeout: Duration(minutes: 10),
);
print('Job completed: ${results.summary.valid} valid emails');
} on ApiException catch (e) {
print('Job failed or timed out: ${e.message}');
}
Implementation
Future<BulkValidationResult> waitForJobCompletion(
String jobId, {
Duration pollInterval = const Duration(seconds: 5),
Duration timeout = const Duration(minutes: 5),
}) async {
final startTime = DateTime.now();
while (DateTime.now().difference(startTime) < timeout) {
final jobStatus = await getJobStatus(jobId);
if (jobStatus.isCompleted) {
return await getJobResults(jobId);
} else if (jobStatus.isFailed) {
throw ApiException(
message:
'Job $jobId failed${jobStatus.errorMessage != null ? ': ${jobStatus.errorMessage}' : ''}',
statusCode: 500,
);
}
await Future.delayed(pollInterval);
}
throw ApiException(
message:
'Job $jobId did not complete within ${timeout.inSeconds} seconds',
statusCode: 408,
);
}