From a6dbdba3f9de78a5b56c90ce6d1d17809b2964e8 Mon Sep 17 00:00:00 2001 From: xy Date: Wed, 12 Nov 2025 01:31:45 +0900 Subject: [PATCH] refactor: reorder clientConfig field in Client struct for improved clarity --- api_client.go | 4 ++-- client.go | 42 +++++++++++++++++++++--------------------- pages.go | 10 +++++----- replay_api_client.go | 4 ++-- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/api_client.go b/api_client.go index 40eec042..095770ae 100644 --- a/api_client.go +++ b/api_client.go @@ -464,14 +464,14 @@ func iterateResponseStream[R any](rs *responseStream[R], responseConverter func( // APIError contains an error response from the server. type APIError struct { - // Code is the HTTP response status code. - Code int `json:"code,omitempty"` // Message is the server response message. Message string `json:"message,omitempty"` // Status is the server response status. Status string `json:"status,omitempty"` // Details field provides more context to an error. Details []map[string]any `json:"details,omitempty"` + // Code is the HTTP response status code. + Code int `json:"code,omitempty"` } type responseWithError struct { diff --git a/client.go b/client.go index 8a52b788..48dfafa6 100644 --- a/client.go +++ b/client.go @@ -29,7 +29,6 @@ import ( // Client is the GenAI client. It provides access to the various GenAI services. type Client struct { - clientConfig ClientConfig // Models provides access to the Models service. Models *Models // Live provides access to the Live service. @@ -47,7 +46,8 @@ type Client struct { // Batches provides access to the Batch service. Batches *Batches // Tunings provides access to the Tunings service. - Tunings *Tunings + Tunings *Tunings + clientConfig ClientConfig } // Backend is the GenAI backend to use for the client. @@ -80,15 +80,27 @@ func (t Backend) String() string { // ClientConfig is the configuration for the GenAI client. type ClientConfig struct { + + // Optional HTTP options to override. + HTTPOptions HTTPOptions + + // Optional. Google credentials. If not specified, [Application Default Credentials] will be used. + // + // [Application Default Credentials]: https://developers.google.com/accounts/docs/application-default-credentials + Credentials *auth.Credentials + + // Optional HTTP client to use. If nil, a default client will be created. + // For Vertex AI, this client must handle authentication appropriately. + // Otherwise, call [UseDefaultCredentials] convenience method to add default credentials to the + // client. + HTTPClient *http.Client + + envVarProvider func() map[string]string // Optional. API Key for GenAI. Required for BackendGeminiAPI. // Can also be set via the GOOGLE_API_KEY or GEMINI_API_KEY environment variable. // Get a Gemini API key: https://ai.google.dev/gemini-api/docs/api-key APIKey string - // Optional. Backend for GenAI. See Backend constants. Defaults to BackendGeminiAPI unless explicitly set to BackendVertexAI, - // or the environment variable GOOGLE_GENAI_USE_VERTEXAI is set to "1" or "true". - Backend Backend - // Optional. GCP Project ID for Vertex AI. Required for BackendVertexAI. // Can also be set via the GOOGLE_CLOUD_PROJECT environment variable. // Find your Project ID: https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects @@ -99,21 +111,9 @@ type ClientConfig struct { // Generative AI locations: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations. Location string - // Optional. Google credentials. If not specified, [Application Default Credentials] will be used. - // - // [Application Default Credentials]: https://developers.google.com/accounts/docs/application-default-credentials - Credentials *auth.Credentials - - // Optional HTTP client to use. If nil, a default client will be created. - // For Vertex AI, this client must handle authentication appropriately. - // Otherwise, call [UseDefaultCredentials] convenience method to add default credentials to the - // client. - HTTPClient *http.Client - - // Optional HTTP options to override. - HTTPOptions HTTPOptions - - envVarProvider func() map[string]string + // Optional. Backend for GenAI. See Backend constants. Defaults to BackendGeminiAPI unless explicitly set to BackendVertexAI, + // or the environment variable GOOGLE_GENAI_USE_VERTEXAI is set to "1" or "true". + Backend Backend } func defaultEnvVarProvider() map[string]string { diff --git a/pages.go b/pages.go index c8b8f98c..63ac20e2 100644 --- a/pages.go +++ b/pages.go @@ -26,13 +26,13 @@ var ErrPageDone = errors.New("no more pages") // Page represents a page of results from a paginated API call. // It contains a slice of items and information about the next page. type Page[T any] struct { - Name string // The name of the resource. - Items []*T // The items in the current page. - NextPageToken string // The token to use to retrieve the next page of results. SDKHTTPResponse *HTTPResponse // The SDKHTTPResponse from the API call. - config map[string]any // The configuration used for the API call. - listFunc func(ctx context.Context, config map[string]any) ([]*T, string, *HTTPResponse, error) // The function used to retrieve the next page. + config map[string]any // The configuration used for the API call. + listFunc func(ctx context.Context, config map[string]any) ([]*T, string, *HTTPResponse, error) // The function used to retrieve the next page. + Name string // The name of the resource. + NextPageToken string // The token to use to retrieve the next page of results. + Items []*T // The items in the current page. } func newPage[T any](ctx context.Context, name string, config map[string]any, listFunc func(ctx context.Context, config map[string]any) ([]*T, string, *HTTPResponse, error)) (Page[T], error) { diff --git a/replay_api_client.go b/replay_api_client.go index 60fcb38f..7d908093 100644 --- a/replay_api_client.go +++ b/replay_api_client.go @@ -38,10 +38,10 @@ import ( // ReplayAPIClient is a client that reads responses from a replay session file. type replayAPIClient struct { ReplayFile *replayFile - ReplaysDirectory string - currentInteractionIndex int t *testing.T server *httptest.Server + ReplaysDirectory string + currentInteractionIndex int } // NewReplayAPIClient creates a new ReplayAPIClient from a replay session file.