Skip to content

Commit

Permalink
Move user-agent header provider to its own package
Browse files Browse the repository at this point in the history
Also makes the provider generic to any header
  • Loading branch information
ssmith-pc committed Mar 26, 2024
1 parent 7d6ed31 commit 6211bd9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
20 changes: 20 additions & 0 deletions internal/provider/header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package provider

import (
"context"
"net/http"
)

type customHeader struct {
name string
value string
}

func NewHeaderProvider(name string, value string) *customHeader {
return &customHeader{name: name, value: value}
}

func (h *customHeader) Intercept(ctx context.Context, req *http.Request) error {
req.Header.Set(h.name, h.value)
return nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package useragent

import (
"fmt"
Expand Down
20 changes: 4 additions & 16 deletions pinecone/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"fmt"
"github.com/deepmap/oapi-codegen/v2/pkg/securityprovider"
"github.com/pinecone-io/go-pinecone/internal/gen/control"
"github.com/pinecone-io/go-pinecone/internal/util"
"github.com/pinecone-io/go-pinecone/internal/provider"
"github.com/pinecone-io/go-pinecone/internal/useragent"
"io"
"net/http"
)
Expand All @@ -17,23 +18,10 @@ type Client struct {
sourceTag string
}

type CustomHeader struct {
userAgent string
}

func NewUserAgentProvider(userAgent string) *CustomHeader {
return &CustomHeader{userAgent: userAgent}
}

func (s *CustomHeader) Intercept(ctx context.Context, req *http.Request) error {
req.Header.Set("User-Agent", s.userAgent)
return nil
}

type NewClientParams struct {
ApiKey string
// optional fields
SourceTag string
SourceTag string
}

func NewClient(in NewClientParams) (*Client, error) {
Expand All @@ -42,7 +30,7 @@ func NewClient(in NewClientParams) (*Client, error) {
return nil, err
}

userAgentProvider := NewUserAgentProvider(util.BuildUserAgent(in.SourceTag))
userAgentProvider := provider.NewHeaderProvider("User-Agent", useragent.BuildUserAgent(in.SourceTag))

client, err := control.NewClient("https://api.pinecone.io",
control.WithRequestEditorFn(apiKeyProvider.Intercept),
Expand Down
4 changes: 2 additions & 2 deletions pinecone/index_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/tls"
"fmt"
"github.com/pinecone-io/go-pinecone/internal/gen/data"
"github.com/pinecone-io/go-pinecone/internal/util"
"github.com/pinecone-io/go-pinecone/internal/useragent"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
Expand All @@ -27,7 +27,7 @@ func newIndexConnection(apiKey string, host string, namespace string, sourceTag
grpc.WithTransportCredentials(credentials.NewTLS(config)),
grpc.WithAuthority(target),
grpc.WithBlock(),
grpc.WithUserAgent(util.BuildUserAgentGRPC(sourceTag)),
grpc.WithUserAgent(useragent.BuildUserAgentGRPC(sourceTag)),
)

if err != nil {
Expand Down

0 comments on commit 6211bd9

Please sign in to comment.