|
@@ -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,
|