-
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 all 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
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
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
100 changes: 70 additions & 30 deletions
100
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,92 @@ | ||
| package io.pinecone; | ||
|
|
||
| import io.pinecone.exceptions.FailedRequestInfo; | ||
| import io.pinecone.exceptions.HttpErrorMapper; | ||
| import io.pinecone.exceptions.PineconeConfigurationException; | ||
| import io.pinecone.model.CreateIndexRequest; | ||
| import org.asynchttpclient.AsyncHttpClient; | ||
| import org.asynchttpclient.DefaultAsyncHttpClient; | ||
| import okhttp3.*; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class PineconeIndexOperationClient { | ||
| private AsyncHttpClient client; | ||
| private PineconeClientConfig clientConfig; | ||
| private String url; | ||
| private final OkHttpClient client; | ||
| private final PineconeClientConfig clientConfig; | ||
| private final String url; | ||
| public static final String ACCEPT_HEADER = "accept"; | ||
| public static final String API_KEY_HEADER_NAME = "Api-Key"; | ||
| 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 DELETE = "DELETE"; | ||
| public static final String EMPTY_RESOURCE_PATH = ""; | ||
| public static final String POST = "POST"; | ||
| public static final String TEXT_PLAIN = "text/plain"; | ||
|
|
||
| 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()); | ||
| } | ||
|
|
||
| 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(); | ||
| private static String createUrl(PineconeClientConfig clientConfig) { | ||
| if (clientConfig.getApiKey() == null || clientConfig.getEnvironment() == null) { | ||
| throw new PineconeConfigurationException("Both API key and environment name are required for index operations."); | ||
| } | ||
|
|
||
| client.close(); | ||
| return BASE_URL_PREFIX + clientConfig.getEnvironment() + BASE_URL_SUFFIX; | ||
| } | ||
|
|
||
| public void deleteIndex(String indexName) throws IOException { | ||
| try (Response response = client.newCall(buildRequest(DELETE, indexName, TEXT_PLAIN,null)).execute()) { | ||
| handleResponse(response); | ||
| } | ||
| } | ||
|
|
||
| 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(); | ||
| MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); | ||
| RequestBody requestBody = RequestBody.create(createIndexRequest.toJson(), mediaType); | ||
|
|
||
| client.close(); | ||
| try (Response response = client.newCall(buildRequest(POST, EMPTY_RESOURCE_PATH, TEXT_PLAIN, requestBody)).execute()) { | ||
| handleResponse(response); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Request buildRequest(String method, String path, String header, RequestBody requestBody) { | ||
| Request.Builder builder = new Request.Builder() | ||
| .url(url + path) | ||
| .addHeader(ACCEPT_HEADER, header) | ||
| .addHeader(API_KEY_HEADER_NAME, clientConfig.getApiKey()); | ||
|
|
||
| if (POST.equals(method)) { | ||
| builder.post(requestBody); | ||
| builder.addHeader(CONTENT_TYPE, CONTENT_TYPE_JSON); | ||
| } else if (DELETE.equals(method)) { | ||
| builder.delete(); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| private void handleResponse(Response response) throws IOException { | ||
| if (!response.isSuccessful()) { | ||
| int statusCode = response.code(); | ||
| String responseBodyString = (response.body() != null) ? response.body().string() : null; | ||
| FailedRequestInfo failedRequestInfo = new FailedRequestInfo(statusCode, responseBodyString); | ||
| HttpErrorMapper.mapHttpStatusError(failedRequestInfo); | ||
| } | ||
| } | ||
|
|
||
| public void close() { | ||
| client.dispatcher().executorService().shutdown(); | ||
| client.connectionPool().evictAll(); | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
src/main/java/io/pinecone/exceptions/FailedRequestInfo.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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package io.pinecone.exceptions; | ||
|
|
||
| public class FailedRequestInfo { | ||
| private final int status; | ||
| private final String message; | ||
|
|
||
| public FailedRequestInfo(int status, String message) { | ||
| this.status = status; | ||
| this.message = message; | ||
| } | ||
|
|
||
| public int getStatus() { | ||
| return status; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } | ||
|
|
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,22 @@ | ||
| package io.pinecone.exceptions; | ||
|
|
||
| public class HttpErrorMapper { | ||
|
|
||
| public static void mapHttpStatusError(FailedRequestInfo failedRequestInfo) { | ||
| int statusCode = failedRequestInfo.getStatus(); | ||
| switch (statusCode) { | ||
| case 400: | ||
| throw new PineconeBadRequestException(failedRequestInfo.getMessage()); | ||
| case 401: | ||
| throw new PineconeAuthorizationException(failedRequestInfo.getMessage()); | ||
| case 404: | ||
| throw new PineconeNotFoundException(failedRequestInfo.getMessage()); | ||
| case 409: | ||
| throw new PineconeAlreadyExistsException(failedRequestInfo.getMessage()); | ||
| case 500: | ||
| throw new PineconeInternalServerException(failedRequestInfo.getMessage()); | ||
| default: | ||
| throw new PineconeUnmappedHttpException(failedRequestInfo.getMessage()); | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/pinecone/exceptions/PineconeAlreadyExistsException.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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.pinecone.exceptions; | ||
|
|
||
| public class PineconeAlreadyExistsException extends PineconeException { | ||
|
|
||
| public PineconeAlreadyExistsException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public PineconeAlreadyExistsException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/pinecone/exceptions/PineconeAuthorizationException.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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.pinecone.exceptions; | ||
|
|
||
| public class PineconeAuthorizationException extends PineconeException { | ||
|
|
||
| public PineconeAuthorizationException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public PineconeAuthorizationException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/pinecone/exceptions/PineconeBadRequestException.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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.pinecone.exceptions; | ||
|
|
||
| public class PineconeBadRequestException extends PineconeException { | ||
|
|
||
| public PineconeBadRequestException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public PineconeBadRequestException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/pinecone/exceptions/PineconeConfigurationException.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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.pinecone.exceptions; | ||
|
|
||
| public class PineconeConfigurationException extends PineconeException { | ||
|
|
||
| public PineconeConfigurationException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public PineconeConfigurationException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...n/java/io/pinecone/PineconeException.java → ...inecone/exceptions/PineconeException.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,4 +1,4 @@ | ||
| package io.pinecone; | ||
| package io.pinecone.exceptions; | ||
|
|
||
| public class PineconeException extends RuntimeException { | ||
|
|
||
|
|
||
12 changes: 12 additions & 0 deletions
12
src/main/java/io/pinecone/exceptions/PineconeInternalServerException.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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.pinecone.exceptions; | ||
|
|
||
| public class PineconeInternalServerException extends PineconeException { | ||
|
|
||
| public PineconeInternalServerException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public PineconeInternalServerException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/pinecone/exceptions/PineconeNotFoundException.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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.pinecone.exceptions; | ||
|
|
||
| public class PineconeNotFoundException extends PineconeException { | ||
|
|
||
| public PineconeNotFoundException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public PineconeNotFoundException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/pinecone/exceptions/PineconeUnmappedHttpException.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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package io.pinecone.exceptions; | ||
|
|
||
| public class PineconeUnmappedHttpException extends PineconeException { | ||
|
|
||
| public PineconeUnmappedHttpException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public PineconeUnmappedHttpException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...pinecone/PineconeValidationException.java → ...ceptions/PineconeValidationException.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
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
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.