mirror of
https://gitlab.rlp.net/proj-wise2526-video2document/video2document.git
synced 2026-06-15 18:01:52 +02:00
55555bcc37
- /src1/main/resources/application.yml - /src1/main/java/com/v2d/document/config/AppProperties.java - /src1/main/java/com/v2d/document/service/ExternalApiService.java - /src1/main/java/com/v2d/document/controller/GenerateController.java - /src1/test/AppPropertiesTest.java - /README.md
46 lines
1.6 KiB
Java
46 lines
1.6 KiB
Java
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<String> 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);
|
|
}
|
|
}
|
|
}
|