-
Notifications
You must be signed in to change notification settings - Fork 13
Update http client and improve error handling #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f9db410
update http client
rohanshah18 338f33c
Add unit test for create index
rohanshah18 b5ac2d8
indent files
rohanshah18 b080e18
Add validations to prevent exceptions
rohanshah18 3187111
Improve client error
rohanshah18 9048bc0
Update unit test
rohanshah18 e7638a8
address code review comments
rohanshah18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
91 changes: 58 additions & 33 deletions
91
src/main/java/io/pinecone/PineconeIndexOperationClient.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,52 +1,77 @@ | ||
| package io.pinecone; | ||
|
|
||
| import io.pinecone.model.CreateIndexRequest; | ||
| import org.asynchttpclient.AsyncHttpClient; | ||
| import org.asynchttpclient.DefaultAsyncHttpClient; | ||
|
|
||
| import okhttp3.*; | ||
| import java.io.IOException; | ||
| import static io.pinecone.utils.Constants.*; | ||
|
|
||
| public class PineconeIndexOperationClient { | ||
| private AsyncHttpClient client; | ||
| private PineconeClientConfig clientConfig; | ||
| private String url; | ||
| private final OkHttpClient client; | ||
| private final PineconeClientConfig clientConfig; | ||
| private final String url; | ||
|
|
||
| PineconeIndexOperationClient(PineconeClientConfig clientConfig, AsyncHttpClient client) { | ||
| this.clientConfig = clientConfig; | ||
| private PineconeIndexOperationClient(PineconeClientConfig clientConfig, OkHttpClient client, String url) { | ||
| this.client = client; | ||
| this.url = "https://controller." + clientConfig.getEnvironment() + ".pinecone.io/databases/"; | ||
| this.clientConfig = clientConfig; | ||
| this.url = url; | ||
| } | ||
|
|
||
| public PineconeIndexOperationClient(PineconeClientConfig clientConfig, OkHttpClient client) { | ||
| this(clientConfig, client, createUrl(clientConfig)); | ||
rohanshah18 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public PineconeIndexOperationClient(PineconeClientConfig clientConfig) { | ||
| this.clientConfig = clientConfig; | ||
| client = new DefaultAsyncHttpClient(); | ||
| this.url = "https://controller." + clientConfig.getEnvironment() + ".pinecone.io/databases/"; | ||
| this(clientConfig, new OkHttpClient()); | ||
| } | ||
|
|
||
| private static String createUrl(PineconeClientConfig clientConfig) { | ||
| if (clientConfig.getApiKey() == null || clientConfig.getEnvironment() == null) { | ||
| throw new PineconeValidationException("Both API key and environment name are required for index operations."); | ||
rohanshah18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rohanshah18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return BASE_URL_PREFIX + clientConfig.getEnvironment() + BASE_URL_SUFFIX; | ||
| } | ||
|
|
||
| public void deleteIndex(String indexName) throws IOException { | ||
| System.out.println("Sending delete index request:"); | ||
| client.prepare("DELETE", url + indexName) | ||
| .setHeader("accept", "text/plain") | ||
| .setHeader("Api-Key", clientConfig.getApiKey()) | ||
| .execute() | ||
| .toCompletableFuture() | ||
| .join(); | ||
|
|
||
| client.close(); | ||
| Request request = new Request.Builder() | ||
rohanshah18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .url(url + indexName) | ||
| .delete() | ||
| .addHeader(ACCEPT_HEADER, TEXT_PLAIN) | ||
| .addHeader(API_KEY, clientConfig.getApiKey()) | ||
| .build(); | ||
|
|
||
| try (Response response = client.newCall(request).execute()) { | ||
| if (!response.isSuccessful()) { | ||
| throw new IOException(response.message()); | ||
| } | ||
| } finally { | ||
| close(client); | ||
rohanshah18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| public void createIndex(CreateIndexRequest createIndexRequest) throws IOException { | ||
| client.prepare("POST", url) | ||
| .setHeader("accept", "text/plain") | ||
| .setHeader("content-type", "application/json") | ||
| .setHeader("Api-Key", clientConfig.getApiKey()) | ||
| .setBody(createIndexRequest.toJson()) | ||
| .execute() | ||
| .toCompletableFuture() | ||
| .join(); | ||
|
|
||
| client.close(); | ||
| } | ||
| } | ||
| MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); | ||
| RequestBody requestBody = RequestBody.create(createIndexRequest.toJson(), mediaType); | ||
|
|
||
| Request request = new Request.Builder() | ||
| .url(url) | ||
| .post(requestBody) | ||
| .addHeader(ACCEPT_HEADER, TEXT_PLAIN) | ||
| .addHeader(CONTENT_TYPE, CONTENT_TYPE_JSON) | ||
| .addHeader(API_KEY, clientConfig.getApiKey()) | ||
| .build(); | ||
|
|
||
| try (Response response = client.newCall(request).execute()) { | ||
rohanshah18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!response.isSuccessful()) { | ||
| throw new IOException(response.message()); | ||
| } | ||
| } finally { | ||
| close(client); | ||
| } | ||
| } | ||
|
|
||
| public void close(OkHttpClient client) { | ||
rohanshah18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| client.dispatcher().executorService().shutdown(); | ||
| client.connectionPool().evictAll(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,11 @@ | ||
| package io.pinecone.utils; | ||
|
|
||
| public class Constants { | ||
rohanshah18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public static final String ACCEPT_HEADER = "accept"; | ||
| public static final String API_KEY = "Api-Key"; | ||
rohanshah18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public static final String BASE_URL_PREFIX = "https://controller."; | ||
| public static final String BASE_URL_SUFFIX = ".pinecone.io/databases/"; | ||
| public static final String CONTENT_TYPE = "content-type"; | ||
| public static final String CONTENT_TYPE_JSON = "application/json"; | ||
| public static final String TEXT_PLAIN = "text/plain"; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.