Skip to content

Commit 5a25197

Browse files
committed
feat: add support for custom user agent for SDK wrappers
1 parent ee6e54f commit 5a25197

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

phase/network/network.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,37 @@ func ConstructHTTPHeaders(appToken string) http.Header {
2424
return headers
2525
}
2626

27+
var customUserAgent string
28+
29+
func SetUserAgent(ua string) {
30+
customUserAgent = ua
31+
}
32+
2733
func GetUserAgent() string {
28-
details := []string{}
34+
if customUserAgent != "" {
35+
return customUserAgent
36+
}
2937

30-
cliVersion := "phase-golang-sdk/" + misc.Version
31-
details = append(details, cliVersion)
38+
details := []string{}
3239

33-
osType := runtime.GOOS
34-
architecture := runtime.GOARCH
35-
details = append(details, fmt.Sprintf("%s %s", osType, architecture))
40+
cliVersion := "phase-golang-sdk/" + misc.Version
41+
details = append(details, cliVersion)
3642

37-
currentUser, err := user.Current()
38-
if err == nil {
39-
hostname, err := os.Hostname()
40-
if err == nil {
41-
userHostString := fmt.Sprintf("%s@%s", currentUser.Username, hostname)
42-
details = append(details, userHostString)
43-
}
44-
}
43+
osType := runtime.GOOS
44+
architecture := runtime.GOARCH
45+
details = append(details, fmt.Sprintf("%s %s", osType, architecture))
46+
47+
currentUser, err := user.Current()
48+
if err == nil {
49+
hostname, err := os.Hostname()
50+
if err == nil {
51+
userHostString := fmt.Sprintf("%s@%s", currentUser.Username, hostname)
52+
details = append(details, userHostString)
53+
}
54+
}
4555

4656
// Return only the concatenated string without "User-Agent:" prefix
47-
return strings.Join(details, " ")
57+
return strings.Join(details, " ")
4858
}
4959

5060
func createHTTPClient() *http.Client {

0 commit comments

Comments
 (0)