Skip to main content
Java SDK provides access to the CatchAll API from Java applications.

Installation

dependencies {
    implementation 'com.newscatcherapi:newscatcher-catchall-sdk:0.3.1'
}

Quickstart

Get started with CatchAll in three steps:
1

Initialize the client

import com.newscatcher.catchall.CatchAllApi;

CatchAllApi client = CatchAllApi.builder()
    .apiKey("YOUR_API_KEY")
    .build();
2

Create a job

import com.newscatcher.catchall.resources.jobs.requests.SubmitRequestDto;

var job = client.jobs().createJob(
    SubmitRequestDto.builder()
        .query("AI company acquisitions")
        .limit(10)
        .build()
);
String jobId = job.getJobId();
3

Wait and retrieve results

import com.newscatcher.catchall.types.StatusResponseDto;
import com.newscatcher.catchall.types.JobStatus;

final int POLL_INTERVAL_MS = 60000;

// Poll for completion
while (true) {
    StatusResponseDto status = client.jobs().getJobStatus(jobId);
    if (JobStatus.COMPLETED.equals(status.getStatus().orElse(null))) {
        break;
    }
    Thread.sleep(POLL_INTERVAL_MS);
}

// Get results
var results = client.jobs().getJobResults(jobId);
System.out.println("Found " + results.getValidRecords().orElse(0) + " valid records");
Jobs process asynchronously and typically complete in 10-15 minutes. See the Quickstart for a complete walkthrough.

Working with jobs

Preview suggested validators, enrichments, and date ranges before creating a job:
import com.newscatcher.catchall.CatchAllApiClient;
import com.newscatcher.catchall.resources.jobs.requests.InitializeRequestDto;
import com.newscatcher.catchall.types.InitializeResponseDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

CatchAllApiClient client = CatchAllApiClient.builder()
    .apiKey("YOUR_API_KEY")
    .build();

InitializeResponseDto suggestions = client.jobs().initialize(
    InitializeRequestDto.builder()
        .query("AI company acquisitions")
        .context("Focus on deal size and acquiring company details")
        .build()
);

ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
System.out.println(mapper.writeValueAsString(suggestions));
To learn more, see the Initialize endpoint.
import com.newscatcher.catchall.CatchAllApi;
import com.newscatcher.catchall.resources.jobs.requests.SubmitRequestDto;
import com.newscatcher.catchall.resources.jobs.requests.ContinueRequestDto;
import com.newscatcher.catchall.types.StatusResponseDto;
import com.newscatcher.catchall.types.PullJobResponseDto;
import com.newscatcher.catchall.types.JobStatus;
import com.newscatcher.catchall.core.NewscatcherApiApiException;

