Skip to content

Commit

Permalink
add doc comments for Index and NewIndexConnParams, add new unit test …
Browse files Browse the repository at this point in the history
…to validate Client.Index
  • Loading branch information
austin-denoble committed Jul 3, 2024
1 parent 8b50028 commit a0365de
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
2 changes: 0 additions & 2 deletions internal/utils/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"context"
"fmt"
"strings"
"testing"

Expand All @@ -23,7 +22,6 @@ func MetadataInterceptor(t *testing.T, expectedMetadata map[string]string) grpc.
opts ...grpc.CallOption,
) error {
metadata, _ := metadata.FromOutgoingContext(ctx)
fmt.Printf("METADATA ?: %+v\n", metadata)
metadataString := metadata.String()

// Check that the outgoing context has the metadata we expect
Expand Down
53 changes: 53 additions & 0 deletions pinecone/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ type NewClientBaseParams struct {
SourceTag string
}

// NewIndexConnParams holds the parameters for creating an IndexConnection to a Pinecone index.
//
// Fields:
// - Host: The host URL of the Pinecone index. This is required. To find your host url use the DescribeIndex or ListIndexes methods.
// Alternatively, the host is displayed in the Pinecone web console.
// - Namespace: Optional index namespace to use for operations. If not provided, the default namespace of "" will be used.
// - AdditionalMetdata: Optional additional metdata to be sent with each RPC request.
//
// See Client.Index for code example.
type NewIndexConnParams struct {
Host string // required - obtained through DescribeIndex or ListIndexes
Namespace string // optional - if not provided the default namespace of "" will be used
Expand Down Expand Up @@ -87,6 +96,50 @@ func NewClientBase(in NewClientBaseParams) (*Client, error) {
return &c, nil
}

// Index creates an IndexConnection to a specified host.
//
// Parameters:
// - in: A NewIndexConnParams object that includes the necessary configuration to create an IndexConnection.
// See NewIndexConnParams for more information.
//
// Note: It is important to handle the error returned by this method to ensure that the IndexConnection is created
// successfully before making data plane calls.
//
// Returns a pointer to an IndexConnection instance or an error.
//
// Example:
//
// ctx := context.Background()
//
// clientParams := pinecone.NewClientParams{
// ApiKey: "YOUR_API_KEY",
// SourceTag: "your_source_identifier", // optional
// }
//
// pc, err := pinecone.NewClient(clientParams)
// if err != nil {
// log.Fatalf("Failed to create Client: %v", err)
// } else {
// fmt.Println("Successfully created a new Client object!")
// }
//
// idx, err := pc.DescribeIndex(ctx, "your-index-name")
// if err != nil {
// log.Fatalf("Failed to describe index \"%s\". Error:%s", idx.Name, err)
// } else {
// fmt.Printf("Successfully found the \"%s\" index!\n", idx.Name)
// }
//
// indexConnParams := pinecone.NewIndexConnParams{
// Host: idx.Host
// }
//
// idxConnection, err := pc.Index(indexConnParams)
// if err != nil {
// log.Fatalf("Failed to create IndexConnection for Host: %v. Error: %v", idx.Host, err)
// } else {
// log.Println("IndexConnection created successfully!")
// }
func (c *Client) Index(in NewIndexConnParams, dialOpts ...grpc.DialOption) (*IndexConnection, error) {
// extract authHeader from Client which is used to authenticate the IndexConnection
// merge authHeader with additionalMetadata provided in NewIndexConnParams
Expand Down
25 changes: 25 additions & 0 deletions pinecone/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,31 @@ func (ts *ClientTests) TestControllerHostOverrideFromEnv() {
}

// TODO - test Index() function applying proper sourceTag and extractAuthHeader is working as expected
func (ts *ClientTests) TestApiKeyPassedToIndexConnection() {
apiKey := "test-api-key"

client, err := NewClient(NewClientParams{ApiKey: apiKey})
if err != nil {
ts.FailNow(err.Error())
}

indexConn, err := client.Index(NewIndexConnParams{Host: "my-index-host.io"})
if err != nil {
ts.FailNow(err.Error())
}

indexMetadata := indexConn.additionalMetadata
metadataHasApiKey := false
fmt.Printf("INDEX METADATA: %v\n", indexMetadata)
for key, value := range indexMetadata {
if key == "Api-Key" && value == apiKey {
metadataHasApiKey = true
break
}
}

assert.True(ts.T(), metadataHasApiKey, "Expected IndexConnection metadata to contain 'Api-Key' with value '%s'", apiKey)
}

func (ts *ClientTests) TestControllerHostNormalization() {
tests := []struct {
Expand Down

0 comments on commit a0365de

Please sign in to comment.