Selaa lähdekoodia

更换 httpClient

wangbo 1 viikko sitten
vanhempi
commit
96e5aad56b
3 muutettua tiedostoa jossa 84 lisäystä ja 139 poistoa
  1. 2 2
      go.mod
  2. 5 4
      go.sum
  3. 77 133
      osm_sdk.go

+ 2 - 2
go.mod

@@ -4,8 +4,7 @@ go 1.23.1
 
 require (
 	git.sxidc.com/go-framework/baize v1.4.9
-	git.sxidc.com/go-tools/utils v1.5.29
-	git.sxidc.com/service-supports/dapr_api v0.2.0
+	git.sxidc.com/go-tools/utils v1.5.31
 	github.com/pkg/errors v0.9.1
 )
 
@@ -41,6 +40,7 @@ require (
 	golang.org/x/sys v0.20.0 // indirect
 	golang.org/x/text v0.15.0 // indirect
 	google.golang.org/protobuf v1.34.1 // indirect
+	gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
 	gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
 )

+ 5 - 4
go.sum

@@ -1,9 +1,7 @@
 git.sxidc.com/go-framework/baize v1.4.9 h1:2Q0PenF5oeW/3vpVkyqY3NOjUhaz8K6rvQWlipLeV8Q=
 git.sxidc.com/go-framework/baize v1.4.9/go.mod h1:NUCqSKPVM2hUUTSmQPWiJcjxecQsbfNAS1STO7OqtyE=
-git.sxidc.com/go-tools/utils v1.5.29 h1:OUrjzmnjxn+Anmz6FK2X5Pr4ljyrq5qNBaDs1dJLM6g=
-git.sxidc.com/go-tools/utils v1.5.29/go.mod h1:uTDb6QK5JZzK5+Fzsfeng7TwmnRDZiTY6JLYxIX94Qw=
-git.sxidc.com/service-supports/dapr_api v0.2.0 h1:OFTvpvWeIIHASnwPm+7khb6A5kOmy8dtH5C3CO2J2Kk=
-git.sxidc.com/service-supports/dapr_api v0.2.0/go.mod h1:lvu4Pba0W9ne4vZFs/H5cxhjedD80MxAeLtCw9B8jJs=
+git.sxidc.com/go-tools/utils v1.5.31 h1:7HIJlYS5OV4mZlRWEB6nZ67R0U43KXqEv00e+qPRzd8=
+git.sxidc.com/go-tools/utils v1.5.31/go.mod h1:uTDb6QK5JZzK5+Fzsfeng7TwmnRDZiTY6JLYxIX94Qw=
 github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
 github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
 github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
@@ -43,8 +41,11 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02
 github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
 github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
 github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
+github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
 github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
 github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
+github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
 github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=

+ 77 - 133
osm_sdk.go

@@ -1,24 +1,24 @@
 package osm_sdk
 
 import (
-	"bytes"
-	"encoding/json"
 	"git.sxidc.com/go-framework/baize/framework/core/api/response"
+	"git.sxidc.com/go-tools/utils/http_client"
 	"git.sxidc.com/go-tools/utils/strutils"
-	"git.sxidc.com/service-supports/dapr_api/invoke"
 	"github.com/pkg/errors"
 	"io"
-	"mime/multipart"
 	"time"
 )
 
 const TimeoutSec = 30
+
+var timeoutDuration = TimeoutSec * time.Second
+
 const servicePrefix = "/osm/api/v1"
 
 var prefix string
 var namespace string
 var name string
