-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
111 lines (94 loc) · 2.65 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
package wazirx
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/pkg/errors"
)
const (
// DefaultBaseURL is the default server URL.
DefaultBaseURL = "https://api.wazirx.com"
)
// withCtx applies 'ctx' to the the http.Request and returns *http.Request
// The provided ctx and req must be non-nil
func withCtx(ctx context.Context, req *http.Request) *http.Request {
if req == nil {
panic("nil http.Request")
}
return req.WithContext(ctx)
}
// Client for accessing different endpoints of the API
type Client struct {
// HTTPClient is a reusable http client instance.
HTTP *http.Client
// BaseURL is the REST endpoints URL of the api server
BaseURL *url.URL
}
// makeGetRequest generates HTTP GET request and calls Do func
func (c Client) makeGetRequest(ctx context.Context, endpoint string, target interface{}) error {
r, err := http.NewRequest(http.MethodGet, DefaultBaseURL+endpoint, nil)
if err != nil {
return errors.Wrap(err, "could not generate http request")
}
if err = c.Do(withCtx(ctx, r), target); err != nil {
return errors.Wrap(err, "request failed")
}
return nil
}
// Do sends the http.Request and unmarshalls the JSON response into 'target'
func (c Client) Do(req *http.Request, target interface{}) error {
if req == nil {
return errors.New("invalid Request")
}
if c.HTTP == nil {
c.HTTP = http.DefaultClient
c.HTTP.Transport = http.DefaultTransport
c.HTTP.Timeout = 15 * time.Second
}
if c.BaseURL != nil {
req.URL.Scheme = c.BaseURL.Scheme
req.URL.Host = c.BaseURL.Host
}
// make request to the api and read the response
resp, err := c.HTTP.Do(req)
if err != nil {
return errors.Wrap(err, "request failed")
}
if resp.StatusCode != http.StatusOK {
return ErrAPI{resp}
}
defer func() {
// Ensure the response body is fully read and closed
// before we reconnect, so that we reuse the same TCPconnection.
const maxBodySlurpSize = 2 << 10
if resp.ContentLength == -1 || resp.ContentLength <= maxBodySlurpSize {
io.CopyN(ioutil.Discard, resp.Body, maxBodySlurpSize)
}
resp.Body.Close()
}()
var buf bytes.Buffer
return json.NewDecoder(io.TeeReader(resp.Body, &buf)).Decode(target)
}
// ErrAPI is returned by API calls when the response status code isn't 200.
type ErrAPI struct {
// Response from the request which returned error.
Response *http.Response
}
// Error implements the error interface.
func (err ErrAPI) Error() (errStr string) {
if err.Response != nil {
errStr += fmt.Sprintf(
"request to %s returned %d (%s)",
err.Response.Request.URL,
err.Response.StatusCode,
http.StatusText(err.Response.StatusCode),
)
}
return errStr
}