-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fc98e8
commit 8e8daa1
Showing
13 changed files
with
1,394 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
src/main/java/com/cohere/api/resources/datasets/DatasetsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/** | ||
* 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.DatasetsListRequest; | ||
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.IOException; | ||
import java.util.Map; | ||
import okhttp3.Headers; | ||
import okhttp3.HttpUrl; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class DatasetsClient { | ||
protected final ClientOptions clientOptions; | ||
|
||
public DatasetsClient(ClientOptions clientOptions) { | ||
this.clientOptions = clientOptions; | ||
} | ||
|
||
public DatasetsListResponse list() { | ||
return list(DatasetsListRequest.builder().build()); | ||
} | ||
|
||
public DatasetsListResponse list(DatasetsListRequest request, RequestOptions requestOptions) { | ||
HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) | ||
.newBuilder() | ||
.addPathSegments("dataset"); | ||
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()); | ||
} | ||
if (request.getOffset().isPresent()) { | ||
httpUrl.addQueryParameter("offset", request.getOffset().get()); | ||
} | ||
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 { | ||
Response response = | ||
clientOptions.httpClient().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); | ||
} | ||
} | ||
|
||
public DatasetsListResponse list(DatasetsListRequest request) { | ||
return list(request, null); | ||
} | ||
|
||
public DatasetsGetUsageResponse getUsage(RequestOptions requestOptions) { | ||
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) | ||
.newBuilder() | ||
.addPathSegments("dataset/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 { | ||
Response response = | ||
clientOptions.httpClient().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); | ||
} | ||
} | ||
|
||
public DatasetsGetUsageResponse getUsage() { | ||
return getUsage(null); | ||
} | ||
|
||
public DatasetsGetResponse get(String id, RequestOptions requestOptions) { | ||
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) | ||
.newBuilder() | ||
.addPathSegments("dataset") | ||
.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 { | ||
Response response = | ||
clientOptions.httpClient().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); | ||
} | ||
} | ||
|
||
public DatasetsGetResponse get(String id) { | ||
return get(id, null); | ||
} | ||
|
||
public Map<String, Object> delete(String id, RequestOptions requestOptions) { | ||
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) | ||
.newBuilder() | ||
.addPathSegments("dataset") | ||
.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 { | ||
Response response = | ||
clientOptions.httpClient().newCall(okhttpRequest).execute(); | ||
if (response.isSuccessful()) { | ||
return ObjectMappers.JSON_MAPPER.readValue( | ||
response.body().string(), new TypeReference<Map<String, Object>>() {}); | ||
} | ||
throw new ApiError( | ||
response.code(), | ||
ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class)); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public Map<String, Object> delete(String id) { | ||
return delete(id, null); | ||
} | ||
} |
Oops, something went wrong.