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

io.github.honhimw.ms.internal.reactive.AbstractReactiveImpl Maven / Gradle / Ivy

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.honhimw.ms.internal.reactive;

import io.github.honhimw.ms.http.HttpFailureException;
import io.github.honhimw.ms.http.ReactiveHttpUtils;
import io.github.honhimw.ms.json.JsonHandler;
import io.github.honhimw.ms.json.TypeRef;
import io.netty.handler.codec.http.HttpResponseStatus;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * @author hon_him
 * @since 2024-01-03
 */

abstract class AbstractReactiveImpl {

    protected final ReactiveMSearchClientImpl _client;

    protected final JsonHandler jsonHandler;

    protected AbstractReactiveImpl(ReactiveMSearchClientImpl client) {
        this._client = client;
        this.jsonHandler = _client.jsonHandler;
    }

    protected ReactiveHttpUtils getHttpClient() {
        return _client.httpClient;
    }

    protected String fulfillUri(String path) {
        return _client.serverUrl + path;
    }

    protected  Mono get(String path, TypeRef typeRef) {
        return get(path, configurer -> {
        }, typeRef);
    }

    protected  Mono post(String path, TypeRef typeRef) {
        return post(path, configurer -> {
        }, typeRef);
    }

    protected  Mono put(String path, TypeRef typeRef) {
        return put(path, configurer -> {
        }, typeRef);
    }

    protected  Mono patch(String path, TypeRef typeRef) {
        return patch(path, configurer -> {
        }, typeRef);
    }

    protected  Mono delete(String path, TypeRef typeRef) {
        return delete(path, configurer -> {
        }, typeRef);
    }

    protected  Mono get(String path, Consumer configurer, TypeRef typeRef) {
        return request("GET", path, configurer, typeRef);
    }

    protected  Mono post(String path, Consumer configurer, TypeRef typeRef) {
        return request("POST", path, configurer, typeRef);
    }

    protected  Mono put(String path, Consumer configurer, TypeRef typeRef) {
        return request("PUT", path, configurer, typeRef);
    }

    protected  Mono patch(String path, Consumer configurer, TypeRef typeRef) {
        return request("PATCH", path, configurer, typeRef);
    }

    protected  Mono delete(String path, Consumer configurer, TypeRef typeRef) {
        return request("DELETE", path, configurer, typeRef);
    }

    protected  Mono request(String method, String path, Consumer configurer, TypeRef typeRef) {
        if (Objects.nonNull(_client.apiKey)) {
            Consumer _apiKey_configurer = configurer1 -> configurer1
                .header("Authorization", String.format("Bearer %s", _client.apiKey));
            configurer = _apiKey_configurer.andThen(configurer);
        }
        ReactiveHttpUtils.ReactiveHttpResult receiver = getHttpClient().receiver(method, fulfillUri(path), configurer);
        return extract(receiver, typeRef);
    }

    protected  Mono extract(ReactiveHttpUtils.ReactiveHttpResult receiver, TypeRef typeRef) {
        return receiver.responseSingle((httpClientResponse, byteBufMono) -> {
                HttpResponseStatus status = httpClientResponse.status();
                int code = status.code();
                Mono stringMono = byteBufMono.asByteArray()
                    .map(bytes -> new String(bytes, StandardCharsets.UTF_8));
                if (code < 200 || 300 <= code) {
                    return stringMono
                        .switchIfEmpty(Mono.just(status.reasonPhrase()))
                        .handle((s, sink) -> {
                            HttpFailureException httpFailureException = new HttpFailureException(code, s);
                            httpFailureException.setMethod(httpClientResponse.method().name());
                            httpFailureException.setUri(httpClientResponse.resourceUrl());
                            sink.error(httpFailureException);
                        });
                } else {
                    return stringMono
                        .mapNotNull(s -> jsonHandler.fromJson(s, typeRef));
                }
            })
            .onErrorResume(throwable -> {
                if (throwable instanceof HttpFailureException) {
                    HttpFailureException httpFailureException = (HttpFailureException) throwable;
                    return httpFailureException.getStatusCode() == 404;
                } else {
                    return false;
                }
            }, throwable -> Mono.empty());
    }

    protected void json(ReactiveHttpUtils.Configurer configurer, Object object) {
        configurer.body(payload -> payload.raw(raw -> raw.json(jsonHandler.toJson(object))));
    }

    protected void json(ReactiveHttpUtils.Configurer configurer, String json) {
        configurer.body(payload -> payload.raw(raw -> raw.json(json)));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy