diff --git a/internal/utils/interceptors.go b/internal/utils/interceptors.go index c6cda77..5241a4c 100644 --- a/internal/utils/interceptors.go +++ b/internal/utils/interceptors.go @@ -2,7 +2,6 @@ package utils import ( "context" - "fmt" "strings" "testing" @@ -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 diff --git a/pinecone/client.go b/pinecone/client.go index 6a06038..33695ef 100644 --- a/pinecone/client.go +++ b/pinecone/client.go @@ -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 @@ -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 diff --git a/pinecone/client_test.go b/pinecone/client_test.go index 39d5955..9431157 100644 --- a/pinecone/client_test.go +++ b/pinecone/client_test.go @@ -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 {