/
launch.go
90 lines (82 loc) · 2.11 KB
/
launch.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
package reportportal
import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"os"
)
const (
importFileFieldName = "file"
)
type RPLaunchService service
func (s *RPLaunchService) Import(ctx context.Context, projectName string, importFile string, launchName string) (*RPLaunchResponse, error) {
file, err := os.Open(importFile)
if err != nil {
return nil, err
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
return nil, err
}
fileName := fi.Name()
if launchName != "" {
fileName = launchName + ".zip"
}
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(importFileFieldName, fileName)
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
if err != nil {
return nil, err
}
writer.WriteField("projectName", projectName)
err = writer.Close()
if err != nil {
return nil, err
}
u := fmt.Sprintf("%s/launch/import", projectName)
req, err := s.client.NewRequest("POST", u, body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", writer.FormDataContentType())
launchResp := &RPLaunchResponse{}
_, err = s.client.Do(ctx, req, launchResp)
if err != nil {
return nil, err
}
return launchResp, nil
}
func (s *RPLaunchService) Update(ctx context.Context, projectName string, launchId int, input *RPLaunchUpdateInput) (*RPLaunchResponse, error) {
u := fmt.Sprintf("%s/launch/%d/update", projectName, launchId)
req, err := s.client.NewRequest("PUT", u, input)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
launchResp := &RPLaunchResponse{}
_, err = s.client.Do(ctx, req, launchResp)
if err != nil {
return nil, err
}
return launchResp, nil
}
func (s *RPLaunchService) Get(ctx context.Context, projectName string, launchUuid string) (*RPLaunchDetailsResponse, error) {
u := fmt.Sprintf("%s/launch/uuid/%s", projectName, launchUuid)
req, err := s.client.NewRequest("GET", u, new(bytes.Buffer))
if err != nil {
return nil, err
}
launchResp := &RPLaunchDetailsResponse{}
_, err = s.client.Do(ctx, req, launchResp)
if err != nil {
return nil, err
}
return launchResp, nil
}