Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat console call #705

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go/qianfan/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions go/qianfan/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func TestAuthWhenUsing(t *testing.T) {
// 如果只设置了部分鉴权信息,则报错
GetConfig().AK = ""
GetConfig().AccessKey = ""
GetConfig().AccessToken = ""
_, err = chat.Do(
context.Background(),
&ChatCompletionRequest{
Expand Down
1 change: 1 addition & 0 deletions go/qianfan/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
46 changes: 46 additions & 0 deletions go/qianfan/console_action.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions go/qianfan/console_action_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions go/qianfan/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ var logger = &logrus.Logger{
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}

func SetLogLevel(level logrus.Level) {
logger.Level = level
}
25 changes: 21 additions & 4 deletions go/qianfan/requestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{}
}

Expand Down
2 changes: 1 addition & 1 deletion go/qianfan/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
package qianfan

// SDK 版本
const Version = "v0.0.9"
const Version = "v0.0.10"
const versionIndicator = "qianfan_go_sdk_" + Version
Loading