#14 -- Add webclient

This commit is contained in:
Anton Romanov 2025-03-19 17:53:03 +04:00
parent 34db9f6018
commit dd73517e93
3 changed files with 92 additions and 0 deletions

View File

@ -30,6 +30,8 @@ dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jetty'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
// web client
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-webflux'
implementation group: 'org.slf4j', name: 'slf4j-api'
implementation group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect'
implementation group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity6'

View File

@ -0,0 +1,84 @@
package ru.ulstu.http;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.util.Base64;
import java.util.concurrent.CompletableFuture;
import static java.nio.charset.StandardCharsets.UTF_8;
public class HttpService {
private final Logger log = LoggerFactory.getLogger(HttpService.class);
private final WebClient client = getWebClient();
private final static String USER_NAME = "admin";
private final static String PASSWORD = "admin";
//private final MultiValueMap<String, String> myCookies = new LinkedMultiValueMap<String, String>();
public WebClient getWebClient() {
final int size = 16 * 1024 * 1024 * 10;
final ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
.build();
return WebClient.builder()
.exchangeStrategies(strategies)
.build();
}
// private void auth() {
// try {
// client
// .get()
// .uri(AUTH_URL)
// .headers(headers -> headers.setBasicAuth(USER_NAME, PASSWORD))
// .header("IBSession", "start")
// .exchange()
// .subscribe(r ->
// {
// System.out.println("get cookies");
// for (String key : r.cookies().keySet()) {
// myCookies.put(key, List.of(r.cookies().get(key).get(0).getValue()));
// }
// }
// );
// System.out.println("start waiting");
// Thread.sleep(1000);
// System.out.println("stop waiting");
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// public String getWithAuth(int yearStart, int yearEnd) {
// auth();
// return get(yearStart, yearEnd);
// }
public String post(String url, Object requestBody) {
String response = null;
try {
System.out.println("set cookies");
CompletableFuture<String> cf = client
.post()
.uri(String.format(url))
.header("Authorization", "Basic " + Base64.getEncoder()
.encodeToString((USER_NAME + ":" + PASSWORD).getBytes(UTF_8)))
.header("IBSession", "finish")
.contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(requestBody), String.class)
//.cookies(cookies -> cookies.addAll(myCookies))
.retrieve()
.bodyToMono(String.class)
.toFuture();
cf.get();
response = cf.get();
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
}

View File

@ -5,6 +5,7 @@ import ru.ulstu.datamodel.Model;
import ru.ulstu.datamodel.exception.ModelingException;
import ru.ulstu.datamodel.ts.TimeSeries;
import ru.ulstu.datamodel.ts.TimeSeriesValue;
import ru.ulstu.http.HttpService;
import ru.ulstu.method.Method;
import ru.ulstu.method.MethodParamValue;
import ru.ulstu.method.MethodParameter;
@ -12,9 +13,12 @@ import ru.ulstu.method.MethodParameter;
import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class PlainFuzzy extends Method {
private final HttpService httpService = new HttpService();
@Override
public List<MethodParameter> getAvailableParameters() {
return PlainFuzzyModel.getAvailableParameters();
@ -68,6 +72,8 @@ public class PlainFuzzy extends Method {
@Override
protected TimeSeries getForecastWithValidParams(Model model, TimeSeries forecast) throws ModelingException {
String fuzzyTimeSeries = ((PlainFuzzyModel) model).getFuzzyTimeSeries().stream().map(Triangle::getLabel).collect(Collectors.joining(","));
String result = httpService.post("http://plans.athene.tech/fuzzyRuleRest/generate-rules/3152", fuzzyTimeSeries);
for (TimeSeriesValue tsVal : forecast.getValues()) {
tsVal.setValue(0.0);
}