Skip to content

Commit

Permalink
refactor: cache user agent (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov authored May 17, 2024
1 parent c811fef commit fda38bb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
16 changes: 8 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ func NewClient(opts ...Option) (Client, error) {
// Removes trailing / so it is easier later Host + URL
d.Host = strings.TrimSuffix(d.Host, "/")

// Formats the user agent
d.UserAgent = fmt.Sprintf(
"go-client-codegen/%s %s",
strings.TrimLeft(Version(), "v"),
strings.TrimSpace(d.UserAgent),
)

return newClient(d), nil
}

Expand Down Expand Up @@ -133,14 +140,7 @@ func (d *aivenClient) do(ctx context.Context, method, path string, v any) (*http
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set(
"User-Agent",
strings.TrimSpace(fmt.Sprintf(
"go-client-codegen/%s %s",
strings.TrimLeft(Version(), "v"),
d.UserAgent,
)),
)
req.Header.Set("User-Agent", d.UserAgent)
req.Header.Set("Authorization", "aivenv1 "+d.Token)

// TODO: BAD hack to get around pagination in most cases
Expand Down
3 changes: 2 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestServiceCreate(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "foo", expectIn.ServiceName)
assert.Equal(t, "kafka", expectIn.ServiceType)
assert.Regexp(t, `go-client-codegen/[0-9\.]+ unit-test`, r.Header["User-Agent"])

// Creates response
w.Header().Set("Content-Type", "application/json")
Expand All @@ -61,7 +62,7 @@ func TestServiceCreate(t *testing.T) {
defer server.Close()

// Points a new client to the server url
c, err := NewClient(TokenOpt("token"), HostOpt(server.URL))
c, err := NewClient(TokenOpt("token"), HostOpt(server.URL), UserAgentOpt("unit-test"))
require.NotNil(t, c)
require.NoError(t, err)

Expand Down

0 comments on commit fda38bb

Please sign in to comment.