Skip to content

About

Newscatcher Java SDK

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Newscatcher Java Library

fern shield Maven Central

The Newscatcher Java library provides convenient access to the Newscatcher APIs from Java.

Table of Contents

Documentation

API reference documentation is available here.

Requirements

This library requires Java 8 or above.

Installation

Gradle

Add the dependency in your build.gradle file:

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

Maven

Add the dependency in your pom.xml file:

<dependency>
  <groupId>com.newscatcherapi</groupId>
  <artifactId>newscatcher-sdk</artifactId>
  <version>3.0.0</version>
</dependency>

Reference

A full reference for this library is available here.

Usage

Instantiate and use the client with the following:

package com.example.usage;

import com.newscatcher.api.NewscatcherApiClient;
import com.newscatcher.api.resources.search.requests.PostSearchRequest;

public class Example {
    public static void main(String[] args) {
        NewscatcherApiClient client = NewscatcherApiClient
            .builder()
            .apiKey("<value>")
            .build();

        client.search().post(
            PostSearchRequest
                .builder()
                .q("\"supply chain\" AND Amazon NOT China")
                .pageSize(1)
                .build()
        );
    }
}

Error Handling

When the API responds with a non-success status code, the SDK throws an exception.

import com.newscatcher.api.core.NewscatcherApiApiException;

try {
    client.search().post(...);
} catch (NewscatcherApiApiException e) {
    System.out.println(e.getMessage());
    System.out.println(e.statusCode());
    System.out.println(e.body());
}

The SDK provides specific error types for different cases:

import com.newscatcher.api.errors.BadRequestError;
import com.newscatcher.api.errors.UnauthorizedError;
import com.newscatcher.api.errors.ForbiddenError;
import com.newscatcher.api.errors.UnprocessableEntityError;
import com.newscatcher.api.errors.RequestTimeoutError;
import com.newscatcher.api.errors.TooManyRequestsError;
import com.newscatcher.api.errors.InternalServerError;

try {
    client.search().post(...);
} catch (BadRequestError e) {
    // Handle 400 error - Bad request
    Error errorBody = e.body();
} catch (UnauthorizedError e) {
    // Handle 401 error - Authentication failed
} catch (ForbiddenError e) {
    // Handle 403 error - Server refuses action
} catch (UnprocessableEntityError e) {
    // Handle 422 error - Validation error
} catch (RequestTimeoutError e) {
    // Handle 408 error - Request timeout
} catch (TooManyRequestsError e) {
    // Handle 429 error - Rate limit exceeded
} catch (InternalServerError e) {
    // Handle 500 error - Internal server error
}

Advanced Features

Retries

The SDK automatically retries failed requests with exponential backoff. When a request fails with any of these status codes, the SDK retries up to 2 times:

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

Client Configuration

Configure the client using the builder pattern:

NewscatcherApiClient client = NewscatcherApiClient.builder()
 .apiKey("YOUR_API_KEY")          // Required
 .timeout(60)                      // Optional: timeout in seconds
 .url("https://custom-url.com")    // Optional: override API URL
 .environment(Environment.DEFAULT) // Optional: use predefined environment
 .build();

Available configurations include:

  • apiKey(String): Sets authentication key (required).
  • timeout(int): Sets request timeout in seconds.
  • url(String): Overrides API base URL with a custom URL
  • environment(Environment): Uses a predefined environment configuration.

Note: You must provide an API key when building the client. Otherwise, a RuntimeException will be thrown.

Example Requests

Latest Headlines

import com.newscatcher.api.resources.latestheadlines.requests.LatestHeadlinesPostRequest;

client.latestHeadlines().post(LatestHeadlinesPostRequest.builder()
    .when("7d")
    .lang(Arrays.asList("en"))
    .build());

Search by Author

import com.newscatcher.api.resources.authors.requests.AuthorsPostRequest;

client.authors().post(AuthorsPostRequest.builder()
    .authorName("Jane Smith")
    .build());

Environments

This SDK allows you to configure different environments for API requests.

import com.newscatcher.api.NewscatcherApiClient;
import com.newscatcher.api.core.Environment;

NewscatcherApiClient client = NewscatcherApiClient
    .builder()
    .environment(Environment.Default)
    .build();

Base Url

You can set a custom base URL when constructing the client.

import com.newscatcher.api.NewscatcherApiClient;

NewscatcherApiClient client = NewscatcherApiClient
    .builder()
    .url("https://example.com")
    .build();

Exception Handling

When the API returns a non-success status code (4xx or 5xx response), an API exception will be thrown.

import com.newscatcher.api.core.NewscatcherApiApiException;

try{
    client.search().post(...);
} catch (NewscatcherApiApiException e){
    // Do something with the API exception...
}

Advanced

Custom Client

This SDK is built to work with any instance of OkHttpClient. By default, if no client is provided, the SDK will construct one. However, you can pass your own client like so:

import com.newscatcher.api.NewscatcherApiClient;
import okhttp3.OkHttpClient;

OkHttpClient customClient = ...;

NewscatcherApiClient client = NewscatcherApiClient
    .builder()
    .httpClient(customClient)
    .build();

Retries

The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2). Before defaulting to exponential backoff, the SDK will first attempt to respect the Retry-After header (as either in seconds or as an HTTP date), and then the X-RateLimit-Reset header (as a Unix timestamp in epoch seconds); failing both of those, it will fall back to exponential backoff.

Which status codes are retried depends on the retry-status-codes generator configuration:

legacy (current default): retries on

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 5XX (All server errors, including 500)

recommended: retries on

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 502 (Bad Gateway)
  • 503 (Service Unavailable)
  • 504 (Gateway Timeout)

Use the maxRetries client option to configure this behavior.

import com.newscatcher.api.NewscatcherApiClient;

NewscatcherApiClient client = NewscatcherApiClient
    .builder()
    .maxRetries(1)
    .build();

Timeouts

The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level.

import com.newscatcher.api.NewscatcherApiClient;
import com.newscatcher.api.core.RequestOptions;

// Client level
NewscatcherApiClient client = NewscatcherApiClient
    .builder()
    .timeout(60)
    .build();

// Request level
client.search().post(
    ...,
    RequestOptions
        .builder()
        .timeout(60)
        .build()
);

Custom Headers

The SDK allows you to add custom headers to requests. You can configure headers at the client level or at the request level.

import com.newscatcher.api.NewscatcherApiClient;
import com.newscatcher.api.core.RequestOptions;

// Client level
NewscatcherApiClient client = NewscatcherApiClient
    .builder()
    .addHeader("X-Custom-Header", "custom-value")
    .addHeader("X-Request-Id", "abc-123")
    .build();
;

// Request level
client.search().post(
    ...,
    RequestOptions
        .builder()
        .addHeader("X-Request-Header", "request-value")
        .build()
);

Access Raw Response Data

The SDK provides access to raw response data, including headers, through the withRawResponse() method. The withRawResponse() method returns a raw client that wraps all responses with body() and headers() methods. (A normal client's response is identical to a raw client's response.body().)

NewscatcherApiHttpResponse response = client.search().withRawResponse().post(...);

System.out.println(response.body());
System.out.println(response.headers().get("X-My-Header"));

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

Release Notes

For release notes and changelog, visit our GitHub releases page.

About

Newscatcher Java SDK

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages