-
Notifications
You must be signed in to change notification settings - Fork 61
/
jsonclient.go
125 lines (101 loc) · 2.92 KB
/
jsonclient.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
package jsonclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// ContentType is the default mime type for JSON
const ContentType = "application/json"
// DefaultClient is the singleton instance of jsonclient using http.DefaultClient
var DefaultClient = NewClient()
// Get fetches url using GET and unmarshals into the passed response using DefaultClient
func Get(url string, response interface{}) error {
return DefaultClient.Get(url, response)
}
// Post sends request as JSON and unmarshals the response JSON into the supplied struct using DefaultClient
func Post(url string, request interface{}, response interface{}) error {
return DefaultClient.Post(url, request, response)
}
// Client is a JSON wrapper around http.Client
type client struct {
httpClient *http.Client
headers http.Header
indent string
}
// NewClient returns a new JSON Client using the default http.Client
func NewClient() Client {
return NewWithHTTPClient(http.DefaultClient)
}
// NewWithHTTPClient returns a new JSON Client using the specified http.Client
func NewWithHTTPClient(httpClient *http.Client) Client {
return &client{
httpClient: httpClient,
headers: http.Header{
"Content-Type": []string{ContentType},
},
}
}
// Headers return the default headers for requests
func (c *client) Headers() http.Header {
return c.headers
}
// Get fetches url using GET and unmarshals into the passed response
func (c *client) Get(url string, response interface{}) error {
res, err := c.httpClient.Get(url)
if err != nil {
return err
}
return parseResponse(res, response)
}
// Post sends request as JSON and unmarshals the response JSON into the supplied struct
func (c *client) Post(url string, request interface{}, response interface{}) error {
var err error
var body []byte
body, err = json.MarshalIndent(request, "", c.indent)
if err != nil {
return fmt.Errorf("error creating payload: %v", err)
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
for key, val := range c.headers {
req.Header.Set(key, val[0])
}
var res *http.Response
res, err = c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("error sending payload: %v", err)
}
return parseResponse(res, response)
}
func (c *client) ErrorResponse(err error, response interface{}) bool {
jerr, isJsonError := err.(Error)
if !isJsonError {
return false
}
return json.Unmarshal([]byte(jerr.Body), response) == nil
}
func parseResponse(res *http.Response, response interface{}) error {
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if res.StatusCode >= 400 {
err = fmt.Errorf("got HTTP %v", res.Status)
}
if err == nil {
err = json.Unmarshal(body, response)
}
if err != nil {
if body == nil {
body = []byte{}
}
return Error{
StatusCode: res.StatusCode,
Body: string(body),
err: err,
}
}
return nil
}