-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutil.go
133 lines (108 loc) · 3.33 KB
/
util.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
package dwolla
import (
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
)
var ErrNoID = errors.New("unable to extract ID")
// ACHDetails contains data sent to the bank account
type ACHDetails struct {
Destination Addenda `json:"destination,omitempty"`
Source Addenda `json:"source,omitempty"`
}
// TransferACHDetails is an alias of ACHDetails for backwards compatibility
type TransferACHDetails = ACHDetails
// Addenda is a transfer addenda
type Addenda struct {
Addenda TransferAddendaValues `json:"addenda,omitempty"`
}
// TransferAddenda is an alias of Addenda for backwards compatibility
type TransferAddenda = Addenda
// AddendaValues is the addenda values
type AddendaValues struct {
Values []string `json:"values,omitempty"`
}
// TransferAddendaValues is an alias of AddendaValues for backwards compatibility
type TransferAddendaValues = AddendaValues
// Address represents a street address
type Address struct {
Address1 string `json:"address1"`
Address2 string `json:"address2,omitempty"`
Address3 string `json:"address3,omitempty"`
City string `json:"city"`
StateProvinceRegion string `json:"stateProvinceRegion"`
PostalCode string `json:"postalCode,omitempty"`
Country string `json:"country"`
}
// Amount is a monetary value
type Amount struct {
Value string `json:"value"`
Currency Currency `json:"currency"`
}
// String returns a string representation of the amount
func (a Amount) String() string {
return fmt.Sprintf("%s %s", a.Value, a.Currency)
}
// Currency represents the monetary currency
type Currency string
const (
// USD is U.S. dollars
USD Currency = "usd"
)
// Clearing is a transfer clearing schedule
type Clearing struct {
Destination string `json:"destination,omitempty"`
Source string `json:"source,omitempty"`
}
// TransferClearing is an alias of Clearing for backwards compatiblity
type TransferClearing = Clearing
// MetaData represents key/value meta data
type MetaData map[string]interface{}
// Passport represents a passport
type Passport struct {
Number string `json:"number"`
Country string `json:"country"`
}
// IDFromHREF takes an HREF link and returns the ID at the end of the HREF.
// This is useful for processing webhooks where you have an HREF, but need
// to make calls using this SDK, which expects bare IDs.
//
// If the input HREF is malformed, or this function is unable to extract the ID,
// ErrNoID will be returned.
func IDFromHREF(href string) (string, error) {
lastIDX := strings.LastIndex(href, "/")
if lastIDX < 0 {
return "", ErrNoID
}
return href[lastIDX:], nil
}
type mockHTTPClient struct {
err error
res *http.Response
}
func (m *mockHTTPClient) Do(req *http.Request) (*http.Response, error) {
return m.res, m.err
}
func (m *mockHTTPClient) SetResponse(res *http.Response, err error) {
m.res, m.err = res, err
}
func newRedirectResponse(url string) *http.Response {
res := &http.Response{
Status: "302",
StatusCode: 302,
Header: http.Header{},
}
res.Header.Set("Location", url)
return res
}
func newMockClient(status int, file string) *Client {
f, _ := os.Open(file)
mr := &http.Response{Body: f, StatusCode: status}
mc := &mockHTTPClient{err: nil, res: mr}
c := NewWithHTTPClient("foobar", "barbaz", Sandbox, mc)
c.Token = &Token{ExpiresIn: 3600, startTime: time.Now()}
return c
}