Skip to content

Commit

Permalink
add support to list indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanshah18 committed Oct 26, 2023
1 parent eef17f8 commit 16bcfaf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -41,6 +42,11 @@ public void createAndDelete() throws IOException {
assertEquals(indexName, indexMeta.getDatabase().getName());
assertEquals("euclidean", indexMeta.getDatabase().getMetric());

// List the index
List<String> indexList = pinecone.listIndexes();
assertEquals(1, indexList.size());
assertEquals(indexList.get(0), indexName);

// Cleanup
pinecone.deleteIndex(indexName);
}
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/io/pinecone/PineconeIndexOperationClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.pinecone;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.pinecone.exceptions.FailedRequestInfo;
import io.pinecone.exceptions.HttpErrorMapper;
import io.pinecone.exceptions.PineconeConfigurationException;
Expand All @@ -8,6 +9,8 @@
import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PineconeIndexOperationClient {
private final OkHttpClient client;
Expand All @@ -19,6 +22,7 @@ public class PineconeIndexOperationClient {
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 CONTENT_TYPE_CHARSET_UTF8 = "charset=utf-8";
public static final String EMPTY_RESOURCE_PATH = "";
public static final String HTTP_METHOD_DELETE = "DELETE";
public static final String HTTP_METHOD_GET = "GET";
Expand Down Expand Up @@ -48,7 +52,7 @@ private static String createUrl(PineconeClientConfig clientConfig) {
}

public void deleteIndex(String indexName) throws IOException {
Request request = buildRequest(HTTP_METHOD_DELETE, indexName, TEXT_PLAIN,null);
Request request = buildRequest(HTTP_METHOD_DELETE, indexName, TEXT_PLAIN, null);
try (Response response = client.newCall(request).execute()) {
handleResponse(response);
}
Expand All @@ -71,19 +75,30 @@ public IndexMeta describeIndex(String indexName) throws IOException {
}
}

public List<String> listIndexes() throws IOException {
String acceptHeaders = CONTENT_TYPE_JSON + "; " + CONTENT_TYPE_CHARSET_UTF8;
List<String> indexList;
ObjectMapper objectMapper = new ObjectMapper();
Request request = buildRequest(HTTP_METHOD_GET, EMPTY_RESOURCE_PATH, acceptHeaders, null);
try (Response response = client.newCall(request).execute()) {
handleResponse(response);
String responseBodyString = response.body().string();
indexList = objectMapper.readValue(responseBodyString, ArrayList.class);
}
return indexList;
}

private Request buildRequest(String method, String path, String acceptHeader, RequestBody requestBody) {
Request.Builder builder = new Request.Builder()
.url(url + path)
.addHeader(ACCEPT_HEADER, acceptHeader)
.addHeader(API_KEY_HEADER_NAME, clientConfig.getApiKey());

if (HTTP_METHOD_POST.equals(method)) {
builder.post(requestBody);
builder.addHeader(CONTENT_TYPE, CONTENT_TYPE_JSON);
} else if (HTTP_METHOD_DELETE.equals(method)) {
builder.delete();
}

return builder.build();
}

Expand Down
48 changes: 46 additions & 2 deletions src/test/java/io/pinecone/PineconeIndexOperationClientTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.pinecone;

import io.pinecone.PineconeClientConfig;
import io.pinecone.PineconeIndexOperationClient;
import io.pinecone.exceptions.PineconeConfigurationException;
import io.pinecone.exceptions.PineconeValidationException;
import io.pinecone.model.*;
Expand All @@ -15,6 +13,8 @@
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -221,4 +221,48 @@ public void testDescribeIndex() throws IOException {

assertEquals(expectedIndexMeta.toString(), actualIndexMeta.toString());
}

@Test
public void testListIndexes() throws IOException {
String responseBody = "[\"index1\",\"index2\"]";
PineconeClientConfig clientConfig = new PineconeClientConfig()
.withApiKey("api_key")
.withEnvironment("testEnvironment");

Call mockCall = mock(Call.class);

Response mockResponse = new Response.Builder()
.request(new Request.Builder().url("http://localhost").build())
.protocol(Protocol.HTTP_1_1)
.code(200)
.message("The index has been successfully deleted.")
.body(ResponseBody.create(responseBody, MediaType.parse("text/plain")))
.build();

OkHttpClient mockClient = mock(OkHttpClient.class);
when(mockClient.newCall(any(Request.class))).thenReturn(mockCall);
when(mockCall.execute()).thenReturn(mockResponse);

PineconeIndexOperationClient indexOperationClient = new PineconeIndexOperationClient(clientConfig, mockClient);
List<String> indexList = indexOperationClient.listIndexes();
assertEquals(2, indexList.size());
assertEquals(indexList.get(0), "index1");
assertEquals(indexList.get(1), "index2");

// Empty response
String emptyResponseBody = "[]";
Response mockEmptyResponse = new Response.Builder()
.request(new Request.Builder().url("http://localhost").build())
.protocol(Protocol.HTTP_1_1)
.code(200)
.message("Empty response.")
.body(ResponseBody.create(emptyResponseBody, MediaType.parse("text/plain")))
.build();

when(mockCall.execute()).thenReturn(mockEmptyResponse);

List<String> emptyIndexList = indexOperationClient.listIndexes();
assertNotNull(emptyIndexList);
assertTrue(emptyIndexList.isEmpty());
}
}

0 comments on commit 16bcfaf

Please sign in to comment.