From df7b50a63a29f9a9883e69e09d22f6eae9b11b04 Mon Sep 17 00:00:00 2001 From: dtapps Date: Tue, 2 Jan 2024 10:06:15 +0800 Subject: [PATCH] - update version --- README.md | 4 ++-- config.go | 16 ++++------------ const.go | 3 ++- http.go | 17 +++++++++++++++-- version.go | 2 +- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 0ca0af1..49941b4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Golang -📦 Golang Request +📦 Golang 请求 [comment]: <> (go) [![godoc](https://pkg.go.dev/badge/go.dtapp.net/gorequest?status.svg)](https://pkg.go.dev/go.dtapp.net/gorequest) @@ -13,7 +13,7 @@ #### 安装 ```shell -go get -v -u go.dtapp.net/gorequest@v1.0.43 +go get -v -u go.dtapp.net/gorequest@v1.0.44 ``` #### 使用 diff --git a/config.go b/config.go index 0d510f2..ab6b5d4 100644 --- a/config.go +++ b/config.go @@ -1,8 +1,8 @@ package gorequest import ( + "fmt" "github.com/shirou/gopsutil/host" - "log" "runtime" ) @@ -13,26 +13,18 @@ type systemResult struct { // 获取系统信息 func getSystem() (result systemResult) { - - hInfo, err := host.Info() - if err != nil { - log.Printf("getSystem.host.Info:%s\n", err) - } - + hInfo, _ := host.Info() result.SystemOs = hInfo.OS result.SystemKernel = hInfo.KernelArch - return result } // 设置配置信息 func (app *App) setConfig() { - info := getSystem() - app.config.systemOs = info.SystemOs app.config.systemKernel = info.SystemKernel - app.config.goVersion = runtime.Version() - + app.config.sdkVersion = Version + app.config.sdkUserAgent = fmt.Sprintf(userAgentFormat1, app.config.systemOs, app.config.systemKernel, app.config.goVersion) } diff --git a/const.go b/const.go index 9adce6a..23ff35f 100644 --- a/const.go +++ b/const.go @@ -1,7 +1,8 @@ package gorequest const ( - userAgentFormat = "%s/%s/%s/%s" + userAgentFormat1 = "%s/%s/%s" + userAgentFormat2 = "%s/%s/%s/%s" ) // 定义请求类型 diff --git a/http.go b/http.go index d87c127..4ef5424 100644 --- a/http.go +++ b/http.go @@ -56,6 +56,8 @@ type App struct { systemOs string // 系统类型 systemKernel string // 系统内核 goVersion string // go版本 + sdkVersion string // sdk版本 + sdkUserAgent string // sdk用户代理 } } @@ -107,7 +109,7 @@ func (app *App) SetAuthToken(token string) { app.httpHeader.Set("Authorization", fmt.Sprintf("Bearer %s", token)) } -// SetUserAgent 设置用户代理,空字符串就随机设置 +// SetUserAgent 设置用户代理,传空字符串就随机设置 func (app *App) SetUserAgent(ua string) { if ua == "" { ua = GetRandomUserAgent() @@ -115,6 +117,15 @@ func (app *App) SetUserAgent(ua string) { app.httpHeader.Set("User-Agent", ua) } +// SetPassSdkVersion 传入SDK版本 +func (app *App) SetPassSdkVersion(sdkVersion string) { + if sdkVersion == "" { + app.httpHeader.Set("Sdk-User-Agent", app.config.sdkUserAgent) + } else { + app.httpHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat2, app.config.systemOs, app.config.systemKernel, app.config.goVersion, sdkVersion)) + } +} + // SetContentTypeJson 设置JSON格式 func (app *App) SetContentTypeJson() { app.httpContentType = httpParamsModeJson @@ -230,7 +241,9 @@ func request(app *App, ctx context.Context) (httpResponse Response, err error) { } // SDK版本 - //httpResponse.RequestHeader.Set("Sdk-User-Agent", fmt.Sprintf(userAgentFormat, app.config.systemOs, app.config.systemKernel, app.config.goVersion)) + if app.config.sdkUserAgent != "" { + httpResponse.RequestHeader.Set("Sdk-User-Agent", app.config.sdkUserAgent) + } // 请求类型 if app.httpContentType == "" { diff --git a/version.go b/version.go index 6a584a3..73a2695 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package gorequest -const Version = "1.0.43" +const Version = "1.0.44"