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

com.arakelian.elastic.OkHttpElasticClient Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 com.arakelian.elastic;

import java.util.concurrent.Callable;

import com.arakelian.elastic.model.About;
import com.arakelian.elastic.model.BulkResponse;
import com.arakelian.elastic.model.ClusterHealth;
import com.arakelian.elastic.model.ClusterHealth.Status;
import com.arakelian.elastic.model.DeletedDocument;
import com.arakelian.elastic.model.Document;
import com.arakelian.elastic.model.Documents;
import com.arakelian.elastic.model.Index;
import com.arakelian.elastic.model.IndexCreated;
import com.arakelian.elastic.model.IndexDeleted;
import com.arakelian.elastic.model.IndexedDocument;
import com.arakelian.elastic.model.Mget;
import com.arakelian.elastic.model.Refresh;
import com.arakelian.elastic.model.VersionComponents;
import com.google.common.base.Preconditions;

import retrofit2.Call;
import retrofit2.Response;

public class OkHttpElasticClient implements ElasticClient {
    protected final VersionComponents version;
    protected final OkHttpElasticApi api;

    public OkHttpElasticClient(final OkHttpElasticApi api, final VersionComponents version) {
        this.api = Preconditions.checkNotNull(api);
        this.version = version;
    }

    @Override
    public Response about() throws ElasticException {
        return executeWithRetry(() -> {
            return api.about();
        });
    }

    @Override
    public Response bulk(final String operations, final Boolean pretty)
            throws ElasticException {
        return executeWithRetry(() -> {
            return api.bulk(operations, pretty);
        });
    }

    @Override
    public Response clusterHealth() throws ElasticException {
        return executeWithRetry(() -> {
            return api.clusterHealth();
        });
    }

    @Override
    public Response clusterHealth(final Status waitForStatus, final String timeout)
            throws ElasticException {
        return executeWithRetry(() -> {
            return api.clusterHealth(waitForStatus, timeout);
        });
    }

    @Override
    public Response clusterHealthForIndex(
            final String names,
            final Status waitForStatus,
            final String timeout) throws ElasticException {
        return executeWithRetry(() -> {
            return api.clusterHealthForIndex(names, waitForStatus, timeout);
        });
    }

    @Override
    public Response createIndex(final String name, final Index index) throws ElasticException {
        return executeWithRetry(() -> {
            return api.createIndex(name, index);
        });
    }

    @Override
    public Response deleteAllIndexes() throws ElasticException {
        return executeWithRetry(() -> {
            return api.deleteAllIndexes();
        });
    }

    @Override
    public Response deleteDocument(final String name, final String type, final String id)
            throws ElasticException {
        return executeWithRetry(() -> {
            return api.deleteDocument(name, type, id);
        });
    }

    @Override
    public Response deleteDocument(
            final String name,
            final String type,
            final String id,
            final long epochMillisUtc) throws ElasticException {
        return executeWithRetry(() -> {
            return api.deleteDocument(name, type, id, epochMillisUtc);
        });
    }

    @Override
    public Response deleteIndex(final String names) throws ElasticException {
        return executeWithRetry(() -> {
            return api.deleteIndex(names);
        });
    }

    protected  Response executeWithRetry(final Callable> callable) throws ElasticException {
        try {
            final Call call = callable.call();
            final Response response = call.execute();
            return response;
        } catch (final Exception e) {
            throw new ElasticException(e.getMessage(), e);
        }
    }

    @Override
    public Response getDocument(
            final String name,
            final String type,
            final String id,
            final String sourceFields) throws ElasticException {
        return executeWithRetry(() -> {
            return api.getDocument(name, type, id, sourceFields);
        });
    }

    @Override
    public Response getDocuments(final Mget mget) throws ElasticException {
        return executeWithRetry(() -> {
            return api.getDocuments(mget);
        });
    }

    @Override
    public Response indexDocument(
            final String name,
            final String type,
            final String id,
            final String document) throws ElasticException {
        return executeWithRetry(() -> {
            return api.indexDocument(name, type, id, document);
        });
    }

    @Override
    public Response indexDocument(
            final String name,
            final String type,
            final String id,
            final String document,
            final long epochMillisUtc) throws ElasticException {
        return executeWithRetry(() -> {
            return api.indexDocument(name, type, id, document, epochMillisUtc);
        });
    }

    @Override
    public Response indexExists(final String name) throws ElasticException {
        return executeWithRetry(() -> {
            return api.indexExists(name);
        });
    }

    @Override
    public Response refreshAllIndexes() throws ElasticException {
        return executeWithRetry(() -> {
            return api.refreshAllIndexes();
        });
    }

    @Override
    public Response refreshIndex(final String names) throws ElasticException {
        return executeWithRetry(() -> {
            return api.refreshIndex(names);
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy