/
api.go
81 lines (68 loc) · 1.97 KB
/
api.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
package sn
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
)
type SpotterRequestBody struct {
AppID string `json:"id"`
SpotterIds []int `json:"markers"`
}
type SpotterResponseBody struct {
Spotters []Spotter `json:"positions"`
}
type Spotter struct {
LastReport string `json:"report_at,omitempty"`
Lat float64 `json:"lat,string,omitempty"`
Lon float64 `json:"lon,string,omitempty"`
Elev int `json:"elev,string,omitempty"`
Dir int `json:"dir,string,omitempty"`
Gps bool `json:"gps,omitempty"`
Callsign string `json:"callsign,omitempty"`
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
Ham string `json:"ham,omitempty"`
HamShow bool `json:"ham_show,omitempty"`
Freq string `json:"freq,omitempty"`
Note string `json:"note,omitempty"`
IM string `json:"im,omitempty"`
Twitter string `json:"twitter,omitempty"`
Web string `json:"web,omitempty"`
Unix int64 `json:"unix,string,omitempty"`
First string `json:"first,omitempty"`
Last string `json:"last,omitempty"`
Marker string `json:"marker,omitempty"`
}
const snAPIURL = "https://www.spotternetwork.org"
func GetSpotters(appID string, spotterIds []int) ([]Spotter, error) {
srBody := SpotterRequestBody{
AppID: appID,
SpotterIds: spotterIds,
}
data, err := json.Marshal(srBody)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", snAPIURL+"/positions", bytes.NewBuffer(data))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{Timeout: time.Second * 30}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
tmp := struct {
Spotters []Spotter `json:"positions"`
}{}
err = json.Unmarshal(body, &tmp)
return tmp.Spotters, nil
}