Skip to content

Commit

Permalink
genai: do not fail with custom HTTP client (#81)
Browse files Browse the repository at this point in the history
If there is no WithAPIKey option, we were returning an error.
But it's possible that the user provided an alternative HTTP
client that does auth, so don't return an error in that case.

Fixes #80.
  • Loading branch information
jba authored Apr 9, 2024
1 parent 32bc54c commit 78aace2
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions genai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,22 @@ Import the option package as "google.golang.org/api/option".`)
return &Client{c: c, mc: mc}, nil
}

// hasAPIKey reports whether one of the options was created with
// WithAPIKey.
// There is no good way to do that, because the type of the option
// is unexported, and the struct that it populates is in an internal package.
// hasAPIKey reports whether the options imply that there is an API key.
// That is the case if either the WithAPIKey or WithHTTPClient options is present.
// (If the latter is present, we assume it handles auth).
//
// There is no good way to make these checks, because the types of the options
// are unexported, and the struct that they populates is in an internal package.
func hasAPIKey(opts []option.ClientOption) bool {
for _, opt := range opts {
v := reflect.ValueOf(opt)
if v.Type().String() == "option.withAPIKey" {
ts := v.Type().String()
if ts == "option.withAPIKey" {
return v.String() != ""
}
if ts == "option.withHTTPClient" {
return true
}
}
return false
}
Expand Down

0 comments on commit 78aace2

Please sign in to comment.