forked from DNS-OARC/ripeatlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
204 lines (184 loc) · 5.69 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
// Author Jerry Lundström <[email protected]>
// Copyright (c) 2017, OARC, Inc.
// All rights reserved.
//
// This file is part of ripeatlas.
//
// ripeatlas is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ripeatlas is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with ripeatlas. If not, see <http://www.gnu.org/licenses/>.
package ripeatlas
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/DNS-OARC/ripeatlas/measurement"
)
// A Http reads RIPE Atlas data from the RIPE Atlas REST API.
type Http struct {
}
const (
MeasurementsUrl = "https://atlas.ripe.net/api/v2/measurements"
)
// NewHttp returns a new Atlaser for reading from the RIPE Atlas REST API.
func NewHttp() *Http {
return &Http{}
}
func (h *Http) get(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, url.PathEscape(pk))
if fragmented {
url += "?format=txt"
} else {
url += "?format=json"
}
return h.get(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, url.PathEscape(pk))
if fragmented {
url += "?format=txt"
} else {
url += "?format=json"
}
if len(qstr) > 0 {
url += "&" + strings.Join(qstr, "&")
}
return h.get(url, fragmented)
}