Skip to content

Commit

Permalink
chore: fix setting user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Nov 5, 2024
1 parent 012f47e commit b52892f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
34 changes: 0 additions & 34 deletions internal/hooks/registration.go

This file was deleted.

21 changes: 21 additions & 0 deletions internal/hooks/user_agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hooks

import (
"net/http"

"github.com/Kong/sdk-konnect-go/pkg/metadata"
)

type UserAgentPreRequestHook struct{}

var _ beforeRequestHook = (*UserAgentPreRequestHook)(nil)

func (i *UserAgentPreRequestHook) BeforeRequest(hookCtx BeforeRequestContext, req *http.Request) (*http.Request, error) {
ua := metadata.GetUserAgent()
if ua != "" {
req.Header.Set("User-Agent", ua)
return req, nil
}

return req, nil
}
24 changes: 24 additions & 0 deletions pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package metadata

import "sync"

var (
l sync.RWMutex
userAgent string
)

// SetUserAgent sets the user agent to be used by the SDK.
// This is then used by UserAgentPreRequestHook in internal/hooks/user_agent.go
func SetUserAgent(ua string) {
l.Lock()
defer l.Unlock()
userAgent = ua
}

// GetUserAgent returns the user agent to be used by the SDK.
// This is then used by UserAgentPreRequestHook in internal/hooks/user_agent.go
func GetUserAgent() string {
l.RLock()
defer l.RUnlock()
return userAgent
}

0 comments on commit b52892f

Please sign in to comment.