-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform3.go
160 lines (127 loc) · 3.87 KB
/
form3.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
package form3
import (
"strconv"
"encoding/json"
"errors"
guuid "github.com/google/uuid"
"github.com/parnurzeal/gorequest"
)
var BASE_API_URL string
type accounts struct {}
// main wrapper: in the future, it could support other resources
type Form3 struct {
Accounts accounts
}
type AccountAttributes struct {
Country string `json:"country"`
BaseCurrency string `json:"base_currency"`
BankId string `json:"bank_id"`
BankIdCode string `json:"bank_id_code"`
AccountNumber string `json:"account_number"`
Bic string `json:"bic"`
Iban string `json:"iban"`
CustomerId string `json:"customer_id"`
Title string `json:"title"`
FirstName string `json:"first_name"`
BankAccountName string `json:"bank_account_name"`
AlternativeBankAccountNames []string `json:"alternative_bank_account_names"`
AccountClassification string `json:"account_classification"`
JointAccount bool `json:"joint_account"`
AccountMatchingOptOut bool `json:"account_matching_opt_out"`
SecondaryIdentification string `json:"secondary_identification"`
}
type Account struct {
Id string `json:"id"`
OrganisationId string `json:"organisation_id"`
Version int `json:"version"`
Attributes AccountAttributes `json:"attributes"`
}
type AccountListPagination struct {
Number int
Size int
}
// list params: in the future it could support filtering too
type AccountListParams struct {
Pagination AccountListPagination
}
// lib config: in the future it could support other config
type Form3Config struct {
Url string
}
type accountResponseBody struct {
Data Account `json:"data"`
}
type accountsResponseBody struct {
Data []Account `json:"data"`
}
// generic error handler, both for HTTP responses and gorequest errors
func handleResponseError(res gorequest.Response, responseBody []byte, errs []error) error {
if len(errs) > 0 {
return errs[0]
}
if res.StatusCode > 399 {
var responseMap map[string]string
if err := json.Unmarshal(responseBody, &responseMap); err != nil {
return err
}
return errors.New(responseMap["error_message"])
}
return nil
}
func New(config *Form3Config) *Form3 {
if config.Url != "" {
BASE_API_URL = config.Url
} else {
// this should be converted into the prod URL
BASE_API_URL = "http://localhost:8080/v1"
}
f := &Form3{}
return f
}
func (a *accounts) Create(organisationId string, attributes *AccountAttributes) (Account, error) {
var body accountResponseBody
accountId := guuid.New().String()
payload := map[string]interface{}{
"data": map[string]interface{}{
"id": accountId,
"organisation_id": organisationId,
"type": "accounts",
"attributes": attributes,
},
}
request := gorequest.New()
res, responseBody, errs := request.Post(BASE_API_URL + "/organisation/accounts").
Send(payload).
EndStruct(&body)
err := handleResponseError(res, responseBody, errs)
return body.Data, err
}
func (a *accounts) List(params *AccountListParams) ([]Account, error) {
var body accountsResponseBody
query := map[string]int{
"page[number]": params.Pagination.Number,
"page[size]": params.Pagination.Size,
}
request := gorequest.New()
res, responseBody, errs := request.Get(BASE_API_URL + "/organisation/accounts").
Query(query).
EndStruct(&body)
err := handleResponseError(res, responseBody, errs)
return body.Data, err
}
func (a *accounts) Fetch(accountId string) (Account, error) {
var body accountResponseBody
request := gorequest.New()
res, responseBody, errs := request.Get(BASE_API_URL + "/organisation/accounts/" + accountId).
EndStruct(&body)
err := handleResponseError(res, responseBody, errs)
return body.Data, err
}
func (a *accounts) Delete(accountId string, version int) error {
request := gorequest.New()
res, responseBody, errs := request.Delete(BASE_API_URL + "/organisation/accounts/" + accountId).
Query("version=" + strconv.Itoa(version)).
End()
err := handleResponseError(res, []byte(responseBody), errs)
return err
}