-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpt.go
225 lines (158 loc) · 3.94 KB
/
wpt.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
// wpt.go
package wpt
import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"time"
"github.com/google/go-querystring/query"
)
const (
DefualtStatusPollingInterval = 5
DefaultTestTimeout = 30
)
var (
StatusPollingInterval time.Duration = DefualtStatusPollingInterval
TestTimeout time.Duration = DefaultTestTimeout
)
type ClientOptions struct {
URL *url.URL
APIKey string
}
type Client struct {
options *ClientOptions
httpClient *http.Client
}
type Test struct {
client *Client
options *TestOptions
Response *TestResponse
}
func NewClient(options *ClientOptions) (*Client, error) {
if options.URL == nil {
options.URL, _ = url.Parse(defaultURL)
}
client := &Client{
options: options,
httpClient: &http.Client{},
}
return client, nil
}
func (c *Client) NewTest(options *TestOptions) (*Test, error) {
return &Test{
client: c,
options: options,
}, nil
}
func (t *Test) Run() (*TestResponse, error) {
v, _ := query.Values(t.options)
if len(t.client.options.APIKey) > 0 {
v.Add("k", t.client.options.APIKey)
}
v.Add("f", "json")
resp, err := t.client.query(wptQueryRunTest, v)
var response TestResponse
if err != nil {
log.Println(err)
} else {
parseData(resp, v.Get("f"), &response)
}
t.Response = &response
return &response, err
}
func (t *Test) RunSync() {
t.Run()
t.monitor()
}
func (t *Test) monitor() {
for {
select {
default:
time.Sleep(StatusPollingInterval * time.Second)
status, _ := t.client.GetStatus(t.Response.Data.TestId)
log.Println(status.StatusCode)
switch status.StatusCode {
case wptStatusTestSuccess, wptStatusTestNotFound, wptStatusTestCancelled:
log.Println("Exiting")
return
}
}
}
}
func (c *Client) GetLocations() (*WPTLocations, error) {
v := url.Values{}
v.Add("f", "json")
var response WPTLocations
resp, err := c.query(wptQueryLocations, v)
if err == nil {
parseData(resp, v.Get("f"), &response)
}
return &response, err
}
func (c *Client) GetStatus(testId string) (*TestStatus, error) {
v := url.Values{}
v.Add("test", testId)
v.Add("f", "json")
resp, err := c.query(wptQueryTestStatus, v)
var testStatus TestStatus
parseData(resp, v.Get("f"), &testStatus)
return &testStatus, err
}
func (c *Client) GetResults(id string) (*WPTResults, error) {
v := url.Values{}
v.Add("test", id)
v.Add("f", "json")
resp, err := c.query(wptQueryTestResults, v)
var results WPTResults
parseData(resp, v.Get("f"), &results)
return &results, err
}
func (c *Client) GetTestHistory(days int, from string, filter string) (*WPTHistory, error) {
v := url.Values{}
v.Add("f", "csv")
v.Add("days", strconv.Itoa(days))
v.Add("all", "on")
if len(from) > 0 {
v.Add("from", from)
}
if len(filter) > 0 {
v.Add("filter", filter)
}
resp, _ := c.query(wptQueryTestHistory, v)
var history WPTHistory
err := parseData(resp, v.Get("f"), &history.Items)
return &history, err
}
func (c *Client) CancelTest(testId string) error {
v := url.Values{}
v.Add("test", testId)
v.Add("k", c.options.APIKey)
_, err := c.query(wptQueryCancelTest, v)
return err
}
func (c *Client) query(path string, values url.Values) (string, error) {
url := c.options.URL
url.Path = path
url.RawQuery = values.Encode()
req, err := http.NewRequest("GET", url.String(), nil)
log.Println("Making Request : ", url.String())
if err != nil {
return "", errCreateRequest
}
resp, err := c.httpClient.Do(req)
if err != nil {
return "", errQueryServer
}
if resp.StatusCode != http.StatusOK {
return "", errBadResponse
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errReadBody
}
//response = parse(body, values.Get("f"), response)
return string(body), nil
}