-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
46 lines (36 loc) · 821 Bytes
/
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
package simpleclient
import (
"fmt"
"net/http"
)
type Client struct {
client *http.Client
}
func NewClient() *Client {
return &Client{
client: &http.Client{},
}
}
func (c *Client) Transport(transport *http.Transport) {
c.client.Transport = transport
}
func (c *Client) GetRequest(url string) (*http.Request, error) {
return c.newRequest(http.MethodGet, url)
}
func (c *Client) newRequest(method, url string) (*http.Request, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
return req, nil
}
func (c *Client) Do(req *http.Request) (*Response, error) {
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Bad response status: %s", res.Status)
}
return NewResponse(res), nil
}