-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
45 additions
and
34 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |