-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
http.go
353 lines (318 loc) · 9.48 KB
/
http.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package ripeatlas
import (
"encoding/json"
"fmt"
"io"
"net/http"
neturl "net/url"
"strings"
"github.com/DNS-OARC/ripeatlas/measurement"
"github.com/DNS-OARC/ripeatlas/request"
)
// A Http reads RIPE Atlas data from the RIPE Atlas REST API.
type Http struct {
}
const (
MeasurementsUrl = "https://atlas.ripe.net/api/v2/measurements"
ProbesUrl = "https://atlas.ripe.net/api/v2/probes"
)
// NewHttp returns a new Atlaser for reading from the RIPE Atlas REST API.
func NewHttp() *Http {
return &Http{}
}
// Measurements gets the metadata of measurements, as described
// by the Params, and sends them to the returned channel.
//
// Params available are:
//
// "page": int64 - The pagination page to read (default 1).
//
// "pk": string - The measurement id to read a specific measurement.
func (h *Http) Measurements(p Params) (<-chan *Measurement, error) {
var qstr []string
var pk string
for k, v := range p {
switch k {
case "page":
v, ok := v.(int64)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be int64", k)
}
qstr = append(qstr, fmt.Sprintf("%s=%d", k, v))
case "pk":
v, ok := v.(string)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be string", k)
}
pk = v
default:
return nil, fmt.Errorf("Invalid parameter %s", k)
}
}
url := MeasurementsUrl
if pk != "" {
url = fmt.Sprintf("%s/%s", MeasurementsUrl, neturl.PathEscape(pk))
}
url += "?format=json"
if len(qstr) > 0 {
url += "&" + strings.Join(qstr, "&")
}
r, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("http.Get(%s): %s", url, err.Error())
}
ch := make(chan *Measurement)
go func() {
d := json.NewDecoder(r.Body)
defer r.Body.Close()
if pk != "" {
var m Measurement
if err := d.Decode(&m); err != nil {
if err != io.EOF {
m := &Measurement{ParseError: fmt.Errorf("json.Decode(%s): %s", url, err.Error())}
ch <- m
}
} else {
ch <- &m
}
close(ch)
return
}
var r struct {
Results []*Measurement `json:"results"`
}
if err := d.Decode(&r); err != nil {
if err != io.EOF {
m := &Measurement{ParseError: fmt.Errorf("json.Decode(%s): %s", url, err.Error())}
ch <- m
}
} else {
for _, i := range r.Results {
ch <- i
}
}
close(ch)
}()
return ch, nil
}
func (h *Http) getMeasurementResults(url string, fragmented bool) (<-chan *measurement.Result, error) {
r, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("http.Get(%s): %s", url, err.Error())
}
ch := make(chan *measurement.Result)
go func() {
d := json.NewDecoder(r.Body)
defer r.Body.Close()
if fragmented {
for {
var r measurement.Result
if err := d.Decode(&r); err == io.EOF {
break
} else if err != nil {
r.ParseError = fmt.Errorf("json.Decode(%s): %s", url, err.Error())
ch <- &r
break
}
ch <- &r
}
} else {
var r []*measurement.Result
if err := d.Decode(&r); err != nil {
if err != io.EOF {
r := &measurement.Result{ParseError: fmt.Errorf("json.Decode(%s): %s", url, err.Error())}
ch <- r
}
} else {
for _, i := range r {
ch <- i
}
}
}
close(ch)
}()
return ch, nil
}
// MeasurementLatest gets the latest measurement results, as described
// by the Params, and sends them to the returned channel.
//
// Params available are:
//
// "pk": string - The measurement id to read results from (required).
//
// "fragmented": bool - If true, use the fragmented/stream format when reading.
func (h *Http) MeasurementLatest(p Params) (<-chan *measurement.Result, error) {
var pk string
var fragmented bool
for k, v := range p {
switch k {
case "pk":
v, ok := v.(string)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be string", k)
}
pk = v
case "fragmented":
v, ok := v.(bool)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be bool", k)
}
fragmented = v
default:
return nil, fmt.Errorf("Invalid parameter %s", k)
}
}
if pk == "" {
return nil, fmt.Errorf("Required parameter pk missing")
}
url := fmt.Sprintf("%s/%s/latest", MeasurementsUrl, neturl.PathEscape(pk))
if fragmented {
url += "?format=txt"
} else {
url += "?format=json"
}
return h.getMeasurementResults(url, fragmented)
}
// MeasurementResults gets the measurement results, as described by the Params,
// and sends them to the returned channel.
//
// Params available are:
//
// "pk": string - The measurement id to read results from (required).
//
// "start": int64 - Get the results starting at the given UNIX timestamp.
//
// "stop": int64 - Get the results up to the given UNIX timestamp.
//
// "probe_ids": none - Unimplemented
//
// "anchors-only": none - Unimplemented
//
// "public-only": none - Unimplemented
//
// "fragmented": bool - If true, use the fragmented/stream format when reading.
func (h *Http) MeasurementResults(p Params) (<-chan *measurement.Result, error) {
var qstr []string
var pk string
var fragmented bool
for k, v := range p {
switch k {
case "pk":
v, ok := v.(string)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be string", k)
}
pk = v
case "start":
fallthrough
case "stop":
v, ok := v.(int64)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be int64", k)
}
qstr = append(qstr, fmt.Sprintf("%s=%d", k, v))
case "probe_ids":
fallthrough
case "anchors-only":
fallthrough
case "public-only":
return nil, fmt.Errorf("Unimplemented parameter %s", k)
case "fragmented":
v, ok := v.(bool)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be bool", k)
}
fragmented = v
default:
return nil, fmt.Errorf("Invalid parameter %s", k)
}
}
if pk == "" {
return nil, fmt.Errorf("Required parameter pk missing")
}
url := fmt.Sprintf("%s/%s/results", MeasurementsUrl, neturl.PathEscape(pk))
if fragmented {
url += "?format=txt"
} else {
url += "?format=json"
}
if len(qstr) > 0 {
url += "&" + strings.Join(qstr, "&")
}
return h.getMeasurementResults(url, fragmented)
}
// Probes gets the metadata of probes, as described by the Params, and sends
// them to the returned channel.
//
// Params available are:
//
// "page": int64 - The pagination page to read (default 1).
//
// "pk": string - The probe id to read a specific probe.
func (h *Http) Probes(p Params) (<-chan *request.Probe, error) {
var qstr []string
var pk string
for k, v := range p {
switch k {
case "page":
v, ok := v.(int64)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be int64", k)
}
qstr = append(qstr, fmt.Sprintf("%s=%d", k, v))
case "pk":
v, ok := v.(string)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be string", k)
}
pk = v
default:
return nil, fmt.Errorf("Invalid parameter %s", k)
}
}
url := ProbesUrl
if pk != "" {
url = fmt.Sprintf("%s/%s", ProbesUrl, neturl.PathEscape(pk))
}
url += "?format=json"
if len(qstr) > 0 {
url += "&" + strings.Join(qstr, "&")
}
r, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("http.Get(%s): %s", url, err.Error())
}
ch := make(chan *request.Probe)
go func() {
d := json.NewDecoder(r.Body)
defer r.Body.Close()
if pk != "" {
var p request.Probe
if err := d.Decode(&p); err != nil {
if err != io.EOF {
p := &request.Probe{ParseError: fmt.Errorf("json.Decode(%s): %s", url, err.Error())}
ch <- p
}
} else {
ch <- &p
}
close(ch)
return
}
var r struct {
Results []*request.Probe `json:"results"`
}
if err := d.Decode(&r); err != nil {
if err != io.EOF {
p := &request.Probe{ParseError: fmt.Errorf("json.Decode(%s): %s", url, err.Error())}
ch <- p
}
} else {
for _, i := range r.Results {
ch <- i
}
}
close(ch)
}()
return ch, nil
}