-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_client.go
130 lines (102 loc) · 3.05 KB
/
account_client.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
package accountapi
import (
"fmt"
"github.com/go-resty/resty/v2"
uuid "github.com/satori/go.uuid"
"os"
"strconv"
)
const defaultBaseURL = "http://localhost:8080"
// AccountClient is used to invoke Form3 Accounts API.
type AccountClient struct {
client *resty.Client
}
// NewAccountClient returns a new instance of AccountClient.
func NewAccountClient() *AccountClient {
client := resty.New()
client.SetDebug(false)
// Try getting Accounts API base URL from env var
apiURL := os.Getenv("API_ADDR")
if apiURL == "" {
apiURL = defaultBaseURL
}
client.SetHostURL(apiURL)
// Setting global error struct that maps to Form3's error response
client.SetError(&Error{})
return &AccountClient{client: client}
}
// Create registers an existing account or creates a new one.
func (a *AccountClient) Create(accountParams AccountParams) (*Resource, error) {
resp, err := a.client.R().
SetResult(&Resource{}).
SetBody(map[string]AccountParams{"data": accountParams}).
Post("/v1/organisation/accounts")
if err != nil {
return nil, fmt.Errorf("create account failed: %s", err)
}
if resp.Error() != nil {
return nil, getAPIError(resp)
}
return resp.Result().(*Resource), nil
}
// List return a list of accounts.
func (a *AccountClient) List(paging PagingParams) (*Resources, error) {
r := a.client.R().SetResult(&Resources{})
if paging.number != "" {
r.SetQueryParam("page[number]", paging.number)
}
if paging.size != "" {
r.SetQueryParam("page[size]", paging.size)
}
resp, err := r.Get("/v1/organisation/accounts")
if err != nil {
return nil, fmt.Errorf("list accounts failed: %s", err)
}
if resp.Error() != nil {
return nil, getAPIError(resp)
}
return resp.Result().(*Resources), nil
}
// Fetch gets a single account by ID
func (a *AccountClient) Fetch(id string) (*Resource, error) {
// Validate the account ID
_, err := uuid.FromString(id)
if err != nil {
return nil, fmt.Errorf("account ID must be a valid UUID")
}
resp, err := a.client.R().
SetResult(&Resource{}).
SetPathParams(map[string]string{"account.id": id}).
Get("/v1/organisation/accounts/{account.id}")
if err != nil {
return nil, fmt.Errorf("fetch account for ID %s failed: %s", id, err)
}
if resp.Error() != nil {
return nil, getAPIError(resp)
}
return resp.Result().(*Resource), nil
}
// Delete deletes an account by ID
func (a *AccountClient) Delete(id string, version int) error {
// Validate the account ID
_, err := uuid.FromString(id)
if err != nil {
return fmt.Errorf("account ID must be a valid UUID")
}
resp, err := a.client.R().
SetPathParams(map[string]string{"account.id": id}).
SetQueryParam("version", strconv.Itoa(version)).
Delete("/v1/organisation/accounts/{account.id}")
if err != nil {
return fmt.Errorf("delete account for ID %s failed: %s", id, err)
}
if resp.Error() != nil {
return getAPIError(resp)
}
return nil
}
// Convert error response into error message
func getAPIError(resp *resty.Response) error {
apiError := resp.Error().(*Error)
return fmt.Errorf("request failed [%s]: %s", apiError.Code, apiError.Message)
}