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

io.deepsense.neptune.clientlibrary.services.apiservice.SimpleApiService Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
/**
 * Copyright (c) 2016, CodiLime Inc.
 */

package io.deepsense.neptune.clientlibrary.services.apiservice;

import com.google.common.base.Preconditions;
import io.deepsense.neptune.apiclient.ApiClient;
import io.deepsense.neptune.apiclient.ApiException;
import io.deepsense.neptune.apiclient.api.DefaultApi;
import io.deepsense.neptune.apiclient.auth.HttpBasicAuth;
import io.deepsense.neptune.apiclient.model.Channel;
import io.deepsense.neptune.apiclient.model.Chart;
import io.deepsense.neptune.apiclient.model.ConfigInfo;
import io.deepsense.neptune.apiclient.model.EditJobParams;
import io.deepsense.neptune.apiclient.model.Experiment;
import io.deepsense.neptune.apiclient.model.InputChannelValues;
import io.deepsense.neptune.apiclient.model.Job;
import io.deepsense.neptune.apiclient.model.KeyValueProperty;
import io.deepsense.neptune.clientlibrary.api.Credentials;
import io.deepsense.neptune.clientlibrary.models.ChannelParams;
import io.deepsense.neptune.clientlibrary.models.ChartParams;
import io.deepsense.neptune.clientlibrary.services.apimodelconverter.ApiModelConverter;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

public class SimpleApiService implements ApiService {

    private final ApiModelConverter apiModelConverter;

    private final DefaultApi apiClient;

    SimpleApiService(ApiModelConverter apiModelConverter, Credentials credentials, String baseUrl) {
        this.apiClient = new DefaultApi(createApiClient(
                Preconditions.checkNotNull(baseUrl),
                Preconditions.checkNotNull(credentials)));
        this.apiModelConverter = Preconditions.checkNotNull(apiModelConverter);
    }

    public SimpleApiService(ApiModelConverter apiModelConverter, Credentials credentials, String hostname, int port) {
        this(apiModelConverter,
                credentials,
                String.format(
                        "http://%s:%d",
                        Preconditions.checkNotNull(hostname),
                        port));
    }

    @Override
    public Channel createChannel(UUID jobId, ChannelParams channelParams) throws ApiException {
        return apiClient.channelsPost(
                jobId.toString(),
                apiModelConverter.channelParamsToApiModel(channelParams),
                "",
                "");
    }

    @Override
    public Chart createChart(UUID jobId, ChartParams chartParams) throws ApiException {
        return apiClient.chartsPost(
                jobId.toString(),
                apiModelConverter.chartParamsToApiModel(chartParams),
                "",
                "");
    }

    @Override
    public Chart updateChart(UUID jobId,
                             UUID chartId,
                             io.deepsense.neptune.clientlibrary.models.Chart chart) throws ApiException {
        return apiClient.chartsChartIdPut(
                jobId.toString(),
                chartId.toString(),
                apiModelConverter.chartToApiModel(chart),
                "",
                "");
    }

    @Override
    public Job getJob(UUID jobId) throws ApiException {
        return apiClient.jobsJobIdGet(jobId.toString());
    }

    @Override
    public Experiment getExperiment(UUID experimentId) throws ApiException {
        return apiClient.getExperiment(experimentId.toString());
    }

    @Override
    public void pingJob(UUID jobId) throws ApiException {
        apiClient.jobsJobIdPingPost(jobId.toString(), "", "");
    }

    @Override
    public void updateTags(UUID jobId, List tags) throws ApiException {
        apiClient.jobsJobIdPut(
                jobId.toString(),
                new EditJobParams()
                        .tags(tags)
                        .properties(null)
                        .channels(null)
                        .charts(null),
                "",
                "");
    }

    @Override
    public void updateProperties(UUID jobId, Map properties) throws ApiException {
        List apiProperties = properties.entrySet().stream()
                .map(propertyEntry -> new KeyValueProperty()
                        .key(propertyEntry.getKey())
                        .value(propertyEntry.getValue()))
                .collect(Collectors.toList());

        apiClient.jobsJobIdPut(
                jobId.toString(),
                new EditJobParams()
                        .tags(null)
                        .properties(apiProperties)
                        .channels(null)
                        .charts(null),
                "",
                ""
                );
    }

    @Override
    public void sendChannelValues(UUID jobId, List channelValues) throws ApiException {
        apiClient.postChannelValues(jobId.toString(), channelValues, "", "");
    }

    @Override
    public ConfigInfo getConfigInfo() throws ApiException {
        return apiClient.configInfoGet();
    }

    private static ApiClient createApiClient(String baseUrl, Credentials credentials) {
        ApiClient client = new ApiClient();
        client.setBasePath(baseUrl);
        if (credentials.getUsername().isPresent() && credentials.getPassword().isPresent()) {
            addAuthenticationHeader(client, credentials);
        }
        return client;
    }

    private static void addAuthenticationHeader(ApiClient apiClient, Credentials credentials) {
        HttpBasicAuth httpBasicAuth = new HttpBasicAuth();
        httpBasicAuth.setUsername(credentials.getUsername().orElse(null));
        httpBasicAuth.setPassword(credentials.getPassword().orElse(null));

        Map headers = new HashMap<>();
        httpBasicAuth.applyToParams(Collections.emptyList(), headers);

        Map.Entry authenticationHeader = headers.entrySet().iterator().next();
        apiClient.addDefaultHeader(authenticationHeader.getKey(), authenticationHeader.getValue());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy