forked from philchia/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
43 lines (34 loc) 路 776 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
package agollo
import (
"errors"
"io"
"io/ioutil"
"net/http"
)
var ErrorStatusNotOK = errors.New("http resp code not ok")
// this is a static check
var _ requester = (*httprequester)(nil)
type requester interface {
request(url string) ([]byte, error)
}
type httprequester struct {
client *http.Client
}
func newHTTPRequester(client *http.Client) requester {
return &httprequester{
client: client,
}
}
func (r *httprequester) request(url string) ([]byte, error) {
resp, err := r.client.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return ioutil.ReadAll(resp.Body)
}
// Diacard all body if status code is not 200
_, _ = io.Copy(ioutil.Discard, resp.Body)
return nil, ErrorStatusNotOK
}