This repository has been archived by the owner on Mar 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbapi.go
289 lines (250 loc) · 7.34 KB
/
dbapi.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package dbapi
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
const (
// V1 corresponds to version v1 of the Deutsche Bank API.
V1 = "v1"
// version specifies the version of this package.
version = "0.5"
)
const (
// DefaultURL is the URL of the Deutsche Bank API which is used by default.
DefaultURL = "https://simulator-api.db.com/gw/dbapi/"
// DefaultVersion is the default API version to use.
DefaultVersion = V1
)
var (
// ErrInvalidClient is raised when the costum HTTP client is invalid (e.g.
// nil).
ErrInvalidClient = errors.New("Invalid http client")
// ErrInvalidURL is raised when the url couldn't be parsed by url.Parse().
ErrInvalidURL = errors.New("Invalid url")
)
// A Client manages communication with the Deutsche Bank API.
type Client struct {
client *http.Client
baseURL *url.URL
version Version
// Authentication
Authentication *AuthenticationService
// API Resources
Addresses *AddressesService
Accounts *AccountsService
Transactions *TransactionsService
UserInfo *UserInfoService
}
// A Response represents a http response from the Deutsche Bank API. It is a
// wrapper around the standard http.Response type.
type Response struct {
*http.Response
}
// Version is the API version.
type Version string
func (v Version) String() string {
return string(v)
}
// An Option serves as a 'functional parameter' which can be used to configure
// the behaviour of the API Client.
type Option func(c *Client) error
// SetClient specifies a custom http client that should be used to make requests.
// An error ErrInvalidClient is returned if the passed client is nil.
func SetClient(client *http.Client) Option {
return func(c *Client) error { return c.setClient(client) }
}
func (c *Client) setClient(client *http.Client) error {
if client == nil {
return ErrInvalidClient
}
c.client = client
return nil
}
// SetToken specifies the api token.
func SetToken(token string) Option {
return func(c *Client) error { return c.setToken(token) }
}
func (c *Client) setToken(token string) error {
c.Authentication.token = token
return nil
}
// SetURL specifies the base url to use. An error ErrInvalidURL is returned if
// the passed url string can't be parsed properly.
func SetURL(urlStr string) Option {
return func(c *Client) error { return c.setURL(urlStr) }
}
func (c *Client) setURL(urlStr string) error {
if len(urlStr) == 0 {
return ErrInvalidURL
}
// If there is no / at the end, add one.
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}
url, err := url.Parse(urlStr)
if err != nil {
return ErrInvalidURL
}
c.baseURL = url
return nil
}
// SetVersion specifies the api version to use.
func SetVersion(version Version) Option {
return func(c *Client) error { return c.setVersion(version) }
}
func (c *Client) setVersion(version Version) error {
c.version = version
return nil
}
// NewClient creates and returns a new api client. Options can be passed to
// configure the client.
func NewClient(options ...Option) (*Client, error) {
// Parse the DefaultURL. Panic if that fails because this means the default
// URL is invalid. But this should never happen because it is covered by
// tests :)
url, err := url.Parse(DefaultURL)
if err != nil {
panic(err)
}
// Create client with default settings.
c := &Client{
client: http.DefaultClient,
baseURL: url,
version: DefaultVersion,
}
c.Authentication = &AuthenticationService{}
c.Addresses = &AddressesService{client: c}
c.Accounts = &AccountsService{client: c}
c.Transactions = &TransactionsService{client: c}
c.UserInfo = &UserInfoService{client: c}
// Apply supplied options.
if err := c.Options(options...); err != nil {
return nil, err
}
return c, nil
}
// Call combines Client.NewRequest() and Client.Do() methodes to avoid code
// duplication.
//
// m is the HTTP method you want to call.
// u is the URL you want to call.
// b is the HTTP body.
// r is the HTTP response.
//
// For more information read https://github.com/google/go-github/issues/234
func (c *Client) Call(m, u string, b interface{}, r interface{}) (*Response, error) {
req, err := c.NewRequest(m, u, b)
if err != nil {
return nil, err
}
resp, err := c.Do(req, r)
if err != nil {
return resp, err
}
return resp, err
}
// CheckResponse checks the API response for errors, and returns them if present.
// A response is considered an error if it has a status code outside the 200 range.
func CheckResponse(r *http.Response) error {
if c := r.StatusCode; 200 <= c && c <= 299 {
return nil
}
return fmt.Errorf("API call to %s failed: %s", r.Request.URL.String(), r.Status)
}
// Do sends an API request and returns the API response.
// The API response is JSON decoded and stored in the value pointed to by r, or
// returned as an error if an API error has occurred. If r implements the
// io.Writer interface, the raw response body will be written to r, without
// attempting to first decode it.
func (c *Client) Do(req *http.Request, r interface{}) (*Response, error) {
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Wrap response
response := &Response{Response: resp}
err = CheckResponse(resp)
if err != nil {
// Return response in case the caller wants to inspect it further.
return response, err
}
if r != nil {
if w, ok := r.(io.Writer); ok {
io.Copy(w, resp.Body)
} else {
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
// Return response in case the caller wants to inspect it further.
return response, err
}
}
}
return response, err
}
// NewRequest creates an API request.
// A relative URL can be provided in urlStr, in which case it is resolved
// relative to the baseURL of the Client. Relative URLs should always be
// specified without a preceding slash. If specified, the value pointed to by
// body is JSON encoded and included as the request body.
func (c *Client) NewRequest(m, urlStr string, body interface{}) (*http.Request, error) {
u, err := c.buildURLForRequest(urlStr)
if err != nil {
return nil, err
}
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
err = json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(m, u, buf)
if err != nil {
return nil, err
}
// Apply Authentication if credentials are present.
// Documentation: https://developer.db.com/#/apidocumentation/apiauthorizationguide
if c.Authentication.HasAuth() {
req.Header.Add("Authorization", "Bearer "+c.Authentication.Token())
}
// Add some important headers.
req.Header.Add("Accept", "application/json")
req.Header.Add("User-Agent", "dbapi/"+version)
return req, nil
}
// Options applies Options to a client instance.
func (c *Client) Options(options ...Option) error {
for _, option := range options {
if err := option(c); err != nil {
return err
}
}
return nil
}
// buildURLForRequest will build the URL (as string) that will be called. It
// does several cleaning tasks for us.
func (c *Client) buildURLForRequest(urlStr string) (string, error) {
u := c.baseURL.String() + c.version.String()
// If there is no / at the end, add one.
if !strings.HasSuffix(u, "/") {
u += "/"
}
// If there is a "/" at the start, remove it.
if strings.HasPrefix(urlStr, "/") {
urlStr = urlStr[1:]
}
rel, err := url.Parse(urlStr)
if err != nil {
return "", err
}
u += rel.String()
return u, nil
}