/
debug-interface.go
168 lines (145 loc) · 5.51 KB
/
debug-interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package handler
import (
serverDomain "github.com/aaronchen2k/deeptest/cmd/server/v1/domain"
"github.com/aaronchen2k/deeptest/internal/pkg/domain"
service "github.com/aaronchen2k/deeptest/internal/server/modules/service"
_domain "github.com/aaronchen2k/deeptest/pkg/domain"
"github.com/kataras/iris/v12"
"github.com/snowlyg/multi"
)
type DebugInterfaceCtrl struct {
EndpointCaseService *service.EndpointCaseService `inject:""`
DebugInterfaceService *service.DebugInterfaceService `inject:""`
ExtractorService *service.ExtractorService `inject:""`
CheckpointService *service.CheckpointService `inject:""`
BaseCtrl
}
// Load
// @Tags 接口调试
// @summary 获取调试接口请求
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param DebugReq body domain.DebugInfo true "获取调试接口请求的请求体"
// @success 200 {object} _domain.Response{data=domain.DebugData}
// @Router /api/v1/debugs/interface/load [post]
func (c *DebugInterfaceCtrl) Load(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
req := domain.DebugInfo{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: _domain.ParamErr.Msg})
return
}
req.UserId = multi.GetUserId(ctx)
data, err := c.DebugInterfaceService.Load(tenantId, req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: data, Msg: _domain.NoErr.Msg})
}
// LoadForExec
// @Tags 接口调试
// @summary 获取调试接口用于执行
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param DebugReq body domain.DebugInfo true "获取调试接口用于执行的请求体"
// @success 200 {object} _domain.Response{data=agentExec.InterfaceExecObj}
// @Router /api/v1/debugs/interface/loadForExec [post]
func (c *DebugInterfaceCtrl) LoadForExec(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
req := domain.DebugInfo{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: _domain.ParamErr.Msg})
return
}
req.UserId = multi.GetUserId(ctx)
req.ProjectId, err = ctx.URLParamInt("currProjectId")
data, err := c.DebugInterfaceService.LoadForExec(tenantId, req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: data, Msg: _domain.NoErr.Msg})
}
// Save
// @Tags 接口调试
// @summary 保存调试接口
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param DebugReq body domain.DebugData true "保存调试接口的请求体"
// @success 200 {object} _domain.Response{data=agentExec.InterfaceExecObj}
// @Router /api/v1/debugs/interface/save [post]
func (c *DebugInterfaceCtrl) Save(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
req := domain.DebugData{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: _domain.ParamErr.Msg})
return
}
po, err := c.DebugInterfaceService.CreateOrUpdate(tenantId, req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
loadReq := domain.DebugInfo{
DebugInterfaceId: po.ID,
EndpointInterfaceId: po.EndpointInterfaceId,
UsedBy: req.UsedBy,
}
data, err := c.DebugInterfaceService.Load(tenantId, loadReq)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.SystemErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: data})
}
// SaveAsCase
func (c *DebugInterfaceCtrl) SaveAsCase(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
req := serverDomain.EndpointCaseSaveReq{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: _domain.ParamErr.Msg})
return
}
req.CreateUserName = multi.GetUsername(ctx)
req.CreateUserId = multi.GetUserId(ctx)
c.EndpointCaseService.SaveFromDebugInterface(tenantId, req)
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code})
}
// LoadCurl 导入cURL命令
// @Tags 快捷调试
// @summary 获取cURL命令
// @accept application/json
// @Produce application/json
// @Param Authorization header string true "Authentication header"
// @Param currProjectId query int true "当前项目ID"
// @Param DiagnoseCurlLoadReq body serverDomain.DiagnoseCurlLoadReq true "导入cURL命令的请求体"
// @success 200 {object} _domain.Response{data=string}
// @Router /api/v1/debugs/interface/loadCurl [post]
func (c *DebugInterfaceCtrl) LoadCurl(ctx iris.Context) {
tenantId := c.getTenantId(ctx)
req := serverDomain.DiagnoseCurlLoadReq{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: _domain.ParamErr.Msg})
return
}
req.ProjectId, err = ctx.URLParamInt("currProjectId")
req.UserId = multi.GetUserId(ctx)
content, err := c.DebugInterfaceService.LoadCurl(tenantId, req)
if err != nil {
ctx.JSON(_domain.Response{Code: _domain.ParamErr.Code, Msg: err.Error()})
return
}
ctx.JSON(_domain.Response{Code: _domain.NoErr.Code, Data: content})
}