in internal/enricher/enricher.go [69:95]
func (mc *MetadataCollector) IsGeminiAPIKeyValid(ctx context.Context) error {
if mc.GeminiAPIKey == "" {
return fmt.Errorf("gemini api key is not configured")
}
client, err := genai.NewClient(ctx, option.WithAPIKey(mc.GeminiAPIKey))
if err != nil {
return fmt.Errorf("failed to create Gemini client: %w", err)
}
defer client.Close()
modelIterator := client.ListModels(ctx)
// Iterate to trigger the API call and check for errors during iteration.
_, err = modelIterator.Next()
if err != nil {
// Check if the error is related to authentication (invalid API key)
if st, ok := status.FromError(err); ok {
if st.Code() == 16 || // Unauthenticated
st.Code() == 7 { // PermissionDenied (sometimes used for invalid keys)
return fmt.Errorf("invalid Gemini API key: %w", err)
}
}
return fmt.Errorf("failed to list Gemini models, potentially due to API key issue or other error: %w", err)
}
return nil
}