Skip to content

feat: add extras request provider to the HTTP options #349

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

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ func buildRequest(ctx context.Context, ac *apiClient, path string, body map[stri
if err != nil {
return nil, err
}
if httpOptions.ExtrasRequestProvider != nil {
log.Printf("Warning: Usage of ExtrasRequestProvider is strongly discouraged. No forward compatibility is guaranteed.")
body = httpOptions.ExtrasRequestProvider(body)
}

b := new(bytes.Buffer)
if len(body) > 0 {
if err := json.NewEncoder(b).Encode(body); err != nil {
Expand Down
35 changes: 35 additions & 0 deletions api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,41 @@ func TestBuildRequest(t *testing.T) {
wantErr: true,
expectedError: "buildRequest: error encoding body",
},
{
name: "With ExtrasRequestProvider",
clientConfig: &ClientConfig{
APIKey: "test-api-key",
Backend: BackendGeminiAPI,
HTTPClient: &http.Client{},
},
path: "models/test-model:generateContent",
body: map[string]any{"original_key": "original_value"},
method: "POST",
httpOptions: &HTTPOptions{
BaseURL: "https://generativelanguage.googleapis.com",
APIVersion: "v1beta",
ExtrasRequestProvider: func(body map[string]any) map[string]any {
body["extra_key"] = "extra_value"
return body
},
},
want: &http.Request{
Method: "POST",
URL: &url.URL{
Scheme: "https",
Host: "generativelanguage.googleapis.com",
Path: "/v1beta/models/test-model:generateContent",
},
Header: http.Header{
"Content-Type": []string{"application/json"},
"X-Goog-Api-Key": []string{"test-api-key"},
"User-Agent": []string{fmt.Sprintf("google-genai-sdk/%s gl-go/%s", version, runtime.Version())},
"X-Goog-Api-Client": []string{fmt.Sprintf("google-genai-sdk/%s gl-go/%s", version, runtime.Version())},
},
Body: io.NopCloser(strings.NewReader("{\"extra_key\":\"extra_value\",\"original_key\":\"original_value\"}\n")),
},
wantErr: false,
},
}

for _, tt := range tests {
Expand Down
7 changes: 7 additions & 0 deletions types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading