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
13 changes: 9 additions & 4 deletions src/main/java/io/pinecone/clients/Pinecone.java
Original file line number Diff line number Diff line change
Expand Up @@ -1778,11 +1778,16 @@ public Pinecone build() {
apiClient = new ApiClient(customOkHttpClient);
} else {
apiClient = new ApiClient(buildOkHttpClient(proxyConfig));
if(host!=null && !host.isEmpty()) {
config.setHost(host);
apiClient.setBasePath(host);
}
}

// Host must be applied regardless of which OkHttpClient path was used above,
// otherwise an explicitly configured host is silently ignored whenever a
// customOkHttpClient is also supplied. See https://github.com/pinecone-io/pinecone-java-client/issues/192
if (host != null && !host.isEmpty()) {
config.setHost(host);
apiClient.setBasePath(host);
}

apiClient.setApiKey(config.getApiKey());
apiClient.setUserAgent(config.getUserAgent());
apiClient.addDefaultHeader("X-Pinecone-Api-Version", Configuration.VERSION);
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/io/pinecone/PineconeBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ public void PineconeWithOkHttpClientAndUserAgent() throws IOException {
assertEquals("lang=java; pineconeClientVersion=" + pineconeClientVersion, requestCaptor.getValue().header("User-Agent"));
}

@Test
public void PineconeWithHostAndCustomOkHttpClientUsesHost() throws IOException {
// Regression test for https://github.com/pinecone-io/pinecone-java-client/issues/192 --
// withHost(...) must still be honored when a custom OkHttpClient is also supplied.
String filePath = "src/test/resources/serverlessIndexJsonString.json";
String indexJsonStringServerless = new String(Files.readAllBytes(Paths.get(filePath)));

AbstractMap.SimpleEntry<Call, OkHttpClient> mockCallAndClient =
buildMockCallAndClient(ResponseBody.create(
indexJsonStringServerless,
MediaType.parse("application/json")));

OkHttpClient mockClient = mockCallAndClient.getValue();

Pinecone client = new Pinecone.Builder("testAPiKey")
.withHost("http://my-custom-host:5080")
.withOkHttpClient(mockClient)
.build();
// Create expected index after client creation to ensure JSON class is initialized
IndexModel expectedIndex = IndexModel.fromJson(indexJsonStringServerless);
IndexModel index = client.describeIndex("testIndex");

ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);

assertEquals(expectedIndex, index);
verify(mockClient, times(1)).newCall(requestCaptor.capture());
assertEquals("my-custom-host", requestCaptor.getValue().url().host());
assertEquals(5080, requestCaptor.getValue().url().port());
}

@Test
public void PineconeWithSourceTag() throws IOException {
String filePath = "src/test/resources/serverlessIndexJsonString.json";
Expand Down