-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathclient.go
153 lines (127 loc) · 3.38 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
// testrail provides a go api for testrail
package testrail
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
// A Client stores the client informations
// and implement all the api functions
// to communicate with testrail
type Client struct {
url string
username string
password string
httpClient *http.Client
useBetaApi bool
}
// NewClient returns a new client
// with the given credential
// for the given testrail domain
func NewClient(url, username, password string, useBetaApi ...bool) (c *Client) {
_useBetaApi := false
if len(useBetaApi) > 0 {
_useBetaApi = useBetaApi[0]
}
return NewCustomClient(url, username, password, nil, _useBetaApi)
}
// NewClient returns a new client with
// with the given credential
// for the given testrail domain
// and custom http Client
func NewCustomClient(url, username, password string, customHttpClient *http.Client, useBetaApi ...bool) (c *Client) {
c = &Client{}
c.username = username
c.password = password
c.url = url
if !strings.HasSuffix(c.url, "/") {
c.url += "/"
}
c.url += "index.php?/api/v2/"
if customHttpClient != nil {
c.httpClient = customHttpClient
} else {
c.httpClient = &http.Client{}
}
if len(useBetaApi) > 0 {
c.useBetaApi = useBetaApi[0]
}
return
}
// sendRequest sends a request of type "method"
// to the url "client.url+uri" and with optional data "data"
// Returns an error if any and the optional data "v"
func (c *Client) sendRequest(method, uri string, data, v interface{}) error {
var body io.Reader
if data != nil {
jsonReq, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("marshaling data: %s", err)
}
body = bytes.NewBuffer(jsonReq)
}
req, err := http.NewRequest(method, c.url+uri, body)
if err != nil {
return err
}
req.SetBasicAuth(c.username, c.password)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
if c.useBetaApi {
req.Header.Add("x-api-ident", "beta")
}
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
jsonCnt, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("reading: %s", err)
}
if resp.StatusCode >= http.StatusBadRequest {
return fmt.Errorf("response: status: %q, body: %s", resp.Status, jsonCnt)
}
if v != nil {
err = json.Unmarshal(jsonCnt, v)
if err != nil {
return fmt.Errorf("unmarshaling response: %s", err)
}
}
return nil
}
type Links struct {
Next *string `json:"next"`
Prev *string `json:"prev"`
}
func (c *Client) sendRequestBeta(method, uri string, data, v interface{}, itemsKeyName string) error {
var wraperMap map[string]json.RawMessage
var returnItems []interface{}
var tempItems []interface{}
var links Links
err := c.sendRequest("GET", uri, nil, &wraperMap)
if err != nil {
return err
}
json.Unmarshal(wraperMap[itemsKeyName], &tempItems)
json.Unmarshal(wraperMap["_links"], &links)
returnItems = append(returnItems, tempItems...)
for err == nil && links.Next != nil {
nextUri := strings.TrimPrefix(*links.Next, "/api/v2/")
err = c.sendRequest("GET", nextUri, nil, &wraperMap)
if err == nil {
json.Unmarshal(wraperMap[itemsKeyName], &tempItems)
json.Unmarshal(wraperMap["_links"], &links)
returnItems = append(returnItems, tempItems...)
} else {
return err
}
}
jsonAll, _ := json.Marshal(returnItems)
json.Unmarshal(jsonAll, v)
return nil
}