All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jdash.client.response.impl.GDSerializedSourceResponse Maven / Gradle / Ivy

The newest version!
package jdash.client.response.impl;

import jdash.client.exception.GDClientException;
import jdash.client.exception.GDResponseException;
import jdash.client.exception.ResponseDeserializationException;
import jdash.client.request.GDRequest;
import jdash.client.response.GDResponse;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;
import java.util.function.Function;

public final class GDSerializedSourceResponse implements GDResponse {

    private final GDRequest request;
    private final Mono serializedSource;
    private final Consumer onDeserialize;

    public GDSerializedSourceResponse(GDRequest request, Mono serializedSource,
                                      Consumer onDeserialize) {
        this.request = request;
        this.serializedSource = serializedSource;
        this.onDeserialize = onDeserialize;
    }

    @Override
    public  Mono deserialize(Function deserializer) {
        return serializedSource
                .handle((response, sink) -> {
                    try {
                        sink.next(deserializer.apply(response));
                    } catch (GDResponseException e) {
                        sink.error(e);
                    } catch (RuntimeException e) {
                        sink.error(new ResponseDeserializationException(response, e));
                    }
                })
                .onErrorMap(e -> new GDClientException(request, e))
                .doOnNext(onDeserialize);
    }
}