Signed-off-by: jagger <cossjie@foxmail.com>

Former-commit-id: 37b5cbe848e7641c1666514b03a63a79d9b98036
This commit is contained in:
jagger 2024-06-27 15:54:21 +08:00
parent c83412b42f
commit 79b5134594
4 changed files with 53 additions and 44 deletions

View File

@ -1822,8 +1822,11 @@ service AICore-api {
type (
ChatReq{
ApiUrl string `json:"apiUrl,optional"`
ApiUrl string `json:"apiUrl"`
Method string `json:"method,optional"`
ReqData map[string]interface{} `json:"reqData"`
}
ChatResult{
Resuluts string `json:"results,optional"`
}
)

View File

@ -18,7 +18,7 @@ func ProxyApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
l := ai.NewProxyApiLogic(r.Context(), svcCtx)
resp, err := l.ProxyApi(&req, w)
resp, err := l.ProxyApi(&req)
result.HttpResult(r, w, resp, err)
}
}

View File

@ -5,8 +5,8 @@ import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/hws"
"net/http"
@ -30,21 +30,28 @@ func NewProxyApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProxyApi
}
}
type ChatResult struct {
Results string `json:"results"`
}
const (
XProjectID = "d18190e28e3f45a281ef0b0696ec9d52"
XStage = "RELEASE"
ContentType = "application/json"
)
type ResponseData struct {
Results string `json:"results"`
}
func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq, w http.ResponseWriter) (resp *types.CommonResp, err error) {
func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq) (resp *types.ChatResult, err error) {
logx.Infof("【开始处理请求目标URL: %s】", req.ApiUrl)
jsonBytes, err := json.Marshal(&req.ReqData)
// 调用第三方接口的 POST 方法
thirdReq, err := http.NewRequest("POST", req.ApiUrl, bytes.NewBuffer(jsonBytes))
if err != nil {
return
logx.Errorf("【序列化请求数据失败: %v】", err)
return nil, errors.New("请求数据序列化失败")
}
resp = &types.ChatResult{}
// 构建 HTTP 请求
request, err := http.NewRequest("POST", req.ApiUrl, bytes.NewBuffer(jsonBytes))
if err != nil {
logx.Errorf("【构建 HTTP 请求失败: %v】", err)
return nil, errors.New("网络错误,请稍后重试")
}
signer := &hws.Signer{
@ -52,38 +59,33 @@ func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq, w http.ResponseWriter) (res
Secret: "JWXCE9qcYbc7RjpSRIWt4WgG3ZKF6Q4lPzkJReX9",
}
if err := signer.Sign(thirdReq); err != nil {
return nil, err
if err := signer.Sign(request); err != nil {
logx.Errorf("【接口签名错误: %v】", err)
return nil, errors.New("网络错误,请稍后重试")
}
// 设置client信任所有证书
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
client := resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
response, err := client.R().
SetHeader("X-Project-Id", XProjectID).
SetHeader("x-stage", XStage).
SetHeader("Content-Type", ContentType).
SetHeader("Authorization", request.Header.Get(hws.HeaderXAuthorization)).
SetHeader("X-Sdk-Date", request.Header.Get(hws.HeaderXDateTime)).
SetBody(jsonBytes).
SetResult(&resp).
Post(req.ApiUrl)
if err != nil {
logx.Errorf("【远程调用接口URL:%s 返回错误: %s】", req.ApiUrl, err.Error())
return nil, errors.New("网络错误,请稍后重试")
}
thirdReq.Header.Set("X-Project-Id", "d18190e28e3f45a281ef0b0696ec9d52")
thirdReq.Header.Set("x-stage", "RELEASE")
thirdReq.Header.Set("Authorization", thirdReq.Header.Get(hws.HeaderXAuthorization))
thirdReq.Header.Set("X-Sdk-Date", thirdReq.Header.Get(hws.HeaderXDateTime))
thirdReq.Header.Set("Content-Type", "application/json")
thirdResp, err := client.Do(thirdReq)
defer thirdReq.Body.Close()
var responseData ResponseData
decoder := json.NewDecoder(thirdResp.Body)
if err := decoder.Decode(&responseData); err != nil {
fmt.Println("Error decoding response:", err)
if response.StatusCode() != 200 {
logx.Errorf("【远程调用接口URL:%s 返回错误: %s】", req.ApiUrl, response.Body())
return nil, errors.New("网络错误,请稍后重试")
}
chatResult := &ChatResult{}
tool.Convert(responseData, &chatResult)
return &types.CommonResp{
Code: thirdResp.StatusCode,
Msg: "success",
Data: chatResult,
}, nil
logx.Infof("【请求处理成功目标URL: %s】", req.ApiUrl)
return resp, nil
}

View File

@ -2877,11 +2877,15 @@ type AiTask struct {
}
type ChatReq struct {
ApiUrl string `json:"apiUrl,optional"`
ApiUrl string `json:"apiUrl"`
Method string `json:"method,optional"`
ReqData map[string]interface{} `json:"reqData"`
}
type ChatResult struct {
Resuluts string `json:"results,optional"`
}
type StorageScreenReq struct {
}