Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

genai: do not fail with custom HTTP client #81

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading