-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
124 lines (110 loc) · 2.91 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
package destiny
import (
"encoding/json"
"io"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
)
var Debug = false
type Client struct {
*http.Client
key string
}
// Get issues a GET to the specified URL.
func (c *Client) Get(url string) (resp *http.Response, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// Head issues a HEAD to the specified URL.
func (c *Client) Head(url string) (resp *http.Response, err error) {
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// Post issues a POST to the specified URL.
func (c *Client) Post(url string, bodyType string, body io.Reader) (resp *http.Response, err error) {
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", bodyType)
return c.Do(req)
}
// PostForm issues a POST to the specified URL,
// with data's keys and values URL-encoded as the request body.
func (c *Client) PostForm(url string, data url.Values) (resp *http.Response, err error) {
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}
// Do sends an HTTP request and returns an HTTP response, following
// policy (such as redirects, cookies, auth) as configured on the
// client.
func (c *Client) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("X-API-Key", c.key)
req.Header.Set("User-Agent", "github.com/FederationOfFathers/lib-destiny")
rsp, err := c.Client.Do(req)
if Debug {
buf, _ := httputil.DumpRequest(req, true)
os.Stderr.Write(buf)
buf2, _ := httputil.DumpResponse(rsp, true)
os.Stderr.Write(buf2)
}
return rsp, err
}
// New returns a new Client which is an http.Client which embeds the
// API Key and User Agent into all its requests automatically
func New(APIKey string, httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
return &Client{
Client: httpClient,
key: APIKey,
}
}
func (c *Client) GetAndUnwrap(url string, into interface{}) (bool, error) {
return c.getAndUnwrap(url, into)
}
func (c *Client) getAndUnwrap(url string, into interface{}) (bool, error) {
rsp, err := c.Get(url)
defer rsp.Body.Close()
if err != nil {
return false, err
}
var e *envelope
err = json.NewDecoder(rsp.Body).Decode(&e)
if err != nil {
return false, err
}
if !e.success() {
return false, e
}
return true, e.into(&into)
}
func (c *Client) GetAndUnwrapData(url string, into interface{}) (bool, error) {
return c.getAndUnwrapData(url, into)
}
func (c *Client) getAndUnwrapData(url string, into interface{}) (bool, error) {
rsp, err := c.Get(url)
defer rsp.Body.Close()
if err != nil {
return false, err
}
var e *envelope
err = json.NewDecoder(rsp.Body).Decode(&e)
if err != nil {
return false, err
}
data, err := e.data()
if err != nil {
return false, err
}
return true, data.into(&into)
}