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

com.gs.api.accelrx.web.client.response.AccelRxWebClientObservableResponse Maven / Gradle / Ivy

The newest version!
package com.gs.api.accelrx.web.client.response;

import com.gs.api.accelrx.AccelRxWebClientRequest;
import com.gs.api.accelrx.AccelRxWebClientStreamResponse;
import com.gs.api.accelrx.web.api.WebClientObservableResponse;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Single;
import io.vertx.rxjava3.core.MultiMap;
import io.vertx.rxjava3.core.buffer.Buffer;
import io.vertx.rxjava3.core.http.HttpClientResponse;

import java.lang.reflect.Type;

public class AccelRxWebClientObservableResponse extends AccelRxWebClientAbstractResponse implements WebClientObservableResponse, AccelRxWebClientStreamResponse {
    private final Observable observableBody;

    private AccelRxWebClientObservableResponse(AccelRxWebClientRequest clientRequest,
                                               boolean success,
                                               int statusCode,
                                               String message,
                                               MultiMap headers,
                                               Observable observableBody,
                                               Buffer errorBody) {
        super(clientRequest, success, statusCode, message, headers, errorBody);
        this.observableBody = observableBody;
    }

    public static AccelRxWebClientObservableResponse create(AccelRxWebClientRequest clientRequest, HttpClientResponse response) {
        if (response.statusCode() >= 200 && response.statusCode() <= 204) {
            Observable bodyBytes = response.pause()
                    .toObservable()
                    .map(buffer -> buffer.getDelegate().getBytes())
                    .doOnSubscribe(subscription -> response.resume());
            return AccelRxWebClientObservableResponse.success(clientRequest, response.statusCode(), response.headers(), bodyBytes);
        }

        return AccelRxWebClientObservableResponse.failure(clientRequest, response.statusCode(), response.statusMessage(), null, response.headers());
    }

    static AccelRxWebClientObservableResponse success(AccelRxWebClientRequest clientRequest, int statusCode, MultiMap headers, Observable observableBody) {
        return new AccelRxWebClientObservableResponse(clientRequest, true, statusCode, null, headers, observableBody, null);
    }

    static AccelRxWebClientObservableResponse failure(AccelRxWebClientRequest clientRequest, int statusCode, String message, Buffer errorBody, MultiMap headers) {
        return new AccelRxWebClientObservableResponse(clientRequest, false, statusCode, message, headers, null, errorBody);
    }

    @Override
    public  Single body(Type responseType) {
        return null;
    }

    @Override
    public  Single body(Class bodyClass) {
        return null;
    }

    @Override
    public Single> bodyStream() {
        return Single.just(observableBody);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy