-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
131 lines (101 loc) · 2.47 KB
/
Copy pathclient.go
File metadata and controls
131 lines (101 loc) · 2.47 KB
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
package splitwise
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"time"
)
const DefaultBaseUrl = "https://secure.splitwise.com"
const DefaultApiVersionPath = "/api/v3.0"
type jsonMarshaler func(interface{}) ([]byte, error)
type jsonUnmarshaler func([]byte, interface{}) error
type logger interface {
Printf(string, ...interface{})
}
type httpClient interface {
Do(*http.Request) (*http.Response, error)
}
type Client struct {
Logger logger
HttpClient httpClient
BaseUrl string
ApiVersionPath string
Token string
JsonMarshaler jsonMarshaler
JsonUnmarshaler jsonUnmarshaler
}
func (c *Client) baseUrl() string {
if c.BaseUrl == "" {
return DefaultBaseUrl
}
return c.BaseUrl
}
func (c *Client) apiVersionPath() string {
if c.ApiVersionPath == "" {
return DefaultApiVersionPath
}
return c.ApiVersionPath
}
func (c *Client) marshal() jsonMarshaler {
if c.JsonMarshaler == nil {
return json.Marshal
}
return c.JsonMarshaler
}
func (c *Client) unmarshal() jsonUnmarshaler {
if c.JsonUnmarshaler == nil {
return json.Unmarshal
}
return c.JsonUnmarshaler
}
func (c *Client) getHttpClient() httpClient {
if c.HttpClient == nil {
c.HttpClient = &http.Client{
Timeout: 10 * time.Second,
}
}
return c.HttpClient
}
func (c *Client) getLogger() logger {
if c.Logger == nil {
return log.Default()
}
return c.Logger
}
func (c *Client) addRequiredHeaders(req *http.Request) {
if c.Token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token))
}
req.Header.Add("Content-Type", "application/json")
}
func (c *Client) paramsToJsonBytesReader(params map[string]interface{}) (io.Reader, error) {
body, err := c.marshal()(params)
if err != nil {
return nil, err
}
return bytes.NewReader(body), nil
}
func (c *Client) do(ctx context.Context, method string, path string, queryParams url.Values, bodyParams map[string]interface{}) (*http.Response, error) {
path = c.baseUrl() + c.apiVersionPath() + path
endpointUrl, err := url.Parse(path)
if err != nil {
return nil, err
}
endpointUrl.RawQuery = queryParams.Encode()
bodyReader, err := c.paramsToJsonBytesReader(bodyParams)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, method, endpointUrl.String(), bodyReader)
if err != nil {
return nil, err
}
c.addRequiredHeaders(req)
c.getLogger().Printf("%s %s\n", method, endpointUrl.String())
return c.getHttpClient().Do(req)
}