From c38fa8fd23d73d3d2a44a818bfe9d602e162d987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=A3=E5=86=9B?= Date: Thu, 5 Sep 2024 11:54:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E6=9E=84=E9=80=A0cu?= =?UTF-8?q?rl=E5=91=BD=E4=BB=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/appbuilder/config.go | 22 ++++++++++++++-------- go/appbuilder/dataset.go | 30 +++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/go/appbuilder/config.go b/go/appbuilder/config.go index de77291f..59fb72d6 100644 --- a/go/appbuilder/config.go +++ b/go/appbuilder/config.go @@ -180,26 +180,32 @@ func NopCloser(r io.Reader) io.ReadCloser { } func (t *SDKConfig) BuildCurlCommand(req *http.Request) { - curlCmd := fmt.Sprintf("curl -X %s -L '%v' \\\n", req.Method, req.URL.String()) + var curlCmd strings.Builder + curlCmd.WriteString(fmt.Sprintf("curl -X %s -L '%v' \\\n", req.Method, req.URL.String())) for k, v := range req.Header { header := fmt.Sprintf("-H '%v: %v' \\\n", k, v[0]) - curlCmd = fmt.Sprintf("%v %v", curlCmd, header) + curlCmd.WriteString(header) } if req.Method == "POST" { - bodyBytes, err := ioutil.ReadAll(req.Body) + bodyBytes, err := io.ReadAll(req.Body) if err != nil { t.logger.Println("Failed to read request body:", err) return } - req.Body.Close() - req.Body = io.NopCloser(strings.NewReader(string(bodyBytes))) + + // 重置 req.Body 以便请求可以再次使用 + req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) body := fmt.Sprintf("-d '%v'", string(bodyBytes)) - curlCmd = fmt.Sprintf("%v %v", curlCmd, body) + curlCmd.WriteString(body) } else if req.Method == "GET" || req.Method == "DELETE" { - curlCmd = strings.TrimSuffix(curlCmd, " \\\n") + // 去掉末尾多余的字符 + cmdStr := curlCmd.String() + curlCmd.Reset() + curlCmd.WriteString(strings.TrimSuffix(cmdStr, " \\\n")) } - fmt.Println("\n" + curlCmd + "\n") + + fmt.Println("\n" + curlCmd.String() + "\n") } diff --git a/go/appbuilder/dataset.go b/go/appbuilder/dataset.go index 726a4211..bc65c764 100644 --- a/go/appbuilder/dataset.go +++ b/go/appbuilder/dataset.go @@ -46,40 +46,60 @@ type Dataset struct { } func (t *Dataset) Create(name string) (string, error) { - request := http.Request{} + request := &http.Request{} header := t.sdkConfig.AuthHeader() serviceURL, err := t.sdkConfig.ServiceURL("/api/v1/ai_engine/agi_platform/v1/datasets/create") if err != nil { return "", err } - request.URL = serviceURL - request.Method = "POST" + + // 使用 http.NewRequest 构造请求对象 + request, err = http.NewRequest("POST", serviceURL.String(), nil) + if err != nil { + return "", err + } + + // 设置请求头 header.Set("Content-Type", "application/json") request.Header = header + + // 构造请求体 req := map[string]string{"name": name} data, _ := json.Marshal(req) request.Body = io.NopCloser(bytes.NewReader(data)) - t.sdkConfig.BuildCurlCommand(&request) - resp, err := t.client.Do(&request) + + // 构建并打印 Curl 命令 + t.sdkConfig.BuildCurlCommand(request) + + // 发送请求 + resp, err := t.client.Do(request) if err != nil { return "", err } defer resp.Body.Close() + + // 检查 HTTP 响应 requestID, err := checkHTTPResponse(resp) if err != nil { return "", fmt.Errorf("requestID=%s, err=%v", requestID, err) } + + // 读取响应体 data, err = io.ReadAll(resp.Body) if err != nil { return "", fmt.Errorf("requestID=%s, err=%v", requestID, err) } + + // 解析响应 rsp := DatasetResponse{} if err := json.Unmarshal(data, &rsp); err != nil { return "", fmt.Errorf("requestID=%s, err=%v", requestID, err) } + if rsp.Code != 0 { return "", fmt.Errorf("requestID=%s, content=%v", requestID, string(data)) } + return rsp.Result["id"].(string), nil }