> ## Documentation Index
> Fetch the complete documentation index at: https://docs.shappire.tools/llms.txt
> Use this file to discover all available pages before exploring further.

# Exemplos em Java

> HttpClient, Spring Boot e tratamento de erros

## Cliente com HttpClient (Java 11+)

```java theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class ShappireClient {
  private static final String BASE = "https://api.shappire.tools/v1";
  private final String apiKey;
  private final HttpClient client = HttpClient.newHttpClient();
  private final ObjectMapper mapper = new ObjectMapper();

  public ShappireClient(String apiKey) {
    this.apiKey = apiKey;
  }

  public JsonNode resolve(String url) throws Exception {
    String body = mapper.writeValueAsString(Map.of("url", url));
    HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create(BASE + "/media/resolve"))
        .header("X-API-Key", apiKey)
        .header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString(body))
        .build();

    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    return mapper.readTree(response.body()).get("data");
  }

  public String enqueue(String mediaId, String format) throws Exception {
    String body = mapper.writeValueAsString(Map.of(
        "media_id", mediaId,
        "format", format
    ));
    HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create(BASE + "/media/download"))
        .header("X-API-Key", apiKey)
        .header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString(body))
        .build();

    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    return mapper.readTree(response.body()).get("data").get("job_id").asText();
  }
}
```

## Spring Boot

```java theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
@Service
public class MediaService {
  private final ShappireClient client;

  public MediaService(@Value("${shappire.api-key}") String apiKey) {
    this.client = new ShappireClient(apiKey);
  }

  public String downloadUrl(String sourceUrl) throws Exception {
    JsonNode media = client.resolve(sourceUrl);
    String jobId = client.enqueue(media.get("id").asText(), "video_best");
    // polling em thread separada ou @Async
    return jobId;
  }
}
```

## application.properties

```properties theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
shappire.api-key=${SHAPPIRE_API_KEY}
```
