Skip to content

Commit

Permalink
wanx support international base url
Browse files Browse the repository at this point in the history
  • Loading branch information
devinyf committed Jul 30, 2024
1 parent fc007fd commit 32f3a11
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion tongyiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (q *TongyiClient) CreateImageGeneration(ctx context.Context, payload *wanx.
}
payload.Model = q.Model
}
return wanx.CreateImageGeneration(ctx, payload, q.httpCli, q.token)
return wanx.CreateImageGeneration(ctx, q.baseUrl, payload, q.httpCli, q.token)
}

// voice file to text.
Expand Down
13 changes: 6 additions & 7 deletions wanx/wanx_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package wanx
import "fmt"

const (
DashScopeBaseURL = "https://dashscope.aliyuncs.com"
ImageSynthesisURI = "/api/v1/services/aigc/text2image/image-synthesis"
TaskURI = "/api/v1/tasks/%s"
ImageSynthesisURI = "/v1/services/aigc/text2image/image-synthesis"
TaskURI = "/v1/tasks/%s"
)

type ModelWanx = string
Expand All @@ -16,10 +15,10 @@ const (
WanxBgGenV2 ModelWanx = "wanx-background-generation-v2"
)

func ImageSynthesisURL() string {
return DashScopeBaseURL + ImageSynthesisURI
func ImageSynthesisURL(baseURL string) string {
return baseURL + ImageSynthesisURI
}

func TaskURL(taskID string) string {
return DashScopeBaseURL + fmt.Sprintf(TaskURI, taskID)
func TaskURL(baseURL, taskID string) string {
return baseURL + fmt.Sprintf(TaskURI, taskID)
}
18 changes: 9 additions & 9 deletions wanx/wanxcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ var (
)

//nolint:lll
func CreateImageGeneration(ctx context.Context, payload *ImageSynthesisRequest, httpcli httpclient.IHttpClient, token string) ([]*ImgBlob, error) {
func CreateImageGeneration(ctx context.Context, baseURL string, payload *ImageSynthesisRequest, httpcli httpclient.IHttpClient, token string) ([]*ImgBlob, error) {
header := map[string]string{
"Content-Type": "application/json",
}

tokenOpt := httpclient.WithTokenHeaderOption(token)
headerOpt := httpclient.WithHeader(header)
resp, err := SyncCall(ctx, payload, httpcli, tokenOpt, headerOpt)
resp, err := SyncCall(ctx, baseURL, payload, httpcli, tokenOpt, headerOpt)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -54,8 +54,8 @@ func CreateImageGeneration(ctx context.Context, payload *ImageSynthesisRequest,
// tongyi-wanx-api only support AsyncCall, so we need to warp it to be Sync.
//
//nolint:lll
func SyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*Output, error) {
rsp, err := AsyncCall(ctx, req, httpcli, options...)
func SyncCall(ctx context.Context, baseURL string, req *ImageSynthesisRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*Output, error) {
rsp, err := AsyncCall(ctx, baseURL, req, httpcli, options...)
if err != nil {
return nil, err
}
Expand All @@ -78,7 +78,7 @@ func SyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclien
time.Sleep(time.Duration(delayDurationToCheckStatus) * time.Millisecond)

// log.Println("TaskStatus: ", currentTaskStatus)
taskResp, err = CheckTaskStatus(ctx, &taskReq, httpcli, options...)
taskResp, err = CheckTaskStatus(ctx, baseURL, &taskReq, httpcli, options...)
if err != nil {
return nil, err
}
Expand All @@ -100,7 +100,7 @@ func SyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclien
// calling tongyi-wanx-api to get image-generation async task id.
//
//nolint:lll
func AsyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*ImageResponse, error) {
func AsyncCall(ctx context.Context, baseURL string, req *ImageSynthesisRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*ImageResponse, error) {
header := map[string]string{"X-DashScope-Async": "enable"}
headerOpt := httpclient.WithHeader(header)
options = append(options, headerOpt)
Expand All @@ -110,7 +110,7 @@ func AsyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclie
}

resp := ImageResponse{}
err := httpcli.Post(ctx, ImageSynthesisURL(), req, &resp, options...)
err := httpcli.Post(ctx, ImageSynthesisURL(baseURL), req, &resp, options...)
if err != nil {
return nil, err
}
Expand All @@ -119,9 +119,9 @@ func AsyncCall(ctx context.Context, req *ImageSynthesisRequest, httpcli httpclie
}

//nolint:lll
func CheckTaskStatus(ctx context.Context, req *TaskRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*TaskResponse, error) {
func CheckTaskStatus(ctx context.Context, baseURL string, req *TaskRequest, httpcli httpclient.IHttpClient, options ...httpclient.HTTPOption) (*TaskResponse, error) {
resp := TaskResponse{}
err := httpcli.Get(ctx, TaskURL(req.TaskID), nil, &resp, options...)
err := httpcli.Get(ctx, TaskURL(baseURL, req.TaskID), nil, &resp, options...)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 32f3a11

Please sign in to comment.