diff --git a/go/qianfan/auth.go b/go/qianfan/auth.go index a3aee043..43bfe687 100644 --- a/go/qianfan/auth.go +++ b/go/qianfan/auth.go @@ -146,5 +146,6 @@ func (m *AuthManager) GetAccessTokenWithRefresh(ctx context.Context, ak, sk stri token: resp.AccessToken, lastUpateTime: time.Now(), } + GetConfig().AccessToken = resp.AccessToken return resp.AccessToken, nil } diff --git a/go/qianfan/auth_test.go b/go/qianfan/auth_test.go index 40ce964f..ce86b1f6 100644 --- a/go/qianfan/auth_test.go +++ b/go/qianfan/auth_test.go @@ -128,6 +128,7 @@ func TestAuthWhenUsing(t *testing.T) { // 如果只设置了部分鉴权信息,则报错 GetConfig().AK = "" GetConfig().AccessKey = "" + GetConfig().AccessToken = "" _, err = chat.Do( context.Background(), &ChatCompletionRequest{ diff --git a/go/qianfan/config.go b/go/qianfan/config.go index f589893c..6d667112 100644 --- a/go/qianfan/config.go +++ b/go/qianfan/config.go @@ -42,6 +42,7 @@ type Config struct { SK string `mapstructure:"QIANFAN_SK"` AccessKey string `mapstructure:"QIANFAN_ACCESS_KEY"` SecretKey string `mapstructure:"QIANFAN_SECRET_KEY"` + AccessToken string `mapstructure:"QIANFAN_ACCESS_TOKEN"` BaseURL string `mapstructure:"QIANFAN_BASE_URL"` IAMSignExpirationSeconds int `mapstructure:"QIANFAN_IAM_SIGN_EXPIRATION_SEC"` ConsoleBaseURL string `mapstructure:"QIANFAN_CONSOLE_BASE_URL"` diff --git a/go/qianfan/console_action.go b/go/qianfan/console_action.go new file mode 100644 index 00000000..24f67b45 --- /dev/null +++ b/go/qianfan/console_action.go @@ -0,0 +1,46 @@ +package qianfan + +import ( + "context" + "fmt" +) + +type ConsoleAction struct { + consoleBase +} + +func NewConsoleAction(optionList ...Option) *ConsoleAction { + options := makeOptions(optionList...) + return &ConsoleAction{ + consoleBase: consoleBase{ + Requestor: newRequestor(options), + }, + } +} + +func (ca *ConsoleAction) baseActionUrl(route, action string) string { + if action == "" { + return route + } else { + return fmt.Sprintf("%v?Action=%v", route, action) + } +} + +func (ca *ConsoleAction) Call(ctx context.Context, route string, action string, params map[string]interface{}) (*ConsoleResponse, error) { + reqBody := BaseRequestBody{ + Extra: params, + } + req, err := newConsoleRequest("POST", ca.baseActionUrl(route, action), &reqBody) + if err != nil { + logger.Error("new console req error", err) + return nil, err + } + logger.Trace("console req", req) + resp := &ConsoleResponse{} + err = ca.requestResource(ctx, req, resp) + if err != nil { + logger.Error("request resource failed", err) + return nil, err + } + return resp, nil +} diff --git a/go/qianfan/console_action_test.go b/go/qianfan/console_action_test.go new file mode 100644 index 00000000..0c453581 --- /dev/null +++ b/go/qianfan/console_action_test.go @@ -0,0 +1,19 @@ +package qianfan + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConsoleAction(t *testing.T) { + ca := NewConsoleAction() + _, err := ca.Call(context.TODO(), "/wenxinworkshop/service/list", "", map[string]interface{}{}) + assert.NoError(t, err) + + // return + ca1 := NewConsoleAction() + _, err1 := ca1.Call(context.TODO(), "/v2/service", "DescribeServices", map[string]interface{}{}) + assert.NoError(t, err1) +} diff --git a/go/qianfan/logger.go b/go/qianfan/logger.go index ea215551..5ade9043 100644 --- a/go/qianfan/logger.go +++ b/go/qianfan/logger.go @@ -26,3 +26,7 @@ var logger = &logrus.Logger{ Hooks: make(logrus.LevelHooks), Level: logrus.InfoLevel, } + +func SetLogLevel(level logrus.Level) { + logger.Level = level +} diff --git a/go/qianfan/requestor.go b/go/qianfan/requestor.go index 2c6acd4f..f07f50fe 100644 --- a/go/qianfan/requestor.go +++ b/go/qianfan/requestor.go @@ -125,13 +125,27 @@ func newRequest(requestType string, method string, url string, body RequestBody) } // 创建一个 Request,body 是一个 map -func newRequestFromMap(requestType string, method string, url string, body map[string]interface{}) (*QfRequest, error) { +func newRequestFromMap(requestType string, method string, urlStr string, body map[string]interface{}) (*QfRequest, error) { + u, err := url.Parse(urlStr) + if err != nil { + return nil, err + } + + // 提取params + params := u.Query() + paramsMap := make(map[string]string) + for key, values := range params { + if len(values) > 0 { + paramsMap[key] = values[0] // 只取第一个值 + } + } + return &QfRequest{ Type: requestType, Method: method, - URL: url, + URL: u.Path, Body: body, - Params: map[string]string{}, + Params: paramsMap, Headers: map[string]string{}, }, nil } @@ -185,8 +199,11 @@ func (r *Requestor) addAuthInfo(ctx context.Context, request *QfRequest) error { return r.addAccessToken(ctx, request) } else if GetConfig().AccessKey != "" && GetConfig().SecretKey != "" { return r.sign(request) + } else if GetConfig().AccessToken != "" { + request.Params["access_token"] = GetConfig().AccessToken + return nil } - logger.Error("no enough credential found. Please check whether (ak, sk) or (access_key, secret_key) is set in config") + logger.Error("no enough credential found. Please check whether (ak, sk) or (access_key, secret_key) or (access_token) is set in config") return &CredentialNotFoundError{} } diff --git a/go/qianfan/version.go b/go/qianfan/version.go index 2952902b..180feeed 100644 --- a/go/qianfan/version.go +++ b/go/qianfan/version.go @@ -26,5 +26,5 @@ package qianfan // SDK 版本 -const Version = "v0.0.9" +const Version = "v0.0.10" const versionIndicator = "qianfan_go_sdk_" + Version