web client throw error on failed

This commit is contained in:
Efstratios Giannopoulos 2024-03-28 11:43:12 +02:00
parent ccf3fa3111
commit 3e155de0d4
2 changed files with 23 additions and 10 deletions

View File

@ -9,6 +9,7 @@ import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
public class TransformerRepository implements FileTransformerClient {
@ -21,27 +22,32 @@ public class TransformerRepository implements FileTransformerClient {
@Override
public FileEnvelopeModel exportDmp(DmpModel dmpModel, String format) {
return transformerClient.post().uri("/export/dmp", uriBuilder -> uriBuilder.queryParam("format", format).build()).bodyValue(dmpModel).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON).exchangeToMono(mono -> mono.bodyToMono(FileEnvelopeModel.class)).block();
return transformerClient.post().uri("/export/dmp", uriBuilder -> uriBuilder.queryParam("format", format).build()).bodyValue(dmpModel)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(FileEnvelopeModel.class)).block();
}
@Override
public DmpModel importDmp(FileEnvelopeModel fileEnvelope) {
return transformerClient.post().uri("/import/dmp").bodyValue(fileEnvelope).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON).exchangeToMono(mono -> mono.bodyToMono(DmpModel.class)).block();
return transformerClient.post().uri("/import/dmp").bodyValue(fileEnvelope)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(DmpModel.class)).block();
}
@Override
public FileEnvelopeModel exportDescription(DescriptionModel descriptionModel, String format) {
return transformerClient.post().uri("/export/description", uriBuilder -> uriBuilder.queryParam("format", format).build()).bodyValue(descriptionModel).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON).exchangeToMono(mono -> mono.bodyToMono(FileEnvelopeModel.class)).block();
return transformerClient.post().uri("/export/description", uriBuilder -> uriBuilder.queryParam("format", format).build()).bodyValue(descriptionModel)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(FileEnvelopeModel.class)).block();
}
@Override
public DescriptionModel importDescription(FileEnvelopeModel fileEnvelope) {
return transformerClient.post().uri("/import/description").bodyValue(fileEnvelope).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON).exchangeToMono(mono -> mono.bodyToMono(DescriptionModel.class)).block();
return transformerClient.post().uri("/import/description").bodyValue(fileEnvelope)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(DescriptionModel.class)).block();
}
@Override
public FileTransformerConfiguration getConfiguration() {
return transformerClient.get().uri("/formats").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON).exchangeToMono(mono -> mono.bodyToMono(new ParameterizedTypeReference<FileTransformerConfiguration>() {})).block();
return transformerClient.get().uri("/formats")
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(new ParameterizedTypeReference<FileTransformerConfiguration>() {})).block();
}

View File

@ -1,14 +1,21 @@
package eu.eudat.service.deposit;
import eu.eudat.commonmodels.models.dmp.DmpModel;
import eu.eudat.depositinterface.repository.DepositClient;
import eu.eudat.depositinterface.repository.DepositConfiguration;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import eu.eudat.commonmodels.models.dmp.DmpModel;
import reactor.core.publisher.Mono;
public class DepositClientImpl implements DepositClient {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DepositClientImpl.class));
private final WebClient depositClient;
@ -19,21 +26,21 @@ public class DepositClientImpl implements DepositClient {
@Override
public String deposit(DmpModel dmpDepositModel, String repositoryAccessToken) throws Exception {
return depositClient.post().uri("", uriBuilder -> uriBuilder.queryParam("authToken", repositoryAccessToken).build()).bodyValue(dmpDepositModel).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON).exchangeToMono(mono -> mono.bodyToMono(String.class)).block();
return depositClient.post().uri("", uriBuilder -> uriBuilder.queryParam("authToken", repositoryAccessToken).build()).bodyValue(dmpDepositModel).exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(String.class)).block();
}
@Override
public String authenticate(String code) {
return depositClient.get().uri("/authenticate/", uriBuilder -> uriBuilder.queryParam("authToken", code).build()).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON).exchangeToMono(mono -> mono.bodyToMono(String.class)).block();
return depositClient.get().uri("/authenticate/", uriBuilder -> uriBuilder.queryParam("authToken", code).build()).exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(String.class)).block();
}
@Override
public DepositConfiguration getConfiguration() {
return depositClient.get().uri("/configuration").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON).exchangeToMono(mono -> mono.bodyToMono(new ParameterizedTypeReference<DepositConfiguration>() {})).block();
return depositClient.get().uri("/configuration").exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(new ParameterizedTypeReference<DepositConfiguration>() {})).block();
}
@Override
public String getLogo() {
return depositClient.get().uri("/logo").exchangeToMono(mono -> mono.bodyToMono(String.class)).block();
return depositClient.get().uri("/logo").exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(String.class)).block();
}
}