diff --git a/src/main/java/io/pinecone/clients/Pinecone.java b/src/main/java/io/pinecone/clients/Pinecone.java index 9d7cc082..6ef380d2 100644 --- a/src/main/java/io/pinecone/clients/Pinecone.java +++ b/src/main/java/io/pinecone/clients/Pinecone.java @@ -1778,11 +1778,14 @@ public Pinecone build() { apiClient = new ApiClient(customOkHttpClient); } else { apiClient = new ApiClient(buildOkHttpClient(proxyConfig)); - if(host!=null && !host.isEmpty()) { - config.setHost(host); - apiClient.setBasePath(host); - } } + + // Set host regardless of whether customOkHttpClient is used + 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); diff --git a/src/test/java/io/pinecone/PineconeBuilderTest.java b/src/test/java/io/pinecone/PineconeBuilderTest.java index 9557f973..eb4315e1 100644 --- a/src/test/java/io/pinecone/PineconeBuilderTest.java +++ b/src/test/java/io/pinecone/PineconeBuilderTest.java @@ -104,4 +104,42 @@ public void PineconeWithSourceTag() throws IOException { verify(mockClient, times(1)).newCall(requestCaptor.capture()); assertEquals("lang=java; pineconeClientVersion=" + pineconeClientVersion + "; source_tag=testSourceTag", requestCaptor.getValue().header("User-Agent")); } + + @Test + public void PineconeWithHostAndCustomOkHttpClient() { + String customHost = "http://localhost:5080"; + OkHttpClient customClient = new OkHttpClient.Builder().build(); + + // Verify that the builder doesn't throw an exception when both host and custom client are set + Pinecone client = new Pinecone.Builder("testApiKey") + .withHost(customHost) + .withOkHttpClient(customClient) + .build(); + + assertNotNull(client, "Pinecone client should be created successfully with both host and custom OkHttpClient"); + } + + @Test + public void PineconeWithHostButNoCustomOkHttpClient() { + String customHost = "http://localhost:5080"; + + // Verify that the builder doesn't throw an exception when host is set without custom client + Pinecone client = new Pinecone.Builder("testApiKey") + .withHost(customHost) + .build(); + + assertNotNull(client, "Pinecone client should be created successfully with host but no custom OkHttpClient"); + } + + @Test + public void PineconeWithCustomOkHttpClientButNoHost() { + OkHttpClient customClient = new OkHttpClient.Builder().build(); + + // Verify that the builder doesn't throw an exception when custom client is set without host + Pinecone client = new Pinecone.Builder("testApiKey") + .withOkHttpClient(customClient) + .build(); + + assertNotNull(client, "Pinecone client should be created successfully with custom OkHttpClient but no host"); + } } \ No newline at end of file