0

I have the below method

public Maybe<HttpResponse<?>> post(Publisher<CompletedFileUpload> files) {
        MultipartBody.Builder requestBody = MultipartBody.builder();
        return Flowable.fromPublisher(files).flatMap(file -> {
            requestBody
                    .addPart("file", file.getFilename(), MediaType.TEXT_PLAIN_TYPE, file.getBytes())
                    .addPart("id", "asdasdsds");
            return this.iProductClient.post(requestBody.build());
        });
    }

The return type from this.iProductClient.post(requestBody.build()); is Maybe<HttpResponse<?>>

How can I convert the below code to return Maybe<HttpResponse<?>>, currently the below method has error

return Flowable.fromPublisher(files).flatMap(file -> {
            requestBody
                    .addPart("file", file.getFilename(), MediaType.TEXT_PLAIN_TYPE, file.getBytes())
                    .addPart("id", "asdasdsds");
            return this.iProductClient.post(requestBody.build());
        });
San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • You have presumably multiple files and thus multiple `Maybe`s. How should they be turned into one `Maybe` from a logic stance? – akarnokd Mar 08 '21 at 08:20
  • @akarnokd basically what I want is to loop all the files and add to the MultipartBody, and call this.iProductClient.post(requestBody.build()). However the return type of post method is Maybe>. Thus I need to return Maybe – San Jaisy Mar 08 '21 at 08:26

1 Answers1

1

You can use collect and then flatmap in the requrest sending:

return Flowable.fromPublisher(files)
    .collect(MultipartBody::builder, (requestBody, file) -> {
        requestBody
            .addPart("file", file.getFilename(), MediaType.TEXT_PLAIN_TYPE, file.getBytes())
            .addPart("id", "asdasdsds");
    })
    .flatMapMaybe(requestBody -> iProductClient.post(requestBody.build()))
    ;
akarnokd
  • 69,132
  • 14
  • 157
  • 192