Skip to content

Commit

Permalink
tools: serpapi api key option (#773)
Browse files Browse the repository at this point in the history
feat: serpapi api key options
  • Loading branch information
AugustDev authored Apr 18, 2024
1 parent fcbc717 commit ab8bf7a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 15 additions & 0 deletions tools/serpapi/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package serpapi

type options struct {
apiKey string
}

type Option func(*options)

// WithAPIKey passes the Serpapi API token to the client. If not set, the token
// is read from the SERPAPI_API_KEY environment variable.
func WithAPIKey(apiKey string) Option {
return func(opts *options) {
opts.apiKey = apiKey
}
}
15 changes: 11 additions & 4 deletions tools/serpapi/serpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ type Tool struct {
var _ tools.Tool = Tool{}

// New creates a new serpapi tool to search on internet.
func New() (*Tool, error) {
apiKey := os.Getenv("SERPAPI_API_KEY")
if apiKey == "" {
func New(opts ...Option) (*Tool, error) {
options := &options{
apiKey: os.Getenv("SERPAPI_API_KEY"),
}

for _, opt := range opts {
opt(options)
}

if options.apiKey == "" {
return nil, ErrMissingToken
}

return &Tool{
client: internal.New(apiKey),
client: internal.New(options.apiKey),
}, nil
}

Expand Down

0 comments on commit ab8bf7a

Please sign in to comment.