You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

proxyapilogic.go 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package ai
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/tls"
  6. "encoding/json"
  7. "github.com/go-resty/resty/v2"
  8. "github.com/pkg/errors"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/hws"
  10. "net/http"
  11. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  12. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type ProxyApiLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewProxyApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProxyApiLogic {
  21. return &ProxyApiLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. const (
  28. XProjectID = "d18190e28e3f45a281ef0b0696ec9d52"
  29. XStage = "RELEASE"
  30. ContentType = "application/json"
  31. )
  32. func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq) (resp *types.ChatResult, err error) {
  33. logx.Infof("【开始处理请求,目标URL: %s】", req.ApiUrl)
  34. jsonBytes, err := json.Marshal(&req.ReqData)
  35. if err != nil {
  36. logx.Errorf("【序列化请求数据失败: %v】", err)
  37. return nil, errors.New("请求数据序列化失败")
  38. }
  39. resp = &types.ChatResult{}
  40. // 构建 HTTP 请求
  41. request, err := http.NewRequest("POST", req.ApiUrl, bytes.NewBuffer(jsonBytes))
  42. if err != nil {
  43. logx.Errorf("【构建 HTTP 请求失败: %v】", err)
  44. return nil, errors.New("网络错误,请稍后重试")
  45. }
  46. signer := &hws.Signer{
  47. Key: "UNEHPHO4Z7YSNPKRXFE4",
  48. Secret: "JWXCE9qcYbc7RjpSRIWt4WgG3ZKF6Q4lPzkJReX9",
  49. }
  50. if err := signer.Sign(request); err != nil {
  51. logx.Errorf("【接口签名错误: %v】", err)
  52. return nil, errors.New("网络错误,请稍后重试")
  53. }
  54. client := resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  55. response, err := client.R().
  56. SetHeader("X-Project-Id", XProjectID).
  57. SetHeader("x-stage", XStage).
  58. SetHeader("Content-Type", ContentType).
  59. SetHeader("Authorization", request.Header.Get(hws.HeaderXAuthorization)).
  60. SetHeader("X-Sdk-Date", request.Header.Get(hws.HeaderXDateTime)).
  61. SetBody(jsonBytes).
  62. SetResult(&resp).
  63. Post(req.ApiUrl)
  64. if err != nil {
  65. logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", req.ApiUrl, err.Error())
  66. return nil, errors.New("网络错误,请稍后重试")
  67. }
  68. if response.StatusCode() != 200 {
  69. logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", req.ApiUrl, response.Body())
  70. return nil, errors.New("网络错误,请稍后重试")
  71. }
  72. logx.Infof("【请求处理成功,目标URL: %s】", req.ApiUrl)
  73. return resp, nil
  74. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.