-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
48 lines (39 loc) · 1022 Bytes
/
request.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
package postmord
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func (c *Client) request(url string) (*Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", userAgent)
res, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() {
if err = res.Body.Close(); err != nil {
fmt.Printf("failed to close body: %s\n", err.Error())
}
}()
// according to swagger it only responds 400 or 200 - so check for != 200
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request failed: %s", res.Status)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := Response{}
if err = json.Unmarshal(b, &response); err != nil {
return nil, err
}
if faults := response.TrackingInformation.CompositeFault.Faults; len(faults) > 0 {
return &response, fmt.Errorf("%s: %s", faults[0].FaultCode, faults[0].ExplanationText)
}
return &response, nil
}