fix bug
Signed-off-by: jagger <cossjie@foxmail.com> Former-commit-id: 37b5cbe848e7641c1666514b03a63a79d9b98036
This commit is contained in:
parent
c83412b42f
commit
79b5134594
|
@ -1822,8 +1822,11 @@ service AICore-api {
|
||||||
|
|
||||||
type (
|
type (
|
||||||
ChatReq{
|
ChatReq{
|
||||||
ApiUrl string `json:"apiUrl,optional"`
|
ApiUrl string `json:"apiUrl"`
|
||||||
Method string `json:"method,optional"`
|
Method string `json:"method,optional"`
|
||||||
ReqData map[string]interface{} `json:"reqData"`
|
ReqData map[string]interface{} `json:"reqData"`
|
||||||
}
|
}
|
||||||
|
ChatResult{
|
||||||
|
Resuluts string `json:"results,optional"`
|
||||||
|
}
|
||||||
)
|
)
|
|
@ -18,7 +18,7 @@ func ProxyApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
l := ai.NewProxyApiLogic(r.Context(), svcCtx)
|
l := ai.NewProxyApiLogic(r.Context(), svcCtx)
|
||||||
resp, err := l.ProxyApi(&req, w)
|
resp, err := l.ProxyApi(&req)
|
||||||
result.HttpResult(r, w, resp, err)
|
result.HttpResult(r, w, resp, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,8 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"github.com/go-resty/resty/v2"
|
||||||
tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
|
"github.com/pkg/errors"
|
||||||
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/hws"
|
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/hws"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
@ -30,21 +30,28 @@ func NewProxyApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProxyApi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatResult struct {
|
const (
|
||||||
Results string `json:"results"`
|
XProjectID = "d18190e28e3f45a281ef0b0696ec9d52"
|
||||||
}
|
XStage = "RELEASE"
|
||||||
|
ContentType = "application/json"
|
||||||
|
)
|
||||||
|
|
||||||
type ResponseData struct {
|
func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq) (resp *types.ChatResult, err error) {
|
||||||
Results string `json:"results"`
|
logx.Infof("【开始处理请求,目标URL: %s】", req.ApiUrl)
|
||||||
}
|
|
||||||
|
|
||||||
func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq, w http.ResponseWriter) (resp *types.CommonResp, err error) {
|
|
||||||
|
|
||||||
jsonBytes, err := json.Marshal(&req.ReqData)
|
jsonBytes, err := json.Marshal(&req.ReqData)
|
||||||
// 调用第三方接口的 POST 方法
|
|
||||||
thirdReq, err := http.NewRequest("POST", req.ApiUrl, bytes.NewBuffer(jsonBytes))
|
|
||||||
if err != nil {
|
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{
|
signer := &hws.Signer{
|
||||||
|
@ -52,38 +59,33 @@ func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq, w http.ResponseWriter) (res
|
||||||
Secret: "JWXCE9qcYbc7RjpSRIWt4WgG3ZKF6Q4lPzkJReX9",
|
Secret: "JWXCE9qcYbc7RjpSRIWt4WgG3ZKF6Q4lPzkJReX9",
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := signer.Sign(thirdReq); err != nil {
|
if err := signer.Sign(request); err != nil {
|
||||||
return nil, err
|
logx.Errorf("【接口签名错误: %v】", err)
|
||||||
|
return nil, errors.New("网络错误,请稍后重试")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置client信任所有证书
|
client := resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
|
||||||
tr := &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
response, err := client.R().
|
||||||
}
|
SetHeader("X-Project-Id", XProjectID).
|
||||||
client := &http.Client{
|
SetHeader("x-stage", XStage).
|
||||||
Transport: tr,
|
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")
|
if response.StatusCode() != 200 {
|
||||||
thirdReq.Header.Set("x-stage", "RELEASE")
|
logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", req.ApiUrl, response.Body())
|
||||||
thirdReq.Header.Set("Authorization", thirdReq.Header.Get(hws.HeaderXAuthorization))
|
return nil, errors.New("网络错误,请稍后重试")
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chatResult := &ChatResult{}
|
logx.Infof("【请求处理成功,目标URL: %s】", req.ApiUrl)
|
||||||
tool.Convert(responseData, &chatResult)
|
return resp, nil
|
||||||
return &types.CommonResp{
|
|
||||||
Code: thirdResp.StatusCode,
|
|
||||||
Msg: "success",
|
|
||||||
Data: chatResult,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2877,11 +2877,15 @@ type AiTask struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatReq struct {
|
type ChatReq struct {
|
||||||
ApiUrl string `json:"apiUrl,optional"`
|
ApiUrl string `json:"apiUrl"`
|
||||||
Method string `json:"method,optional"`
|
Method string `json:"method,optional"`
|
||||||
ReqData map[string]interface{} `json:"reqData"`
|
ReqData map[string]interface{} `json:"reqData"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChatResult struct {
|
||||||
|
Resuluts string `json:"results,optional"`
|
||||||
|
}
|
||||||
|
|
||||||
type StorageScreenReq struct {
|
type StorageScreenReq struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue