tyc-server/app/main/api/internal/logic/query/querysingletestlogic.go

61 lines
1.8 KiB
Go
Raw Normal View History

package query
import (
"context"
"encoding/json"
2025-03-21 15:53:59 +08:00
"time"
2025-04-09 15:58:06 +08:00
"tyc-server/common/xerr"
2025-03-20 02:03:28 +08:00
"github.com/pkg/errors"
2025-04-27 12:17:18 +08:00
"tyc-server/app/main/api/internal/svc"
"tyc-server/app/main/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type QuerySingleTestLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewQuerySingleTestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QuerySingleTestLogic {
return &QuerySingleTestLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *QuerySingleTestLogic) QuerySingleTest(req *types.QuerySingleTestReq) (resp *types.QuerySingleTestResp, err error) {
2025-03-21 15:47:11 +08:00
// _, err = l.svcCtx.FeatureModel.FindOneByApiId(l.ctx, req.Api)
// if err != nil {
// return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "单查测试, 获取接口失败 : %d", err)
// }
2025-03-20 02:03:28 +08:00
marshalParams, err := json.Marshal(req.Params)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "单查测试, 序列化参数失败 : %d", err)
}
2025-03-21 15:53:59 +08:00
// 创建一个30分钟超时的上下文
timeoutCtx, cancel := context.WithTimeout(l.ctx, 30*time.Minute)
defer cancel() // 确保在函数返回时取消上下文,防止资源泄漏
// 使用新的超时上下文
apiResp, err := l.svcCtx.ApiRequestService.PreprocessRequestApi(timeoutCtx, marshalParams, req.Api)
if err != nil {
2025-03-21 16:12:47 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "单查测试, 接口请求失败 : %d", err)
}
var respData interface{}
2025-03-21 15:47:11 +08:00
err = json.Unmarshal(apiResp.Data, &respData)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "单查测试, 反序列化接口失败 : %d", err)
}
return &types.QuerySingleTestResp{
Data: respData,
Api: req.Api,
}, nil
}