Java model objects for Brave Search API.
Find a file
2026-05-03 20:18:49 -07:00
codebuild build fixes 2025-09-09 17:30:53 -07:00
gradle/wrapper build fixes 2025-09-09 17:30:53 -07:00
src/main/java/com/agonyforge/brave4j initial commit 2025-09-09 00:06:51 -07:00
.gitignore build fixes 2025-09-09 17:30:53 -07:00
build.gradle migrate to forgejo 2026-05-03 20:18:49 -07:00
gradlew initial commit 2025-09-09 00:06:51 -07:00
gradlew.bat initial commit 2025-09-09 00:06:51 -07:00
LICENSE Initial commit 2025-09-08 23:48:57 -07:00
README.md migrate to forgejo 2026-05-03 20:18:49 -07:00
settings.gradle initial commit 2025-09-09 00:06:51 -07:00

brave4j

Model objects for Brave Search API

These are just model objects. It's not a full framework for making queries. To use them you will need to write some code.

Add Brave4j to your project

In build.gradle you need to give it the Forgejo package repository:

repositories {
	mavenCentral()
	maven {
		url = uri("https://git.keeler.world/api/packages/scion/maven")
	}
}

Then you can add the dependency as normal into your dependencies section:

implementation('com.agonyforge:brave4j:0.0.1-SNAPSHOT')

Compatibility with RestTemplate

To make this work with Spring's RestTemplate I had to make it use the Apache HTTP client so that it could successfully convert the responses into objects.

In build.gradle:

implementation('org.apache.httpcomponents.client5:httpclient5')

In a @Configuration annotated class:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    ConnectionConfig connectionConfig = ConnectionConfig
            .custom()
            .setConnectTimeout(30, TimeUnit.SECONDS)
            .setSocketTimeout(30, TimeUnit.SECONDS)
            .build();

    @SuppressWarnings("resource")
    BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();

    connectionManager.setConnectionConfig(connectionConfig);

    return builder
            .requestFactory(() -> new HttpComponentsClientHttpRequestFactory(HttpClientBuilder
                    .create()
                    .setConnectionManager(connectionManager)
                    .build()))
            .build();
}

Write some code

Finally, here's how to call the Brave Search API with your new and improved RestTemplate:

// takes a search query, sends it to Brave and returns the URLs from the search results
public List<String> getBraveSearchUrls(String query) {
        LOGGER.info("Web search query: {}", query);
        String urlTemplate = UriComponentsBuilder
                .fromUriString("https://api.search.brave.com/res/v1/web/search")
                .queryParam("q", "{keywords}")
                .encode()
                .toUriString();

        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.add("Accept-Encoding", "gzip");
        headers.add("X-Subscription-Token", YOUR_API_KEY_HERE);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> request = new HttpEntity<>("parameters", headers);

        try {
            ResponseEntity<Response> responseEntity = restTemplate.exchange(
                    urlTemplate,
                    HttpMethod.GET,
                    request,
                    Response.class,
                    Map.of("keywords", query));

            Response response = responseEntity.getBody();

            if (response == null || response.getWeb() == null || response.getWeb().getResults() == null) {
                LOGGER.info("Search returned no results");
                return List.of();
            }

            List<String> urls = Arrays.stream(response.getWeb().getResults())
                    .map(Result::getUrl)
                    .toList();

            LOGGER.info("Found {} results: {}", urls.size(), urls);

            return new ArrayList<>(urls);
        } catch (Exception e) {
            LOGGER.error("Error searching for results: {}", e.getMessage());
            return List.of();
        }
    }