com.cohere.api.resources.connectors.ConnectorsClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cohere-java Show documentation
Show all versions of cohere-java Show documentation
The official Java library for Cohere's API.
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.cohere.api.resources.connectors;
import com.cohere.api.core.ApiError;
import com.cohere.api.core.ClientOptions;
import com.cohere.api.core.MediaTypes;
import com.cohere.api.core.ObjectMappers;
import com.cohere.api.core.RequestOptions;
import com.cohere.api.resources.connectors.requests.ConnectorsListRequest;
import com.cohere.api.resources.connectors.requests.ConnectorsOAuthAuthorizeRequest;
import com.cohere.api.resources.connectors.requests.CreateConnectorRequest;
import com.cohere.api.resources.connectors.requests.UpdateConnectorRequest;
import com.cohere.api.types.CreateConnectorResponse;
import com.cohere.api.types.GetConnectorResponse;
import com.cohere.api.types.ListConnectorsResponse;
import com.cohere.api.types.OAuthAuthorizeResponse;
import com.cohere.api.types.UpdateConnectorResponse;
import com.fasterxml.jackson.core.type.TypeReference;
import java.io.IOException;
import java.util.Map;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class ConnectorsClient {
protected final ClientOptions clientOptions;
public ConnectorsClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
/**
* Returns a list of connectors ordered by descending creation date (newer first). See 'Managing your Connector' for more information.
*/
public ListConnectorsResponse list() {
return list(ConnectorsListRequest.builder().build());
}
/**
* Returns a list of connectors ordered by descending creation date (newer first). See 'Managing your Connector' for more information.
*/
public ListConnectorsResponse list(ConnectorsListRequest request) {
return list(request, null);
}
/**
* Returns a list of connectors ordered by descending creation date (newer first). See 'Managing your Connector' for more information.
*/
public ListConnectorsResponse list(ConnectorsListRequest request, RequestOptions requestOptions) {
HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("connectors");
if (request.getLimit().isPresent()) {
httpUrl.addQueryParameter("limit", request.getLimit().get().toString());
}
if (request.getOffset().isPresent()) {
httpUrl.addQueryParameter("offset", request.getOffset().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();
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ListConnectorsResponse.class);
}
throw new ApiError(
response.code(),
ObjectMappers.JSON_MAPPER.readValue(
responseBody != null ? responseBody.string() : "{}", Object.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Creates a new connector. The connector is tested during registration and will cancel registration when the test is unsuccessful. See 'Creating and Deploying a Connector' for more information.
*/
public CreateConnectorResponse create(CreateConnectorRequest request) {
return create(request, null);
}
/**
* Creates a new connector. The connector is tested during registration and will cancel registration when the test is unsuccessful. See 'Creating and Deploying a Connector' for more information.
*/
public CreateConnectorResponse create(CreateConnectorRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("connectors")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
} catch (Exception e) {
throw new RuntimeException(e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.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();
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CreateConnectorResponse.class);
}
throw new ApiError(
response.code(),
ObjectMappers.JSON_MAPPER.readValue(
responseBody != null ? responseBody.string() : "{}", Object.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Retrieve a connector by ID. See 'Connectors' for more information.
*/
public GetConnectorResponse get(String id) {
return get(id, null);
}
/**
* Retrieve a connector by ID. See 'Connectors' for more information.
*/
public GetConnectorResponse get(String id, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("connectors")
.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();
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), GetConnectorResponse.class);
}
throw new ApiError(
response.code(),
ObjectMappers.JSON_MAPPER.readValue(
responseBody != null ? responseBody.string() : "{}", Object.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Delete a connector by ID. See 'Connectors' for more information.
*/
public Map delete(String id) {
return delete(id, null);
}
/**
* Delete a connector by ID. See 'Connectors' for more information.
*/
public Map delete(String id, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("connectors")
.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();
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(
responseBody.string(), new TypeReference