-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
214 lines (189 loc) · 4.97 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package httpclient
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"sync"
)
// Error is the custom error type returns from HTTP requests.
type Error struct {
Message string
StatusCode int
URL string
}
// Error returns the error message.
func (e *Error) Error() string {
return e.Message
}
// File represents a file.
type File struct {
// File name with no directory.
Name string
// Contents of the file.
Data []byte
}
// A Client is an HTTP client.
// It wraps net/http's client and add some methods for making HTTP request easier.
type httpClient struct {
client *http.Client
}
// New returns new client.
func New() *httpClient {
return &httpClient{client: &http.Client{}}
}
func (c *httpClient) err(resp *http.Response, message string) error {
if message == "" {
message = fmt.Sprintf("Get %s -> %d", resp.Request.URL.String(), resp.StatusCode)
}
return &Error{
Message: message,
StatusCode: resp.StatusCode,
URL: resp.Request.URL.String(),
}
}
// Get issues a GET to the specified URL. It returns an http.Response for further processing.
func (c *httpClient) Get(url string) (*http.Response, error) {
return c.client.Get(url)
}
// Bytes fetches the specified url and returns the response body as bytes.
func (c *httpClient) Bytes(url string) ([]byte, error) {
resp, err := c.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, c.err(resp, "")
}
p, err := ioutil.ReadAll(resp.Body)
return p, err
}
// String fetches the specified URL and returns the response body as a string.
func (c *httpClient) String(url string) (string, error) {
bytes, err := c.Bytes(url)
if err != nil {
return "", err
}
return string(bytes), nil
}
// Reader issues a GET request to a specified URL and returns an reader from the response body.
func (c *httpClient) Reader(url string) (io.ReadCloser, error) {
resp, err := c.Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
err = c.err(resp, "")
resp.Body.Close()
return nil, err
}
return resp.Body, nil
}
// JSON issues a GET request to a specified URL and unmarshal json data from the response body.
func (c *httpClient) JSON(url string, v interface{}) error {
resp, err := c.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return c.err(resp, "")
}
err = json.NewDecoder(resp.Body).Decode(v)
if _, ok := err.(*json.SyntaxError); ok {
err = c.err(resp, "JSON syntax error at "+url)
}
return err
}
// XML issues a GET request to a specified URL and unmarshal XML data from the response body.
func (c *httpClient) XML(url string, v interface{}) error {
resp, err := c.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return c.err(resp, "")
}
err = xml.NewDecoder(resp.Body).Decode(v)
return err
}
// Files downloads multiple files concurrency.
func (c *httpClient) Files(urls []string, files *[]File) error {
l := len(urls)
fs := make([]File, l)
ch := make(chan error, l)
var wg sync.WaitGroup
wg.Add(l)
for i, url := range urls {
go func(i int) {
defer wg.Done()
resp, err := c.Get(url)
if err != nil {
ch <- err
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var err error
err = c.err(resp, "")
ch <- err
return
}
fs[i].Data, err = ioutil.ReadAll(resp.Body)
if err != nil {
ch <- c.err(resp, err.Error())
return
}
ch <- nil
}(i)
}
wg.Wait()
for _ = range fs {
if err := <-ch; err != nil {
return err
}
}
*files = fs
return nil
}
// Download downloads multiple files concurrency.
func (c *httpClient) Download(urls []string, files *[]File) error {
return c.Files(urls, files)
}
var client = New()
// Get issues a GET to the specified URL. It returns an http.Response for further processing.
func Get(url string) (*http.Response, error) {
return client.Get(url)
}
// Bytes fetches the specified url and returns the response body as bytes.
func Bytes(url string) ([]byte, error) {
return client.Bytes(url)
}
// String fetches the specified URL and returns the response body as a string.
func String(url string) (string, error) {
return client.String(url)
}
// Reader issues a GET request to a specified URL and returns an reader from the response body.
func Reader(url string) (io.ReadCloser, error) {
return client.Reader(url)
}
// JSON issues a GET request to a specified URL and unmarshal json data from the response body.
func JSON(url string, v interface{}) error {
return client.JSON(url, v)
}
// XML issues a GET request to a specified URL and unmarshal xml data from the response body.
func XML(url string, v interface{}) error {
return client.JSON(url, v)
}
// Files downloads multiple files concurrency.
func Files(urls []string, files *[]File) error {
return client.Files(urls, files)
}
// Download downloads multiple files concurrency.
func Download(urls []string, files *[]File) error {
return client.Files(urls, files)
}