Skip to content

Instantly share code, notes, and snippets.

@janedbal
Created November 6, 2026 14:28
Show Gist options
  • Select an option

  • Save janedbal/8c4dfc7d5e95ff5a76e568afdc0ff467 to your computer and use it in GitHub Desktop.

Select an option

Save janedbal/8c4dfc7d5e95ff5a76e568afdc0ff467 to your computer and use it in GitHub Desktop.
package com.example;
import com.fasterxml.jackson.databind.json.JsonMapper;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
public class CompanyFinder
{
private static final String ENDPOINT = "https://some.api";
public String getCompanyName(String identification) throws Exception
{
var httpClient = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(new URI(ENDPOINT + "/company-info.json?identification=" + identification))
.build();
var httpResponse = httpClient.send(request, BodyHandlers.ofString());
var body = new JsonMapper().readValue(httpResponse.body(), ApiResponse.class);
if (body.error() != null) {
return null;
}
return body.companyFound()
? body.companyData().companyName()
: null;
}
record ApiResponse(String error, boolean companyFound, CompanyData companyData)
{
record CompanyData(String companyName)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment