-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
161 lines (127 loc) · 3.03 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
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
package gowup
import (
"bytes"
"encoding/hex"
"encoding/json"
"io/ioutil"
"net/http"
)
var (
apiEntryPoint string = "https://api.wheresitup.com/v4"
)
type Error struct {
msg string
}
func (e *Error) Error() string {
return e.msg
}
type Location struct {
Name string `json:"name"`
Title string `json:"title"`
City string `json:"location"`
State string `json:"state"`
Country string `json:"country"`
Lat string `json:"latitude"`
Lon string `json:"longitude"`
Continent string `json:"continent_name"`
}
type WIU struct {
Client string
Token string
}
func (api WIU) Locations() ([]Location, error) {
response, err := api.get("sources")
if err != nil {
return nil, err
}
var body map[string][]Location
if err := api.parse(response, &body); err != nil {
return nil, err
}
sources, ok := body["sources"]
if !ok {
return nil, &Error{msg: "Locations missing from response"}
}
return sources, nil
}
func (api WIU) Jobs() (map[string]JobSummary, error) {
response, err := api.get("jobs")
if err != nil {
return nil, err
}
var jobs map[string]JobSummary
if err := api.parse(response, &jobs); err != nil {
return nil, err
}
return jobs, nil
}
func (api WIU) Job(id string) (*Job, error) {
if _, err := hex.DecodeString(id); err != nil {
return nil, &Error{msg: "Invalid job ID '" + id + "': " + err.Error()}
}
response, err := api.get("jobs/" + id)
if err != nil {
return nil, err
}
job := &Job{}
if err := api.parse(response, job); err != nil {
return nil, err
}
return job, nil
}
func (api WIU) Submit(req *JobRequest) (string, error) {
if req == nil {
return "", &Error{msg: "Nothing to submit"}
}
response, err := api.post("jobs", req)
if err != nil {
return "", err
}
posted := map[string]string{}
if err := api.parse(response, &posted); err != nil {
return "", err
}
id, ok := posted["jobID"]
if !ok {
return "", &Error{msg: "Submission failed"}
}
return id, nil
}
func (api WIU) setHeaders(req *http.Request, headers map[string]string) {
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Auth", "Bearer "+api.Client+" "+api.Token)
for header, content := range headers {
req.Header.Add(header, content)
}
}
func (api WIU) parse(response *http.Response, body interface{}) error {
raw, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
defer response.Body.Close()
if err := json.Unmarshal(raw, body); err != nil {
return err
}
return nil
}
func (api WIU) get(endpoint string) (*http.Response, error) {
req, err := http.NewRequest("GET", apiEntryPoint+"/"+endpoint, nil)
if err != nil {
return nil, err
}
api.setHeaders(req, nil)
return http.DefaultClient.Do(req)
}
func (api WIU) post(endpoint string, data interface{}) (*http.Response, error) {
body, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", apiEntryPoint+"/"+endpoint, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
api.setHeaders(req, nil)
return http.DefaultClient.Do(req)
}