|
@@ -5,8 +5,12 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.storlead.framework.common.result.Result;
|
|
import com.storlead.framework.common.result.Result;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import java.io.InputStreamReader;
|
|
|
import java.net.URI;
|
|
import java.net.URI;
|
|
|
import java.net.http.*;
|
|
import java.net.http.*;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
@@ -23,6 +27,7 @@ public class HttpService {
|
|
|
private final HttpClient httpClient;
|
|
private final HttpClient httpClient;
|
|
|
private final ObjectMapper objectMapper = JacksonHolder.OBJECT_MAPPER;
|
|
private final ObjectMapper objectMapper = JacksonHolder.OBJECT_MAPPER;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
public HttpService() {
|
|
public HttpService() {
|
|
|
this.httpClient = HttpClient.newBuilder()
|
|
this.httpClient = HttpClient.newBuilder()
|
|
|
.connectTimeout(Duration.ofSeconds(5))
|
|
.connectTimeout(Duration.ofSeconds(5))
|
|
@@ -124,6 +129,50 @@ public class HttpService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public void postStream(
|
|
|
|
|
+ String url,
|
|
|
|
|
+ String authorization,
|
|
|
|
|
+ String body,
|
|
|
|
|
+ SseEmitter emitter
|
|
|
|
|
+ ) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
|
|
+ .uri(URI.create(url))
|
|
|
|
|
+ .timeout(Duration.ofSeconds(60))
|
|
|
|
|
+ .header("Authorization", authorization)
|
|
|
|
|
+ .header("Accept", "text/event-stream")
|
|
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
|
|
+ .POST(HttpRequest.BodyPublishers.ofString(body))
|
|
|
|
|
+ .build();
|
|
|
|
|
+
|
|
|
|
|
+ HttpResponse<InputStream> response =
|
|
|
|
|
+ httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
|
|
|
|
+
|
|
|
|
|
+ try (BufferedReader reader = new BufferedReader(
|
|
|
|
|
+ new InputStreamReader(response.body(), StandardCharsets.UTF_8))) {
|
|
|
|
|
+
|
|
|
|
|
+ String line;
|
|
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
|
|
+ if (line.startsWith("data:")) {
|
|
|
|
|
+ String data = line.substring(5);
|
|
|
|
|
+
|
|
|
|
|
+ if (data.contains("message_end")) {
|
|
|
|
|
+ emitter.complete();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ emitter.send(data);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ emitter.complete();
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ emitter.completeWithError(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/* ================= PUT ================= */
|
|
/* ================= PUT ================= */
|
|
|
|
|
|
|
|
public <T> Result<T> put(
|
|
public <T> Result<T> put(
|