-var invokeAPI *invoke.API
+var httpClient *http_client.Client
 
 type (
 	ConfigKey struct {
@@ -55,11 +55,11 @@ type (
 )
 
 func Destroy() {
-	if invokeAPI == nil {
+	if httpClient == nil {
 		return
 	}
-	invoke.DestroyAPI(invokeAPI)
-	invokeAPI = nil
+	http_client.Destroy(httpClient)
+	httpClient = nil
 }
 
 func Init(configPrefix, configNamespace, configName, baseUrl string) error {
@@ -67,24 +67,13 @@ func Init(configPrefix, configNamespace, configName, baseUrl string) error {
 	namespace = configNamespace
 	name = configName
 
-	if invokeAPI == nil {
-		invokeAPI = invoke.NewAPI(baseUrl, TimeoutSec*time.Second)
+	if httpClient == nil {
+		httpClient = http_client.New()
 	}
 
 	return nil
 }
 
-//func Touch(reader io.Reader, to string) error {
-//	return TouchPrefix(reader, to, true)
-//}
-
-//func TouchPrefix(reader io.Reader, to string, addPfx bool) error {
-//	if addPfx {
-//		return Bkt.PutObject(prefix+to, reader)
-//	}
-//	return Bkt.PutObject(to, reader)
-//}
-
 func GetUrl(objName string) (string, error) {
 	return getUrl(objName)
 }
@@ -98,17 +87,19 @@ func getUrl(objName string) (string, error) {
 		return "", nil
 	}
 
-	responseBytes, err := invokeAPI.GetWithoutHeaders(servicePrefix+"/operation/url/get", map[string]string{
-		"namespace": namespace,
-		"name":      name,
-		"objName":   objName,
-	})
+	getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
+		Get(servicePrefix+"/operation/url/get",
+			http_client.WithRequestQueryParams(map[string]string{
+				"namespace": namespace,
+				"name":      name,
+				"objName":   objName,
+			}))
 	if err != nil {
 		return "", err
 	}
 
 	resp := new(response.InfoResponse[string])
-	err = json.Unmarshal(responseBytes, resp)
+	err = getResponse.Json(resp)
 	if err != nil {
 		return "", err
 	}
@@ -155,26 +146,22 @@ func zoomUrl(objName, process string) (string, error) {
 		return "", nil
 	}
 
-	requestJson, err := json.Marshal(ZoomUrlJsonBody{
-		ConfigKey: ConfigKey{
-			Namespace: namespace,
-			Name:      name,
-		},
-		ObjName:      objName,
-		Process:      process,
-		RequireInfos: nil,
-	})
-	if err != nil {
-		return "", err
-	}
-
-	responseBytes, err := invokeAPI.PostJSON(servicePrefix+"/operation/obj/zoomUrl", requestJson, nil)
+	postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
+		Post(servicePrefix+"/operation/obj/zoomUrl", ZoomUrlJsonBody{
+			ConfigKey: ConfigKey{
+				Namespace: namespace,
+				Name:      name,
+			},
+			ObjName:      objName,
+			Process:      process,
+			RequireInfos: nil,
+		}, nil)
 	if err != nil {
 		return "", err
 	}
 
 	resp := new(response.InfoResponse[string])
-	err = json.Unmarshal(responseBytes, resp)
+	err = postResponse.Json(resp)
 	if err != nil {
 		return "", err
 	}
@@ -207,17 +194,16 @@ func exist(objName string) (bool, error) {
 	if strutils.IsStringEmpty(objName) {
 		return false, nil
 	}
-	responseBytes, err := invokeAPI.GetWithoutHeaders(servicePrefix+"/operation/obj/check", map[string]string{
+	getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Get(servicePrefix+"/operation/obj/check", http_client.WithRequestQueryParams(map[string]string{
 		"namespace": namespace,
 		"name":      name,
-		"objName":   objName,
-	})
+		"objName":   objName}))
 	if err != nil {
 		return false, err
 	}
 
 	resp := new(response.InfoResponse[bool])
-	err = json.Unmarshal(responseBytes, resp)
+	err = getResponse.Json(resp)
 	if err != nil {
 		return false, err
 	}
@@ -245,8 +231,7 @@ func deleteMulti(objNames ...string) error {
 	if len(objNames) == 0 {
 		return nil
 	}
-
-	requestJson, err := json.Marshal(DeleteMultiObjJsonBody{
+	postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Post(servicePrefix+"/operation/obj/deleteMulti", DeleteMultiObjJsonBody{
 		ConfigKey: ConfigKey{
 			Namespace: namespace,
 			Name:      name,
@@ -258,13 +243,8 @@ func deleteMulti(objNames ...string) error {
 		return err
 	}
 
-	responseBytes, err := invokeAPI.PostJSON(servicePrefix+"/operation/obj/deleteMulti", requestJson, nil)
-	if err != nil {
-		return err
-	}
-
 	resp := new(response.MsgResponse)
-	err = json.Unmarshal(responseBytes, resp)
+	err = postResponse.Json(resp)
 	if err != nil {
 		return err
 	}
@@ -289,17 +269,17 @@ func listDir(dirPath string) ([]string, error) {
 		return nil, nil
 	}
 
-	responseBytes, err := invokeAPI.GetWithoutHeaders(servicePrefix+"/operation/objPath/list", map[string]string{
+	getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Get(servicePrefix+"/operation/objPath/list", http_client.WithRequestQueryParams(map[string]string{
 		"namespace": namespace,
 		"name":      name,
 		"dirPath":   dirPath,
-	})
+	}))
 	if err != nil {
 		return nil, err
 	}
 
 	resp := new(response.InfoResponse[[]string])
-	err = json.Unmarshal(responseBytes, resp)
+	err = getResponse.Json(resp)
 	if err != nil {
 		return nil, err
 	}
@@ -330,17 +310,18 @@ func lsDetails(dirPath string) ([]ObjectInfo, error) {
 		return []ObjectInfo{}, nil
 	}
 
-	responseBytes, err := invokeAPI.GetWithoutHeaders(servicePrefix+"/operation/objPath/listDetail", map[string]string{
-		"namespace": namespace,
-		"name":      name,
-		"dirPath":   dirPath,
-	})
+	getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
+		Get(servicePrefix+"/operation/objPath/listDetail", http_client.WithRequestQueryParams(map[string]string{
+			"namespace": namespace,
+			"name":      name,
+			"dirPath":   dirPath,
+		}))
 	if err != nil {
 		return nil, err
 	}
 
 	resp := new(response.InfoResponse[[]ObjectInfo])
-	err = json.Unmarshal(responseBytes, resp)
+	err = getResponse.Json(resp)
 	if err != nil {
 		return nil, err
 	}
@@ -376,32 +357,28 @@ func move(srcObjName, dstObjName string, requireInfos []string) error {
 	if strutils.IsStringEmpty(dstObjName) {
 		return nil
 	}
-	requestJson, err := json.Marshal(MoveJsonBody{
-		ConfigKey: ConfigKey{
-			Namespace: namespace,
-			Name:      name,
-		},
-		SrcObjName:   srcObjName,
-		DstObjName:   dstObjName,
-		RequireInfos: requireInfos,
-		Prefix:       prefix,
-	})
-	if err != nil {
-		return err
-	}
-	responseBytes, err := invokeAPI.PutJSON(servicePrefix+"/operation/obj/move", requestJson, nil)
+	putResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
+		Put(servicePrefix+"/operation/obj/move", MoveJsonBody{
+			ConfigKey: ConfigKey{
+				Namespace: namespace,
+				Name:      name,
+			},
+			SrcObjName:   srcObjName,
+			DstObjName:   dstObjName,
+			RequireInfos: requireInfos,
+			Prefix:       prefix,
+		}, nil)
 	if err != nil {
 		return err
 	}
 	resp := new(response.InfoResponse[any])
-	err = json.Unmarshal(responseBytes, resp)
+	err = putResponse.Json(resp)
 	if err != nil {
 		return err
 	}
 	if !resp.Success {
 		return errors.New(resp.Msg)
 	}
-
 	return nil
 }
 
@@ -426,27 +403,23 @@ func copying(srcObjName, dstObjName string, requireInfos []string) error {
 		return nil
 	}
 
-	requestJson, err := json.Marshal(CopyJsonBody{
-		ConfigKey: ConfigKey{
-			Namespace: namespace,
-			Name:      name,
-		},
-		SrcObjName:   srcObjName,
-		DstObjName:   dstObjName,
-		RequireInfos: requireInfos,
-		Prefix:       prefix,
-	})
-	if err != nil {
-		return err
-	}
-
-	responseBytes, err := invokeAPI.PostJSON(servicePrefix+"/operation/obj/copy", requestJson, nil)
+	postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
+		Post(servicePrefix+"/operation/obj/copy", CopyJsonBody{
+			ConfigKey: ConfigKey{
+				Namespace: namespace,
+				Name:      name,
+			},
+			SrcObjName:   srcObjName,
+			DstObjName:   dstObjName,
+			RequireInfos: requireInfos,
+			Prefix:       prefix,
+		}, nil)
 	if err != nil {
 		return err
 	}
 
 	resp := new(response.InfoResponse[any])
-	err = json.Unmarshal(responseBytes, resp)
+	err = postResponse.Json(resp)
 	if err != nil {
 		return err
 	}
@@ -473,7 +446,9 @@ func copyPublic(srcObjName, dstObjName string, requireInfos []string) error {
 	if strutils.IsStringEmpty(dstObjName) {
 		return nil
 	}
-	requestJson, err := json.Marshal(CopyJsonBody{
+
+	postRequest := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration))
+	postResponse, err := postRequest.Post(servicePrefix+"/operation/obj/copyPublic", CopyJsonBody{
 		ConfigKey: ConfigKey{
 			Namespace: namespace,
 			Name:      name,
@@ -486,14 +461,8 @@ func copyPublic(srcObjName, dstObjName string, requireInfos []string) error {
 	if err != nil {
 		return err
 	}
-
-	responseBytes, err := invokeAPI.PostJSON(servicePrefix+"/operation/obj/copyPublic", requestJson, nil)
-	if err != nil {
-		return err
-	}
-
 	resp := new(response.InfoResponse[any])
-	err = json.Unmarshal(responseBytes, resp)
+	err = postResponse.Json(resp)
 	if err != nil {
 		return err
 	}
@@ -518,40 +487,16 @@ func createImage(reader io.Reader, objName string) (string, error) {
 	if strutils.IsStringEmpty(objName) {
 		return "", nil
 	}
-	bodyBuffer := &bytes.Buffer{}
-	multipartWriter := multipart.NewWriter(bodyBuffer)
-	// 添加字段
-	fieldWriter, err := multipartWriter.CreateFormField("objName")
-	if err != nil {
-		return "", err
-	}
-	_, err = fieldWriter.Write([]byte(objName))
-	if err != nil {
-		return "", err
-	}
-
-	// 添加文件
-	fileWriter, err := multipartWriter.CreateFormFile("file", objName)
-	if err != nil {
-		return "", err
-	}
-	_, err = io.Copy(fileWriter, reader)
-	if err != nil {
-		return "", err
-	}
 
-	bodyByte, err := io.ReadAll(bodyBuffer)
-	if err != nil {
-		return "", err
-	}
-	responseBytes, err := invokeAPI.Post(servicePrefix+"/operation/obj/create/image", map[string]string{
-		"Content-Type": multipartWriter.FormDataContentType()}, bodyByte)
+	postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
+		Post(servicePrefix+"/operation/obj/create/image", nil,
+			http_client.WithRequestFileReader("file", objName, reader))
 	if err != nil {
 		return "", err
 	}
 
 	resp := new(response.InfoResponse[string])
-	err = json.Unmarshal(responseBytes, resp)
+	err = postResponse.Json(resp)
 	if err != nil {
 		return "", err
 	}
@@ -559,7 +504,6 @@ func createImage(reader io.Reader, objName string) (string, error) {
 	if !resp.Success {
 		return "", errors.New(resp.Msg)
 	}
-
 	return resp.Info, nil
 }
 
@@ -606,7 +550,7 @@ func createImage(reader io.Reader, objName string) (string, error) {
 //	}
 //	buf := new(bytes.Buffer)
 //
-//	responseBytes, err := invokeAPI.GetWithoutHeaders(servicePrefix+"/operation/obj/getContent", map[string]string{
+//	responseBytes, err := httpClient.GetWithoutHeaders(servicePrefix+"/operation/obj/getContent", map[string]string{
 //		"namespace": namespace,
 //		"name":      name,
 //		"objName":   objName,