Skip to content

Commit

Permalink
Add option to pass custom HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanBratanov committed Jan 8, 2024
1 parent 56aee8c commit bc5f701
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/io/github/stefanbratanov/chatjpt/ChatJPT.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public static class Builder {
private String baseUrl = DEFAULT_BASE_URL;

private Optional<String> organization = Optional.empty();
private Optional<HttpClient> httpClient = Optional.empty();

Builder(String apiKey) {
this.apiKey = apiKey;
Expand All @@ -192,12 +193,23 @@ public Builder organization(String organization) {
return this;
}

/**
* @param httpClient a custom {@link HttpClient} which will be used for the API requests
*/
public Builder httpClient(HttpClient httpClient) {
this.httpClient = Optional.of(httpClient);
return this;
}

public ChatJPT build() {
if (!baseUrl.endsWith("/")) {
baseUrl += "/";
}
HttpClient httpClient = HttpClient.newBuilder().build();
return new ChatJPT(URI.create(baseUrl), apiKey, organization, httpClient);
return new ChatJPT(
URI.create(baseUrl),
apiKey,
organization,
httpClient.orElseGet(HttpClient::newHttpClient));
}
}
}

0 comments on commit bc5f701

Please sign in to comment.