This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontacts.go
121 lines (100 loc) · 3.23 KB
/
contacts.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
package starling
import (
"context"
"net/http"
"path"
)
// Contact represents the details of a payee
type Contact struct {
UID string `json:"id"`
Name string `json:"name"`
}
// Contacts are a list of payees
type contacts struct {
Contacts []Contact
}
// HALContacts is a HAL wrapper around the Contacts type.
type halContacts struct {
Links struct{} `json:"_links"`
Embedded *contacts `json:"_embedded"`
}
// ContactAccount holds payee account details
type ContactAccount struct {
UID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
AccountNumber string `json:"accountNumber"`
SortCode string `json:"sortCode"`
}
// ContactAccounts holds a list of accounts for a payee
type contactAccounts struct {
ContactAccounts []ContactAccount `json:"contactAccounts"`
}
// Contacts returns the contacts for the current customer.
func (c *Client) Contacts(ctx context.Context) ([]Contact, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/contacts", nil)
if err != nil {
return nil, nil, err
}
var halWrapper *halContacts
resp, err := c.Do(ctx, req, &halWrapper)
if err != nil {
return nil, resp, err
}
contactWrapper := new(contacts)
if halWrapper.Embedded != nil {
contactWrapper = halWrapper.Embedded
}
return contactWrapper.Contacts, resp, nil
}
// Contact returns an individual contact for the current customer.
func (c *Client) Contact(ctx context.Context, uid string) (*Contact, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/contacts/"+uid, nil)
if err != nil {
return nil, nil, err
}
contact := new(Contact)
resp, err := c.Do(ctx, req, contact)
return contact, resp, err
}
// DeleteContact deletes an individual contact for the current customer. It returns http.StatusNoContent
// on success. No payload is returned.
func (c *Client) DeleteContact(ctx context.Context, uid string) (*http.Response, error) {
req, err := c.NewRequest("DELETE", "/api/v1/contacts/"+uid, nil)
if err != nil {
return nil, err
}
resp, err := c.Do(ctx, req, nil)
return resp, err
}
// ContactAccounts returns the accounts for a given contact.
func (c *Client) ContactAccounts(ctx context.Context, uid string) ([]ContactAccount, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/contacts/"+uid+"/accounts", nil)
if err != nil {
return nil, nil, err
}
var cas *contactAccounts
resp, err := c.Do(ctx, req, &cas)
return cas.ContactAccounts, resp, err
}
// ContactAccount returns the specified account for a given contact.
func (c *Client) ContactAccount(ctx context.Context, cUID, aUID string) (*ContactAccount, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/contacts/"+cUID+"/accounts/"+aUID, nil)
if err != nil {
return nil, nil, err
}
var ca *ContactAccount
resp, err := c.Do(ctx, req, &ca)
return ca, resp, nil
}
// CreateContactAccount creates the specified account for a given contact.
func (c *Client) CreateContactAccount(ctx context.Context, ca ContactAccount) (string, *http.Response, error) {
req, err := c.NewRequest("POST", "/api/v1/contacts", ca)
if err != nil {
return "", nil, err
}
resp, err := c.Do(ctx, req, nil)
loc := resp.Header.Get("Location")
uid := path.Base(loc)
return uid, resp, err
}