public class CompleteExample {
    public static void main(String[] args) {
        final int POLL_INTERVAL_MS = 60000;

        CatchAllApi client = CatchAllApi.builder()
            .apiKey("YOUR_API_KEY")
            .timeout(30)
            .maxRetries(3)
            .build();

        try {
            // Create job with custom enrichments
            var job = client.jobs().createJob(
                SubmitRequestDto.builder()
                    .query("AI company acquisitions")
                    .context("Focus on deal size and acquiring company details")
                    .limit(10)
                    .enrichments(
                        EnrichmentSchema.builder()
                            .name("acquirer_company")
                            .description("Extract the acquiring company name")
                            .type(EnrichmentType.COMPANY)
                            .build(),
                        EnrichmentSchema.builder()
                            .name("deal_value")
                            .description("Extract acquisition price if mentioned")
                            .type(EnrichmentType.NUMBER)
                            .build()
                    )
                    .build()
            );
            String jobId = job.getJobId();
            System.out.println("Job created: " + jobId);

            // Poll with early results access
            while (true) {
                StatusResponseDto status = client.jobs().getJobStatus(jobId);

                if (JobStatus.ENRICHING.equals(status.getStatus().orElse(null)) || 
                    JobStatus.COMPLETED.equals(status.getStatus().orElse(null))) {
                    
                    PullJobResponseDto results = client.jobs().getJobResults(jobId);
                    if (results.getValidRecords().orElse(0) > 0) {
                        System.out.println(String.format(
                            "Progress: %d valid records",
                            results.getValidRecords().orElse(0)
                        ));
                    }

                    if (JobStatus.COMPLETED.equals(status.getStatus().orElse(null))) {
                        break;
                    }
                }

                Thread.sleep(POLL_INTERVAL_MS);
            }

            // Continue if needed
            PullJobResponseDto results = client.jobs().getJobResults(jobId);
            if (results.getValidRecords().orElse(0) >= 10) {
                client.jobs().continueJob(
                    ContinueRequestDto.builder()
                        .jobId(jobId)
                        .newLimit(50)
                        .build()
                );

                while (true) {
                    StatusResponseDto status = client.jobs().getJobStatus(jobId);
                    if (JobStatus.COMPLETED.equals(status.getStatus().orElse(null))) {
                        break;
                    }
                    Thread.sleep(POLL_INTERVAL_MS);
                }

                results = client.jobs().getJobResults(jobId);
            }

            // Display results
            System.out.println("\nFinal: " + results.getValidRecords().orElse(0) + " valid records");
            results.getAllRecords().ifPresent(records ->
                records.forEach(record -> System.out.println("  " + record.getRecordTitle()))
            );

        } catch (NewscatcherApiApiException e) {
            System.err.println("Status: " + e.statusCode());
            System.err.println("Error: " + e.body());
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("Operation interrupted");
        }
    }
}

Working with monitors

Automate recurring queries with scheduled execution.
Create a monitor from a completed job:
import com.newscatcher.catchall.resources.monitors.requests.CreateMonitorRequestDto;
import com.newscatcher.catchall.types.CreateMonitorResponseDto;
import com.newscatcher.catchall.types.WebhookDto;
import com.newscatcher.catchall.types.WebhookDtoMethod;
import java.util.Map;

CreateMonitorResponseDto monitor = client.monitors().createMonitor(
    CreateMonitorRequestDto.builder()
        .referenceJobId(jobId)
        .schedule("every day at 12 PM UTC")
        .webhook(WebhookDto.builder()
            .url("https://your-endpoint.com/webhook")
            .method(WebhookDtoMethod.POST)
            .headers(Map.of("Authorization", "Bearer YOUR_TOKEN"))
            .build())
        .build()
);
System.out.println("Monitor created: " + monitor.getMonitorId().orElse("N/A"));
Monitors require a minimum 24-hour interval between executions. Learn more in the Monitors documentation.
import com.newscatcher.catchall.CatchAllApi;
import com.newscatcher.catchall.resources.monitors.requests.CreateMonitorRequestDto;
import com.newscatcher.catchall.resources.monitors.requests.UpdateMonitorRequestDto;
import com.newscatcher.catchall.resources.monitors.requests.ListMonitorJobsRequest;
import com.newscatcher.catchall.resources.monitors.types.ListMonitorJobsResponse;
import com.newscatcher.catchall.types.CreateMonitorResponseDto;
import com.newscatcher.catchall.types.UpdateMonitorResponseDto;
import com.newscatcher.catchall.types.ListMonitorsResponseDto;
import com.newscatcher.catchall.types.PullMonitorResponseDto;
import com.newscatcher.catchall.types.WebhookDto;
import com.newscatcher.catchall.types.WebhookDtoMethod;
import com.newscatcher.catchall.core.NewscatcherApiApiException;
import java.util.Map;

