-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
134 lines (116 loc) · 3.11 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
package sendowl
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"golang.org/x/net/context"
)
// DefaultEndpoint is the default root API endpoint.
const DefaultEndpoint = "https://www.sendowl.com/api/v1/"
var defaultEndpointURL *url.URL
func init() {
var err error
defaultEndpointURL, err = url.Parse(DefaultEndpoint)
if err != nil {
panic(err)
}
}
var ErrNotFound = errors.New("sendowl: not found")
type ResponseNotJSONError struct {
ContentType string
Body []byte
}
func (e *ResponseNotJSONError) Error() string {
return fmt.Sprintf("sendowl: response is not JSON (got Content-Type %q)", e.ContentType)
}
// New creates a new Client which can be used to make requests to Sendowl
// services.
func New(key, secret string) *Client {
return &Client{
logger: log.New(ioutil.Discard, "", log.LstdFlags),
transportFunc: defaultTransportFunc,
key: key,
secret: secret,
endpoint: defaultEndpointURL,
}
}
type TransportFunc func(context.Context) http.RoundTripper
func defaultTransportFunc(ctx context.Context) http.RoundTripper {
return http.DefaultTransport
}
// Client is a type which makes requests to Sendowl.
type Client struct {
logger *log.Logger
transportFunc TransportFunc
key string
secret string
endpoint *url.URL `datastore:"-"`
}
func (c *Client) WithLogger(l *log.Logger) *Client {
c.logger = l
return c
}
func (c *Client) WithTransportFunc(f TransportFunc) *Client {
c.transportFunc = f
return c
}
func (c *Client) WithEndpoint(e *url.URL) *Client {
c.endpoint = e
return c
}
func (c *Client) newRequest(method, refURL string, body io.Reader) (*http.Request, error) {
ref, err := url.Parse(refURL)
if err != nil {
return nil, err
}
r, err := http.NewRequest(method, c.endpoint.ResolveReference(ref).String(), body)
if err != nil {
return nil, err
}
r.SetBasicAuth(c.key, c.secret)
r.Header.Set("Accept", "application/json")
return r, nil
}
func (c *Client) do(ctx context.Context, r *http.Request, data interface{}) error {
c.logger.Printf("sendowl: %s %s (content-type: %q)", r.Method, r.URL, r.Header.Get("Content-Type"))
rawReq, err := httputil.DumpRequestOut(r, true)
if err != nil {
return err
}
c.logger.Printf("%s", rawReq)
resp, err := c.transportFunc(ctx).RoundTrip(r)
if err != nil {
return err
}
defer resp.Body.Close()
return c.decodeResponse(resp, data)
}
// decodeResponse decodes the response from Sendowl as JSON.
func (c *Client) decodeResponse(resp *http.Response, data interface{}) error {
body := &bytes.Buffer{}
r := io.TeeReader(resp.Body, body)
b, err := ioutil.ReadAll(r)
if err != nil {
return err
}
c.logger.Printf("%s", b)
ct := resp.Header.Get("Content-Type")
if !strings.HasPrefix(ct, "application/json") {
return &ResponseNotJSONError{ContentType: ct, Body: body.Bytes()}
}
if resp.StatusCode == http.StatusNotFound {
return ErrNotFound
}
if resp.StatusCode > 299 {
return fmt.Errorf("sendowl: call returned non-2xx status %d", resp.StatusCode)
}
return json.NewDecoder(body).Decode(data)
}