Skip to content

Commit

Permalink
Add auth support for MarqoCloud (#12)
Browse files Browse the repository at this point in the history
Allow user to provide an optional API key on client creation.
This key is passed as a header on every Marqo api call.

Docs (see MarqoCloud section):
https://docs.marqo.ai/latest/reference/api/search/search/#response

Co-authored-by: Madeleine North <[email protected]>
  • Loading branch information
msnorth and Madeleine North authored Sep 4, 2024
1 parent ce5bcf0 commit a9c42a4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ func WithLogger(logger *slog.Logger) func(*Client) {
}
}

// WithMarqoCloudAuth sets the API key for authentication if you are using MarqoCloud
func WithMarqoCloudAuth(apiKey string) func(*Client) {
return func(c *Client) {
c.apiKey = apiKey
}
}

// Client is the client for the Marqo server
type Client struct {
url string
logger *slog.Logger
reqClient *req.Client
apiKey string // Field to hold the API key for use with MarqoCloud
}

// NewClient creates a new client for the Marqo server.
Expand Down Expand Up @@ -74,6 +82,11 @@ func NewClient(url string, opt ...Options) (*Client, error) {
if client.reqClient == nil {
client.reqClient = req.NewClient()
client.reqClient.BaseURL = url

// Add the API key header if provided
if client.apiKey != "" {
client.reqClient.SetCommonHeader("x-api-key", client.apiKey)
}
}

// set default logger
Expand All @@ -84,4 +97,4 @@ func NewClient(url string, opt ...Options) (*Client, error) {
}))
}
return client, nil
}
}

0 comments on commit a9c42a4

Please sign in to comment.