I want to do the following via Spring WebFlux and a given REST-API:
- Retrieve a list of file names (GET /files)
- Delete each retrieved file (DELETE /files/local/{file name} for each)
The problem is that I cannot combine both actions to "one" Mono instance. My current implementation is insufficient, because it blocks the Mono instances to execute the api calls immediately insteaf of performing them reactive.
My non reactive implementation:
public Mono cleanUpUploadedFiles() {
WebClient webClient = this.getWebClient();
// get all files / directories
Mono<FilesOverview> filesOverviewMono = this.getResource("/files", FilesOverview.class);
FilesOverview filesOverview = filesOverviewMono.block(); // TODO: prevent blocking mono
// delete file / directory one by one
for (FileOverview file : filesOverview.getFiles()) {
ClientResponse clientResponse;
clientResponse = webClient
.delete()
.uri(String.format("/files/local/%s", file.getName()))
.exchange()
.block(); // TODO: prevent blocking mono
if (clientResponse == null) {
return Mono.error(new MyException(String.format("could not execute rest call to delete uploaded files with uuid %s", file.getName())));
}
HttpStatus clientResponseStatusCode = clientResponse.statusCode();
if (clientResponseStatusCode.isError()) {
return Mono.error(new MyException(String.format("cannot delete uploaded files with uuid %s", file.getName())));
}
}
return Mono.empty(); // TODO: return Mono instance performing everything reactive without blocking
}
How to perform the consecutive web requests in one Mono instance reactive?