-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
53 lines (45 loc) 路 1.26 KB
/
client.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
package library
import (
"encoding/json"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
const (
ISSUE_MSG = " Please open up a Github issue to report this error! https://github.com/karl-cardenas-coding/disaster-cli"
)
var disasterClient = &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 10,
ResponseHeaderTimeout: 10 * time.Second,
IdleConnTimeout: 5 * time.Second,
DisableCompression: true,
ForceAttemptHTTP2: true,
},
}
func getJson(url string, target interface{}) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println("unable to create HTTP request")
}
req.Header = http.Header{
"Content-Type": []string{`application/json; charset=utf-8`},
"Accept-Encoding": []string{"gzip, deflate, br"},
"Accept": []string{"application/json"},
}
r, err := disasterClient.Do(req)
if err != nil {
log.WithFields(log.Fields{
"package": "cmd",
"file": "client.go",
"parent_function": "getJSON",
"function": "disasterClient.Get",
"error": err,
"data": url,
}).Error("Error conencting to NASA's API.", ISSUE_MSG)
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}