Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ public class ChatPost {
}
```

If you need custom HTTP settings, provide your own `OkHttpClient`:

```java
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;

OkHttpClient customClient = new OkHttpClient.Builder()
.callTimeout(30, TimeUnit.SECONDS)
.build();

Cohere cohere = Cohere.builder()
.token("<<apiKey>>")
.clientName("snippet")
.httpClient(customClient)
.build();
```

### Handling Errors
When the API returns a non-success status code (4xx or 5xx response),
a subclass of [ApiError](src/main/java/com/Cohere/api/core/ApiError.java)
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/cohere/api/CohereBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.cohere.api.core.ClientOptions;
import com.cohere.api.core.Environment;
import okhttp3.OkHttpClient;

public final class CohereBuilder {
private ClientOptions.Builder clientOptionsBuilder = ClientOptions.builder();
Expand Down Expand Up @@ -42,6 +43,11 @@ public CohereBuilder url(String url) {
return this;
}

public CohereBuilder httpClient(OkHttpClient httpClient) {
this.clientOptionsBuilder.httpClient(httpClient);
return this;
}

public Cohere build() {
if (token == null) {
throw new RuntimeException("Please provide token or set the CO_API_KEY environment variable.");
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/cohere/api/core/ClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public static final class Builder {

private final Map<String, Supplier<String>> headerSuppliers = new HashMap<>();

private OkHttpClient httpClient;

public Builder environment(Environment environment) {
this.environment = environment;
return this;
Expand All @@ -95,10 +97,18 @@ public Builder addHeader(String key, Supplier<String> value) {
return this;
}

public Builder httpClient(OkHttpClient httpClient) {
this.httpClient = httpClient;
return this;
}

public ClientOptions build() {
OkHttpClient okhttpClient = new OkHttpClient.Builder()
.addInterceptor(new RetryInterceptor(3))
.build();
OkHttpClient okhttpClient = this.httpClient;
if (okhttpClient == null) {
okhttpClient = new OkHttpClient.Builder()
.addInterceptor(new RetryInterceptor(3))
.build();
}
return new ClientOptions(environment, headers, headerSuppliers, okhttpClient);
}
}
Expand Down