Skip to content

Commit

Permalink
genai: check for non-empty API key (#52)
Browse files Browse the repository at this point in the history
Fixes #51.
  • Loading branch information
jba authored Feb 22, 2024
1 parent 5b241fc commit 7285b13
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions genai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error
Visit https://ai.google.dev to get one, put it in an environment variable like GEMINI_API_KEY,
then pass it as an option:
genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
(If you're doing that already, then maybe the environment variable is empty or unset.)
Import the option package as "google.golang.org/api/option".`)
}
c, err := gl.NewGenerativeRESTClient(ctx, opts...)
Expand All @@ -75,9 +76,9 @@ Import the option package as "google.golang.org/api/option".`)
// is unexported, and the struct that it populates is in an internal package.
func hasAPIKey(opts []option.ClientOption) bool {
for _, opt := range opts {
t := reflect.TypeOf(opt)
if t.String() == "option.withAPIKey" {
return true
v := reflect.ValueOf(opt)
if v.Type().String() == "option.withAPIKey" {
return v.String() != ""
}
}
return false
Expand Down
4 changes: 4 additions & 0 deletions genai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,8 @@ func TestNoAPIKey(t *testing.T) {
if err == nil {
t.Fatal("got nil, want error")
}
_, err = NewClient(context.Background(), option.WithAPIKey(""))
if err == nil {
t.Fatal("got nil, want error")
}
}

0 comments on commit 7285b13

Please sign in to comment.