mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-01-11 00:18:17 -05:00
feat(source/http): Add User Agent to calls (#1102)
This change adds a User Agent for all http calls as part of source. This will append the user agent being passed as part of prebuilt tools context to default headers.
This commit is contained in:
committed by
GitHub
parent
6d691d582f
commit
6f55b78e96
@@ -93,6 +93,18 @@ func (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.So
|
||||
return nil, fmt.Errorf("failed to parse BaseUrl %v", err)
|
||||
}
|
||||
|
||||
ua, err := util.UserAgentFromContext(ctx)
|
||||
if err != nil {
|
||||
fmt.Printf("Error in User Agent retrieval: %s", err)
|
||||
}
|
||||
if r.DefaultHeaders == nil {
|
||||
r.DefaultHeaders = make(map[string]string)
|
||||
}
|
||||
if existingUA, ok := r.DefaultHeaders["User-Agent"]; ok {
|
||||
ua = ua + " " + existingUA
|
||||
}
|
||||
r.DefaultHeaders["User-Agent"] = ua
|
||||
|
||||
s := &Source{
|
||||
Name: r.Name,
|
||||
Kind: SourceKind,
|
||||
|
||||
@@ -31,7 +31,6 @@ import (
|
||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||
httpsrc "github.com/googleapis/genai-toolbox/internal/sources/http"
|
||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||
"github.com/googleapis/genai-toolbox/internal/util"
|
||||
)
|
||||
|
||||
const kind string = "http"
|
||||
@@ -314,10 +313,6 @@ func (t Tool) Invoke(ctx context.Context, params tools.ParamValues) (any, error)
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
if ua, err := util.UserAgentFromContext(ctx); err == nil {
|
||||
req.Header.Set("User-Agent", ua)
|
||||
}
|
||||
|
||||
// Make request and fetch response
|
||||
resp, err := t.Client.Do(req)
|
||||
if err != nil {
|
||||
|
||||
@@ -30,7 +30,6 @@ import (
|
||||
"github.com/googleapis/genai-toolbox/internal/sources"
|
||||
httpsrc "github.com/googleapis/genai-toolbox/internal/sources/http"
|
||||
"github.com/googleapis/genai-toolbox/internal/tools"
|
||||
"github.com/googleapis/genai-toolbox/internal/util"
|
||||
)
|
||||
|
||||
const kind string = "alloydb-wait-for-operation"
|
||||
@@ -256,10 +255,6 @@ func (t *Tool) Invoke(ctx context.Context, params tools.ParamValues) (any, error
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
if ua, err := util.UserAgentFromContext(ctx); err == nil {
|
||||
req.Header.Set("User-Agent", ua)
|
||||
}
|
||||
|
||||
resp, err := t.Client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Printf("error making HTTP request during polling: %s, retrying in %v\n", err, delay)
|
||||
|
||||
Reference in New Issue
Block a user