|
|
|
@@ -0,0 +1,56 @@ |
|
|
|
package ai |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"context" |
|
|
|
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" |
|
|
|
tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" |
|
|
|
"k8s.io/apimachinery/pkg/util/json" |
|
|
|
"net/http" |
|
|
|
|
|
|
|
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" |
|
|
|
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" |
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx" |
|
|
|
) |
|
|
|
|
|
|
|
type ProxyApiLogic struct { |
|
|
|
logx.Logger |
|
|
|
ctx context.Context |
|
|
|
svcCtx *svc.ServiceContext |
|
|
|
} |
|
|
|
|
|
|
|
func NewProxyApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProxyApiLogic { |
|
|
|
return &ProxyApiLogic{ |
|
|
|
Logger: logx.WithContext(ctx), |
|
|
|
ctx: ctx, |
|
|
|
svcCtx: svcCtx, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
type ChatResult struct { |
|
|
|
Results string `json:"results"` |
|
|
|
} |
|
|
|
|
|
|
|
func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq, w http.ResponseWriter) (resp *types.CommonResp, err error) { |
|
|
|
jsonBytes, err := json.Marshal(&req.ReqData) |
|
|
|
// 调用第三方接口的 POST 方法 |
|
|
|
respThirdParty, err := http.Post(req.ApiUrl, "application/json", bytes.NewBuffer(jsonBytes)) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
defer respThirdParty.Body.Close() |
|
|
|
marshal, err := json.Marshal(&respThirdParty.Body) |
|
|
|
if err != nil { |
|
|
|
return nil, result.NewDefaultError(err.Error()) |
|
|
|
} |
|
|
|
json.Unmarshal(marshal, &resp) |
|
|
|
|
|
|
|
chatResult := &ChatResult{} |
|
|
|
tool.Convert(resp, &chatResult.Results) |
|
|
|
return &types.CommonResp{ |
|
|
|
Code: respThirdParty.StatusCode, |
|
|
|
Msg: "success", |
|
|
|
Data: chatResult, |
|
|
|
}, nil |
|
|
|
} |