-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser_agent.go
49 lines (42 loc) · 1.18 KB
/
user_agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package coze
import (
"encoding/json"
"net/http"
"os"
"runtime"
"strings"
)
const version = "0.1.0"
var (
userAgentSDK = "cozego"
userAgentLang = "go"
userAgentLangVersion = strings.TrimPrefix(runtime.Version(), "go")
userAgentOsName = runtime.GOOS
userAgentOsVersion = os.Getenv("OSVERSION")
userAgent = userAgentSDK + "/" + version + " " + userAgentLang + "/" + userAgentLangVersion + " " + userAgentOsName + "/" + userAgentOsVersion
clientUserAgent string
)
func setUserAgent(req *http.Request) {
req.Header.Set("User-Agent", userAgent)
req.Header.Set("X-Coze-Client-User-Agent", clientUserAgent)
}
func init() {
clientUserAgent = getCozeClientUserAgent()
}
type userAgentInfo struct {
Version string `json:"version"`
Lang string `json:"lang"`
LangVersion string `json:"lang_version"`
OsName string `json:"os_name"`
OsVersion string `json:"os_version"`
}
func getCozeClientUserAgent() string {
data, _ := json.Marshal(userAgentInfo{
Version: version,
Lang: userAgentSDK,
LangVersion: userAgentLangVersion,
OsName: userAgentOsName,
OsVersion: userAgentOsVersion,
})
return string(data)
}