public class MonitorExample {
    public static void main(String[] args) {
        CatchAllApi client = CatchAllApi.builder()
            .apiKey("YOUR_API_KEY")
            .build();

        try {
            // Create monitor from completed job
            String jobId = "af7a26d6-cf0b-458c-a6ed-4b6318c74da3";

            CreateMonitorResponseDto monitor = client.monitors().createMonitor(
                CreateMonitorRequestDto.builder()
                    .referenceJobId(jobId)
                    .schedule("every day at 12 PM UTC")
                    .webhook(WebhookDto.builder()
                        .url("https://your-endpoint.com/webhook")
                        .method(WebhookDtoMethod.POST)
                        .headers(Map.of("Authorization", "Bearer YOUR_TOKEN"))
                        .build())
                    .build()
            );
            String monitorId = monitor.getMonitorId().orElseThrow();
            System.out.println("Monitor created: " + monitorId);

            // Update webhook
            client.monitors().updateMonitor(
                monitorId,
                UpdateMonitorRequestDto.builder()
                    .webhook(WebhookDto.builder()
                        .url("https://new-endpoint.com/webhook")
                        .method(WebhookDtoMethod.POST)
                        .build())
                    .build()
            );

            // List all monitors
            ListMonitorsResponseDto allMonitors = client.monitors().listMonitors();
            allMonitors.getMonitors().forEach(m -> {
                String status = m.getEnabled() ? "active" : "paused";
                System.out.println(String.format("%s: %s", m.getMonitorId(), status));
            });

            // Control execution
            client.monitors().disableMonitor(monitorId);
            client.monitors().enableMonitor(monitorId);

            // List execution history
            ListMonitorJobsResponse jobs = client.monitors().listMonitorJobs(
                monitorId,
                ListMonitorJobsRequest.builder().sort("desc").build()
            );
            System.out.println("\nMonitor executed " + jobs.getTotalJobs() + " jobs");
            jobs.getJobs().forEach(job -> {
                System.out.println(String.format(
                    "  Job %s: %s to %s",
                    job.getJobId(),
                    job.getStartDate(),
                    job.getEndDate()
                ));
            });

            // Get aggregated results
            PullMonitorResponseDto results = client.monitors().pullMonitorResults(monitorId);
            System.out.println("\nCollected " + results.getRecords().orElse(0) + " total records");
            results.getAllRecords().ifPresent(records ->
                records.forEach(record -> System.out.println("  " + record.getRecordTitle()))
            );

        } catch (NewscatcherApiApiException e) {
            System.err.println("Status: " + e.statusCode());
            System.err.println("Error: " + e.body());
        }
    }
}

Error handling

Handle API errors with structured exception handling:
import com.newscatcher.catchall.core.NewscatcherApiApiException;

try {
    client.jobs().createJob(
        SubmitRequestDto.builder()
            .query("AI company acquisitions")
            .build()
    );
} catch (NewscatcherApiApiException e) {
    System.err.println("Status: " + e.statusCode());
    System.err.println("Error: " + e.body());
}

Advanced usage

Pagination

Retrieve large result sets page by page:
import com.newscatcher.catchall.resources.jobs.requests.GetJobResultsRequest;

int page = 1;
while (true) {
    var results = client.jobs().getJobResults(
        jobId,
        GetJobResultsRequest.builder()
            .page(page)
            .pageSize(100)
            .build()
    );

    System.out.println("Page " + page + "/" + results.getTotalPages().orElse(1));

    results.getAllRecords().ifPresent(records ->
        records.forEach(record ->
            System.out.println("  " + record.getRecordTitle())
        )
    );

    if (results.getPage().orElse(0) >= results.getTotalPages().orElse(1)) {
        break;
    }
    page++;
}

Timeouts

Configure custom timeouts at client or request level:
CatchAllApi client = CatchAllApi.builder()
    .apiKey("YOUR_API_KEY")
    .timeout(30)
    .build();

Retries

Configure automatic retry behavior for failed requests:
CatchAllApi client = CatchAllApi.builder()
    .apiKey("YOUR_API_KEY")
    .maxRetries(3)
    .build();

Resources