open_api_lib.go 1.3 KB
package apilib

import (
	"fmt"
	"github.com/beego/beego/v2/core/logs"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api"
	"net/http"
	"time"
)

type OpenApiLib struct {
	Token string
	api.BaseServiceGateway
}

func (gateway *OpenApiLib) WithToken(token string) *OpenApiLib {
	gateway.Token = token
	return gateway
}

func (gateway *OpenApiLib) DefaultHeader() http.Header {
	var header = make(map[string][]string)
	header["x-mmm-accesstoken"] = []string{gateway.Token}
	header["x-mmm-appproject"] = []string{"byte-bank"}
	return header
}

func NewApiAuthLib(host string) *OpenApiLib {
	gt := api.NewBaseServiceGateway(host)
	gt.ConnectTimeout = 360 * time.Second
	gt.ReadWriteTimeout = 360 * time.Second
	gt.Interceptor = func(msg string) {
		logs.Debug(msg)
	}
	gt.ServiceName = "【开发接口】"
	return &OpenApiLib{
		BaseServiceGateway: gt,
	}
}

func (gateway *OpenApiLib) Upload(param RequestUpload) (*DataUploadItem, error) {
	url := gateway.Host() + "/v1/vod/putObject"
	method := "post"
	var data DataUpload
	err := gateway.FastDoRequest(url, method, struct{}{}, &data, api.WithHeader(gateway.DefaultHeader()), api.WithFileMap(param.UploadFileMap))
	if err != nil {
		return nil, err
	}
	if len(data) > 0 {
		return data[0], nil
	}
	return nil, fmt.Errorf("上传失败")
}