added ai-moderation image and text
This commit is contained in:
52
src/main/java/com/krylov/refound/ai/client/AiHttpClient.java
Normal file
52
src/main/java/com/krylov/refound/ai/client/AiHttpClient.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package com.krylov.refound.ai.client;
|
||||
|
||||
import com.krylov.refound.ai.exception.AiBadRequestException;
|
||||
import com.krylov.refound.ai.exception.AiServerException;
|
||||
import com.krylov.refound.ai.exception.AiUnavailableException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.reactive.function.client.WebClientRequestException;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class AiHttpClient {
|
||||
|
||||
private final WebClient aiWebClient;
|
||||
|
||||
public <T, R> R post(String uri, T request, Class<R> responseType) {
|
||||
log.info("AI POST {}", uri);
|
||||
try {
|
||||
R response = aiWebClient.post()
|
||||
.uri(uri)
|
||||
.bodyValue(request)
|
||||
.retrieve()
|
||||
.onStatus(
|
||||
HttpStatusCode::is4xxClientError,
|
||||
resp -> resp.bodyToMono(String.class)
|
||||
.map(AiBadRequestException::new)
|
||||
)
|
||||
.onStatus(
|
||||
HttpStatusCode::is5xxServerError,
|
||||
resp -> resp.bodyToMono(String.class)
|
||||
.map(AiServerException::new)
|
||||
)
|
||||
.bodyToMono(responseType)
|
||||
.block();
|
||||
|
||||
log.info("AI response received");
|
||||
return response;
|
||||
} catch (WebClientRequestException ex) {
|
||||
log.error("AI connection failed", ex);
|
||||
throw new AiUnavailableException(ex);
|
||||
|
||||
} catch (Exception ex) {
|
||||
log.error("AI request failed", ex);
|
||||
throw new AiUnavailableException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user