package com.v2d.document.service; import com.v2d.document.config.AppProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; @Service public class ExternalApiService { private final Logger log = LoggerFactory.getLogger(ExternalApiService.class); private final AppProperties props; private final HttpClient http = HttpClient.newHttpClient(); public ExternalApiService(AppProperties props) { this.props = props; } public String callProvider(String jsonPayload) { String key = props.getApiKey(); if (key == null || key.isBlank()) { log.warn("External API key is not configured."); throw new IllegalStateException("External API key missing"); } try { HttpRequest req = HttpRequest.newBuilder() .uri(URI.create("https://api.example.com/endpoint")) // replace with real endpoint .header("Authorization", "Bearer " + key) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(jsonPayload)) .build(); HttpResponse resp = http.send(req, HttpResponse.BodyHandlers.ofString()); return resp.body(); } catch (Exception e) { log.error("External API call failed: {}", e.getMessage()); throw new RuntimeException("External API call failed", e); } } }