-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclient.go
More file actions
268 lines (227 loc) · 6.45 KB
/
client.go
File metadata and controls
268 lines (227 loc) · 6.45 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package blnkgo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"github.com/google/go-querystring/query"
)
type Client struct {
ApiKey *string
BaseURL *url.URL
options Options
client *http.Client
Ledger *LedgerService
LedgerBalance *LedgerBalanceService
Transaction *TransactionService
BalanceMonitor *BalanceMonitorService
Identity *IdentityService
Search *SearchService
Reconciliation *ReconciliationService
Metadata *MetadataService
}
// create a client interface
type ClientInterface interface {
NewRequest(endpoint, method string, opt interface{}) (*http.Request, error)
CallWithRetry(req *http.Request, resBody interface{}) (*http.Response, error)
NewFileUploadRequest(endpoint string, fileParam string, file interface{}, fileName string, fields map[string]string) (*http.Request, error)
}
type service struct {
client ClientInterface
}
type Options struct {
RetryCount int
Timeout time.Duration
Logger Logger
}
func DefaultOptions() Options {
return Options{
RetryCount: 1,
Timeout: time.Second * 10,
Logger: NewDefaultLogger(),
}
}
func NewClient(baseURL *url.URL, apiKey *string, opts ...ClientOption) *Client {
//if base url is nil or empty, return error
if baseURL == nil || baseURL.String() == "" {
panic(errors.New("base url is required"))
}
//check if base url ends with a "/", if it doesnt append it
if baseURL.String()[len(baseURL.String())-1:] != "/" {
baseURL.Path += "/"
}
//set default options if not provided
client := &Client{
ApiKey: apiKey,
BaseURL: baseURL,
options: DefaultOptions(),
client: &http.Client{Timeout: 10 * time.Second},
}
//apply options
for _, opt := range opts {
opt(client)
//if options.timeout is set, update the client.client timeout
if client.options.Timeout != 0 {
client.client.Timeout = client.options.Timeout
}
if client.options.RetryCount == 0 {
client.options.RetryCount = 1
}
}
//initialize services
client.Ledger = &LedgerService{client: client}
client.LedgerBalance = &LedgerBalanceService{client: client}
client.Transaction = &TransactionService{client: client}
client.BalanceMonitor = &BalanceMonitorService{client: client}
client.Identity = &IdentityService{client: client}
client.Search = &SearchService{client: client}
client.Reconciliation = &ReconciliationService{client: client}
client.Metadata = &MetadataService{client: client}
return client
}
func (c *Client) SetBaseURL(baseURL *url.URL) {
c.BaseURL = baseURL
}
func (c *Client) NewRequest(endpoint, method string, opt interface{}) (*http.Request, error) {
//creates and returns a new HTTP request
//endpoint is the API endpoint
//method is the HTTP method
//opt is the request body
//returns the request and an error if any
u, err := url.Parse(c.BaseURL.String() + endpoint)
if err != nil {
return nil, err
}
//if method is get and opt is not nil, add query params to the url
if method == http.MethodGet && opt != nil {
q, err := query.Values(opt)
if err != nil {
return nil, err
}
u.RawQuery = q.Encode()
}
var bodyBuf io.ReadWriter
if method != http.MethodGet && opt != nil {
bodyBuf = new(bytes.Buffer)
err := json.NewEncoder(bodyBuf).Encode(opt)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), bodyBuf)
if err != nil {
return nil, err
}
//if c has api key, add it to the header
if c.ApiKey != nil {
req.Header.Add("X-Blnk-Key", *c.ApiKey)
}
req.Header.Add("Content-Type", "application/json")
return req, nil
}
// to:Do implement retry strategies
func (c *Client) CallWithRetry(req *http.Request, resBody interface{}) (*http.Response, error) {
retryCount := c.options.RetryCount
var resp *http.Response
var err error
for i := 0; i < retryCount; i++ {
resp, err = c.client.Do(req)
if err != nil {
c.options.Logger.Info(err.Error())
time.Sleep(time.Second * 2)
continue
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
logString := fmt.Sprintf("Request failed with status code %v and Status %v", resp.StatusCode, resp.Status)
c.options.Logger.Error(logString)
time.Sleep(time.Second * 2)
continue
}
//check resp
err = c.DecodeResponse(resp, resBody)
if err != nil {
c.options.Logger.Error(err.Error())
return resp, err
}
return resp, nil
}
return nil, errors.New("max retry count exceeded")
}
// decode response, this function will take in a response, and an interface it'll then decode the response body into the interface
// before that it will call checkResponse to check if the response is valid
// the function returns 2 values, the interface and an error if any
// the value passed should be a pointer to a struct
func (c *Client) DecodeResponse(resp *http.Response, v interface{}) error {
err := c.CheckResponse(resp)
if err != nil {
return err
}
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
return err
}
return nil
}
func (c *Client) NewFileUploadRequest(endpoint string, fileParam string, file interface{}, fileName string, fields map[string]string) (*http.Request, error) {
// Prepare multipart form data
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// Add file to the form
var fileReader io.Reader
switch v := file.(type) {
case string: // File path
openedFile, err := os.Open(v)
if err != nil {
return nil, err
}
defer openedFile.Close()
fileReader = openedFile
if fileName == "" {
fileName = filepath.Base(v)
}
case io.Reader: // Read stream
fileReader = v
// Default file name
if fileName == "" {
fileName = "upload"
}
default:
return nil, fmt.Errorf("unsupported file input type")
}
part, err := writer.CreateFormFile(fileParam, fileName)
if err != nil {
return nil, err
}
if _, err := io.Copy(part, fileReader); err != nil {
return nil, err
}
// Add additional form fields
for key, value := range fields {
if err := writer.WriteField(key, value); err != nil {
return nil, err
}
}
if err := writer.Close(); err != nil {
fmt.Println("in error", err)
return nil, err
}
// Create the HTTP request
req, err := http.NewRequest(http.MethodPost, c.BaseURL.ResolveReference(&url.URL{Path: endpoint}).String(), io.NopCloser(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
//print out the file type
if c.ApiKey != nil {
req.Header.Add("X-Blnk-Key", *c.ApiKey)
}
return req, nil
}