/
client.go
280 lines (247 loc) · 7.53 KB
/
client.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package gorp
import (
"bytes"
"encoding/json"
"fmt"
"mime/multipart"
"os"
"time"
"gopkg.in/resty.v1"
)
//Client is ReportPortal REST API Client
type Client struct {
project string
http *resty.Client
}
//NewClient creates new instance of Client
//host - server hostname
//project - name of the project
//uuid - User Token (see user profile page)
func NewClient(host, project, uuid string) *Client {
http := resty.New().
//SetDebug(true).
SetHostURL(host).
SetAuthToken(uuid).
OnAfterResponse(func(client *resty.Client, rs *resty.Response) error {
if (rs.StatusCode() / 100) >= 4 {
return fmt.Errorf("status code error: %d\n%s", rs.StatusCode(), rs.String())
}
return nil
})
return &Client{
project: project,
http: http,
}
}
//StartLaunch starts new launch in RP
func (c *Client) StartLaunch(launch *StartLaunchRQ) (*EntryCreatedRS, error) {
return c.startLaunch(launch)
}
//StartLaunchRaw starts new launch in RP with body in form of bytes buffer
func (c *Client) StartLaunchRaw(body *bytes.Buffer) (*EntryCreatedRS, error) {
return c.startLaunch(body)
}
//StartLaunch starts new launch in RP
func (c *Client) startLaunch(body interface{}) (*EntryCreatedRS, error) {
var rs EntryCreatedRS
_, err := c.http.R().
SetPathParams(map[string]string{"project": c.project}).
SetBody(body).
SetResult(&rs).
Post("/api/v1/{project}/launch")
return &rs, err
}
//FinishLaunch finishes launch in RP
func (c *Client) FinishLaunch(id string, launch *FinishExecutionRQ) (*FinishLaunchRS, error) {
return c.finishLaunch(id, launch)
}
//FinishLaunchRaw finishes launch in RP with body in form of bytes buffer
func (c *Client) FinishLaunchRaw(id string, body *bytes.Buffer) (*FinishLaunchRS, error) {
return c.finishLaunch(id, body)
}
//FinishLaunch finishes launch in RP
func (c *Client) finishLaunch(id string, body interface{}) (*FinishLaunchRS, error) {
var rs FinishLaunchRS
_, err := c.http.R().
SetPathParams(map[string]string{
"project": c.project,
"launchId": id,
}).
SetBody(body).
SetResult(&rs).
Put("/api/v1/{project}/launch/{launchId}/finish")
return &rs, err
}
//StopLaunch forces finishing launch
func (c *Client) StopLaunch(id string) (*MsgRS, error) {
var rs MsgRS
_, err := c.http.R().
SetPathParams(map[string]string{
"project": c.project,
"launchId": id,
}).
SetBody(&FinishExecutionRQ{
EndTime: Timestamp{Time: time.Now()},
Status: StatusStopped,
}).
SetResult(&rs).
Put("/api/v1/{project}/launch/{launchId}/stop")
return &rs, err
}
//StartTest starts new test in RP
func (c *Client) StartTest(item *StartTestRQ) (*EntryCreatedRS, error) {
return c.startTest(item)
}
//StartTestRaw starts new test in RP accepting request body as array of bytes
func (c *Client) StartTestRaw(body *bytes.Buffer) (*EntryCreatedRS, error) {
return c.startTest(body)
}
//startTest starts new test in RP
func (c *Client) startTest(body interface{}) (*EntryCreatedRS, error) {
var rs EntryCreatedRS
_, err := c.http.R().
SetPathParams(map[string]string{"project": c.project}).
SetBody(body).
SetResult(&rs).
Post("/api/v1/{project}/item/")
return &rs, err
}
//startChildTest starts new test in RP
func (c *Client) startChildTest(parent string, body interface{}) (*EntryCreatedRS, error) {
var rs EntryCreatedRS
_, err := c.http.R().
SetPathParams(map[string]string{
"project": c.project,
"itemId": parent,
}).
SetBody(body).
SetResult(&rs).
Post("/api/v1/{project}/item/{itemId}")
return &rs, err
}
//StartChildTest starts new test in RP
func (c *Client) StartChildTest(parent string, item *StartTestRQ) (*EntryCreatedRS, error) {
return c.startChildTest(parent, item)
}
//StartChildTestRaw starts new test in RP accepting request body as array of bytes
func (c *Client) StartChildTestRaw(parent string, body *bytes.Buffer) (*EntryCreatedRS, error) {
return c.startChildTest(parent, body)
}
//FinishTest finishes test in RP
func (c *Client) FinishTest(id string, launch *FinishTestRQ) (*MsgRS, error) {
return c.finishTest(id, launch)
}
//FinishTestRaw finishes test in RP accepting body as array of bytes
func (c *Client) FinishTestRaw(id string, body *bytes.Buffer) (*MsgRS, error) {
return c.finishTest(id, body)
}
//finishTest finishes test in RP
func (c *Client) finishTest(id string, body interface{}) (*MsgRS, error) {
var rs MsgRS
_, err := c.http.R().
SetPathParams(map[string]string{
"project": c.project,
"itemId": id,
}).
SetBody(body).
SetResult(&rs).
Put("/api/v1/{project}/item/{itemId}")
return &rs, err
}
//SaveLog attaches log in RP
func (c *Client) SaveLog(log *SaveLogRQ) (*EntryCreatedRS, error) {
var rs EntryCreatedRS
_, err := c.http.R().
SetPathParams(map[string]string{
"project": c.project,
}).
SetBody(log).
SetResult(&rs).
Post("/api/v1/{project}/log")
return &rs, err
}
//SaveLog attaches log in RP
func (c *Client) SaveLogMultipart(log *SaveLogRQ, files map[string][]os.File) (*EntryCreatedRS, error) {
body := &bytes.Buffer{}
// JSON PART
mWriter := multipart.NewWriter(body)
jsonPart, _ := mWriter.CreatePart(map[string][]string{"Content-Type": {"application/json"}})
err := json.NewEncoder(jsonPart).Encode(log)
// BINARY PART
var rs EntryCreatedRS
rq := c.http.R().
SetPathParams(map[string]string{
"project": c.project,
})
_, err = rq.
SetResult(&rs).
Post("/api/v1/{project}/log")
return &rs, err
}
//GetLaunches retrieves latest launches
func (c *Client) GetLaunches() (*LaunchPage, error) {
var launches LaunchPage
_, err := c.http.R().
SetPathParams(map[string]string{"project": c.project}).
SetResult(&launches).
Get("/api/v1/{project}/launch")
return &launches, err
}
//GetLaunchesByFilter retrieves launches by filter
func (c *Client) GetLaunchesByFilter(filter map[string]string) (*LaunchPage, error) {
var launches LaunchPage
_, err := c.http.R().
SetPathParams(map[string]string{"project": c.project}).
SetResult(&launches).
SetQueryParams(filter).
Get("/api/v1/{project}/launch")
return &launches, err
}
//GetLaunchesByFilterString retrieves launches by filter as string
func (c *Client) GetLaunchesByFilterString(filter string) (*LaunchPage, error) {
var launches LaunchPage
_, err := c.http.R().
SetPathParams(map[string]string{"project": c.project}).
SetResult(&launches).
SetQueryString(filter).
Get("/api/v1/{project}/launch")
return &launches, err
}
//GetLaunchesByFilterName retrieves launches by filter name
func (c *Client) GetLaunchesByFilterName(name string) (*LaunchPage, error) {
filter, err := c.GetFiltersByName(name)
if err != nil {
return nil, err
}
if filter.Page.Size < 1 || len(filter.Content) == 0 {
return nil, fmt.Errorf("no filter %s found", name)
}
var launches LaunchPage
params := ConvertToFilterParams(filter.Content[0])
_, err = c.http.R().
SetPathParams(map[string]string{"project": c.project}).
SetResult(&launches).
SetQueryParams(params).
Get("/api/v1/{project}/launch")
return &launches, err
}
//GetFiltersByName retrieves filter by its name
func (c *Client) GetFiltersByName(name string) (*FilterPage, error) {
var filter FilterPage
_, err := c.http.R().
SetPathParams(map[string]string{"project": c.project, "name": name}).
SetQueryParam("filter.eq.name", name).
SetResult(&filter).
Get("/api/v1/{project}/filter")
return &filter, err
}
//MergeLaunches merge two launches
func (c *Client) MergeLaunches(rq *MergeLaunchesRQ) (*LaunchResource, error) {
var rs LaunchResource
_, err := c.http.R().
SetPathParams(map[string]string{"project": c.project}).
SetBody(rq).
SetResult(&rs).
Post("/api/v1/{project}/launch/merge")
return &rs, err
}