Skip to content

searcher: set custom user agent in search client #2203

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
Jul 29, 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
searcher: set custom user agent in search client
The format is "Devbox/$VERSION ($GOOS; $GOARCH)".
  • Loading branch information
gcurtis committed Jul 26, 2024
commit 95a792470063083048403b982911096c5247d7c0
6 changes: 6 additions & 0 deletions internal/searcher/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"io"
"net/http"
"net/url"
"runtime"

"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/build"
"go.jetpack.io/devbox/internal/envir"
"go.jetpack.io/devbox/internal/redact"
)
Expand Down Expand Up @@ -83,11 +85,15 @@ func (c *client) ResolveV2(ctx context.Context, name, version string) (*ResolveR
return execGet[ResolveResponse](ctx, searchURL)
}

var userAgent = fmt.Sprintf("Devbox/%s (%s; %s)", build.Version, runtime.GOOS, runtime.GOARCH)

func execGet[T any](ctx context.Context, url string) (*T, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, redact.Errorf("GET %s: %w", redact.Safe(url), redact.Safe(err))
}
req.Header.Set("User-Agent", userAgent)

response, err := http.DefaultClient.Do(req)
if err != nil {
return nil, redact.Errorf("GET %s: %w", redact.Safe(url), redact.Safe(err))
Expand Down