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

com.cohere.api.resources.datasets.DatasetsClient Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
/**
 * This file was auto-generated by Fern from our API Definition.
 */
package com.cohere.api.resources.datasets;

import com.cohere.api.core.ApiError;
import com.cohere.api.core.ClientOptions;
import com.cohere.api.core.ObjectMappers;
import com.cohere.api.core.RequestOptions;
import com.cohere.api.resources.datasets.requests.DatasetsCreateRequest;
import com.cohere.api.resources.datasets.requests.DatasetsListRequest;
import com.cohere.api.resources.datasets.types.DatasetsCreateResponse;
import com.cohere.api.resources.datasets.types.DatasetsGetResponse;
import com.cohere.api.resources.datasets.types.DatasetsGetUsageResponse;
import com.cohere.api.resources.datasets.types.DatasetsListResponse;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;
import java.util.Optional;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class DatasetsClient {
    protected final ClientOptions clientOptions;

    public DatasetsClient(ClientOptions clientOptions) {
        this.clientOptions = clientOptions;
    }

    /**
     * List datasets that have been created.
     */
    public DatasetsListResponse list() {
        return list(DatasetsListRequest.builder().build());
    }

    /**
     * List datasets that have been created.
     */
    public DatasetsListResponse list(DatasetsListRequest request) {
        return list(request, null);
    }

    /**
     * List datasets that have been created.
     */
    public DatasetsListResponse list(DatasetsListRequest request, RequestOptions requestOptions) {
        HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("datasets");
        if (request.getDatasetType().isPresent()) {
            httpUrl.addQueryParameter("datasetType", request.getDatasetType().get());
        }
        if (request.getBefore().isPresent()) {
            httpUrl.addQueryParameter("before", request.getBefore().get().toString());
        }
        if (request.getAfter().isPresent()) {
            httpUrl.addQueryParameter("after", request.getAfter().get().toString());
        }
        if (request.getLimit().isPresent()) {
            httpUrl.addQueryParameter("limit", request.getLimit().get().toString());
        }
        if (request.getOffset().isPresent()) {
            httpUrl.addQueryParameter("offset", request.getOffset().get().toString());
        }
        if (request.getValidationStatus().isPresent()) {
            httpUrl.addQueryParameter(
                    "validationStatus", request.getValidationStatus().get().toString());
        }
        Request.Builder _requestBuilder = new Request.Builder()
                .url(httpUrl.build())
                .method("GET", null)
                .headers(Headers.of(clientOptions.headers(requestOptions)))
                .addHeader("Content-Type", "application/json");
        Request okhttpRequest = _requestBuilder.build();
        try {
            OkHttpClient client = clientOptions.httpClient();
            if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
                client = clientOptions.httpClientWithTimeout(requestOptions);
            }
            Response response = client.newCall(okhttpRequest).execute();
            if (response.isSuccessful()) {
                return ObjectMappers.JSON_MAPPER.readValue(response.body().string(), DatasetsListResponse.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Create a dataset by uploading a file. See 'Dataset Creation' for more information.
     */
    public DatasetsCreateResponse create(File data, Optional evalData, DatasetsCreateRequest request) {
        return create(data, evalData, request, null);
    }

    /**
     * Create a dataset by uploading a file. See 'Dataset Creation' for more information.
     */
    public DatasetsCreateResponse create(
            File data, Optional evalData, DatasetsCreateRequest request, RequestOptions requestOptions) {
        HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("datasets");
        httpUrl.addQueryParameter("name", request.getName());
        httpUrl.addQueryParameter("type", request.getType().toString());
        if (request.getKeepOriginalFile().isPresent()) {
            httpUrl.addQueryParameter(
                    "keep_original_file", request.getKeepOriginalFile().get().toString());
        }
        if (request.getSkipMalformedInput().isPresent()) {
            httpUrl.addQueryParameter(
                    "skip_malformed_input",
                    request.getSkipMalformedInput().get().toString());
        }
        if (request.getKeepFields().isPresent()) {
            httpUrl.addQueryParameter("keep_fields", request.getKeepFields().get());
        }
        if (request.getOptionalFields().isPresent()) {
            httpUrl.addQueryParameter(
                    "optional_fields", request.getOptionalFields().get());
        }
        if (request.getTextSeparator().isPresent()) {
            httpUrl.addQueryParameter(
                    "text_separator", request.getTextSeparator().get());
        }
        if (request.getCsvDelimiter().isPresent()) {
            httpUrl.addQueryParameter("csv_delimiter", request.getCsvDelimiter().get());
        }
        if (request.getDryRun().isPresent()) {
            httpUrl.addQueryParameter("dry_run", request.getDryRun().get().toString());
        }
        MultipartBody.Builder body = new MultipartBody.Builder().setType(MultipartBody.FORM);
        try {
            String dataMimeType = Files.probeContentType(data.toPath());
            MediaType dataMediaType = dataMimeType != null ? MediaType.parse(dataMimeType) : null;
            body.addFormDataPart("data", data.getName(), RequestBody.create(dataMediaType, data));
            if (evalData.isPresent()) {
                String evalDataMimeType = Files.probeContentType(evalData.get().toPath());
                MediaType evalDataMediaType = evalDataMimeType != null ? MediaType.parse(evalDataMimeType) : null;
                body.addFormDataPart(
                        "eval_data", evalData.get().getName(), RequestBody.create(evalDataMediaType, evalData.get()));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Request.Builder _requestBuilder = new Request.Builder()
                .url(httpUrl.build())
                .method("POST", body.build())
                .headers(Headers.of(clientOptions.headers(requestOptions)));
        Request okhttpRequest = _requestBuilder.build();
        try {
            OkHttpClient client = clientOptions.httpClient();
            if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
                client = clientOptions.httpClientWithTimeout(requestOptions);
            }
            Response response = client.newCall(okhttpRequest).execute();
            if (response.isSuccessful()) {
                return ObjectMappers.JSON_MAPPER.readValue(response.body().string(), DatasetsCreateResponse.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * View the dataset storage usage for your Organization. Each Organization can have up to 10GB of storage across all their users.
     */
    public DatasetsGetUsageResponse getUsage() {
        return getUsage(null);
    }

    /**
     * View the dataset storage usage for your Organization. Each Organization can have up to 10GB of storage across all their users.
     */
    public DatasetsGetUsageResponse getUsage(RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("datasets/usage")
                .build();
        Request okhttpRequest = new Request.Builder()
                .url(httpUrl)
                .method("GET", null)
                .headers(Headers.of(clientOptions.headers(requestOptions)))
                .addHeader("Content-Type", "application/json")
                .build();
        try {
            OkHttpClient client = clientOptions.httpClient();
            if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
                client = clientOptions.httpClientWithTimeout(requestOptions);
            }
            Response response = client.newCall(okhttpRequest).execute();
            if (response.isSuccessful()) {
                return ObjectMappers.JSON_MAPPER.readValue(response.body().string(), DatasetsGetUsageResponse.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Retrieve a dataset by ID. See 'Datasets' for more information.
     */
    public DatasetsGetResponse get(String id) {
        return get(id, null);
    }

    /**
     * Retrieve a dataset by ID. See 'Datasets' for more information.
     */
    public DatasetsGetResponse get(String id, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("datasets")
                .addPathSegment(id)
                .build();
        Request okhttpRequest = new Request.Builder()
                .url(httpUrl)
                .method("GET", null)
                .headers(Headers.of(clientOptions.headers(requestOptions)))
                .addHeader("Content-Type", "application/json")
                .build();
        try {
            OkHttpClient client = clientOptions.httpClient();
            if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
                client = clientOptions.httpClientWithTimeout(requestOptions);
            }
            Response response = client.newCall(okhttpRequest).execute();
            if (response.isSuccessful()) {
                return ObjectMappers.JSON_MAPPER.readValue(response.body().string(), DatasetsGetResponse.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Delete a dataset by ID. Datasets are automatically deleted after 30 days, but they can also be deleted manually.
     */
    public Map delete(String id) {
        return delete(id, null);
    }

    /**
     * Delete a dataset by ID. Datasets are automatically deleted after 30 days, but they can also be deleted manually.
     */
    public Map delete(String id, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("datasets")
                .addPathSegment(id)
                .build();
        Request okhttpRequest = new Request.Builder()
                .url(httpUrl)
                .method("DELETE", null)
                .headers(Headers.of(clientOptions.headers(requestOptions)))
                .addHeader("Content-Type", "application/json")
                .build();
        try {
            OkHttpClient client = clientOptions.httpClient();
            if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
                client = clientOptions.httpClientWithTimeout(requestOptions);
            }
            Response response = client.newCall(okhttpRequest).execute();
            if (response.isSuccessful()) {
                return ObjectMappers.JSON_MAPPER.readValue(
                        response.body().string(), new TypeReference>() {});
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy