Skip to content

Commit

Permalink
add newIndexParameters struct and swap to using that for newIndexConn…
Browse files Browse the repository at this point in the history
…ection
  • Loading branch information
austin-denoble committed May 1, 2024
1 parent 395be59 commit e5753fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pinecone/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *Client) IndexWithNamespace(host string, namespace string) (*IndexConnec
}

func (c *Client) IndexWithAdditionalMetadata(host string, namespace string, additionalMetadata map[string]string) (*IndexConnection, error) {
idx, err := newIndexConnection(c.apiKey, host, namespace, c.sourceTag, additionalMetadata)
idx, err := newIndexConnection(newIndexParameters{apiKey: c.apiKey, host: host, namespace: namespace, sourceTag: c.sourceTag, additionalMetadata: additionalMetadata})
if err != nil {
return nil, err
}
Expand All @@ -82,7 +82,7 @@ func (c *Client) ListIndexes(ctx context.Context) ([]*Index, error) {
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode)
}

var indexList control.IndexList
err = json.NewDecoder(res.Body).Decode(&indexList)
if err != nil {
Expand Down
16 changes: 12 additions & 4 deletions pinecone/index_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ type IndexConnection struct {
grpcConn *grpc.ClientConn
}

func newIndexConnection(apiKey string, host string, namespace string, sourceTag string, additionalMetadata map[string]string) (*IndexConnection, error) {
type newIndexParameters struct {
apiKey string
host string
namespace string
sourceTag string
additionalMetadata map[string]string
}

func newIndexConnection(in newIndexParameters) (*IndexConnection, error) {
config := &tls.Config{}
target := fmt.Sprintf("%s:443", host)
target := fmt.Sprintf("%s:443", in.host)
conn, err := grpc.Dial(
target,
grpc.WithTransportCredentials(credentials.NewTLS(config)),
grpc.WithAuthority(target),
grpc.WithBlock(),
grpc.WithUserAgent(useragent.BuildUserAgentGRPC(sourceTag)),
grpc.WithUserAgent(useragent.BuildUserAgentGRPC(in.sourceTag)),
)

if err != nil {
Expand All @@ -39,7 +47,7 @@ func newIndexConnection(apiKey string, host string, namespace string, sourceTag

dataClient := data.NewVectorServiceClient(conn)

idx := IndexConnection{Namespace: namespace, apiKey: apiKey, dataClient: &dataClient, grpcConn: conn, additionalMetadata: additionalMetadata}
idx := IndexConnection{Namespace: in.namespace, apiKey: in.apiKey, dataClient: &dataClient, grpcConn: conn, additionalMetadata: in.additionalMetadata}
return &idx, nil
}

Expand Down
10 changes: 5 additions & 5 deletions pinecone/index_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func (ts *IndexConnectionTests) SetupSuite() {
namespace, err := uuid.NewV7()
assert.NoError(ts.T(), err)

idxConn, err := newIndexConnection(ts.apiKey, ts.host, namespace.String(), "", nil)
idxConn, err := newIndexConnection(newIndexParameters{apiKey: ts.apiKey, host: ts.host, namespace: namespace.String(), sourceTag: ""})
assert.NoError(ts.T(), err)
ts.idxConn = idxConn

ts.sourceTag = "test_source_tag"
idxConnSourceTag, err := newIndexConnection(ts.apiKey, ts.host, namespace.String(), ts.sourceTag, nil)
idxConnSourceTag, err := newIndexConnection(newIndexParameters{apiKey: ts.apiKey, host: ts.host, namespace: namespace.String(), sourceTag: ts.sourceTag})
assert.NoError(ts.T(), err)
ts.idxConnSourceTag = idxConnSourceTag

Expand All @@ -97,7 +97,7 @@ func (ts *IndexConnectionTests) TestNewIndexConnection() {
apiKey := "test-api-key"
namespace := ""
sourceTag := ""
idxConn, err := newIndexConnection(apiKey, ts.host, namespace, sourceTag, nil)
idxConn, err := newIndexConnection(newIndexParameters{apiKey: apiKey, host: ts.host, namespace: namespace, sourceTag: sourceTag})
assert.NoError(ts.T(), err)

if idxConn.apiKey != apiKey {
Expand All @@ -124,7 +124,7 @@ func (ts *IndexConnectionTests) TestNewIndexConnectionNamespace() {
apiKey := "test-api-key"
namespace := "test-namespace"
sourceTag := "test-source-tag"
idxConn, err := newIndexConnection(apiKey, ts.host, namespace, sourceTag, nil)
idxConn, err := newIndexConnection(newIndexParameters{apiKey: apiKey, host: ts.host, namespace: namespace, sourceTag: sourceTag})
assert.NoError(ts.T(), err)

if idxConn.apiKey != apiKey {
Expand All @@ -146,7 +146,7 @@ func (ts *IndexConnectionTests) TestNewIndexConnectionAdditionalMetadata() {
namespace := "test-namespace"
sourceTag := "test-source-tag"
additionalMetadata := map[string]string{"test-header": "test-value"}
idxConn, err := newIndexConnection(apiKey, ts.host, namespace, sourceTag, additionalMetadata)
idxConn, err := newIndexConnection(newIndexParameters{apiKey: apiKey, host: ts.host, namespace: namespace, sourceTag: sourceTag, additionalMetadata: additionalMetadata})
assert.NoError(ts.T(), err)

if idxConn.apiKey != apiKey {
Expand Down

0 comments on commit e5753fb

Please sign in to comment.