forked from hakluke/haktrails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.go
58 lines (51 loc) · 1.72 KB
/
utilities.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
)
// get a response from an endpoint as a string
func getResponse(method string, url string, postBody string) string {
for {
response, status := tryRequest(method, url, postBody)
errorEncountered, message := checkJSONError(response, status)
if errorEncountered {
return "JSON error: " + message
} else {
return string(response)
}
}
}
func tryRequest(method string, url string, postBody string) (string, int) {
client := &http.Client{}
url = apiEndpoint + url
req, err := http.NewRequest(method, url, strings.NewReader(postBody))
if err != nil {
log.Println("Error creating request:", err)
return "", 0
}
req.Header.Set("apikey", config.SecurityTrails.Key)
resp, err := client.Do(req)
if err != nil {
log.Println("Error retrieving:", url, err)
return "", 0
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Error reading request body.", err)
return "", 0
}
return string(body), resp.StatusCode
}
// Checks for {"message":"API rate limit exceeded"} or similar
func checkJSONError(body string, status int) (bool, string) {
var results map[string]string
err := json.Unmarshal([]byte(body), &results)
if (err != nil && status == 200) {
return false, ""
}
return true, results["message"]
}