This repository was archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform3api.go
More file actions
97 lines (86 loc) · 2.39 KB
/
form3api.go
File metadata and controls
97 lines (86 loc) · 2.39 KB
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
package form3api
import (
"context"
"fmt"
"net/http"
"strconv"
)
// Client communicates with Fake Form 3 Account API
type Client struct {
baseURL string
client HTTPClient
}
// NewClient creates a new Fake Form 3 Account API client.
//
// If httpClient is nil, then built-in http client will be used.
func NewClient(httpClient HTTPClient, baseURL string) *Client {
if httpClient == nil {
httpClient = &http.Client{}
}
return &Client{
client: httpClient,
baseURL: baseURL,
}
}
// CreateAccount creates an account.
//
// Form 3 API docs: https://api-docs.form3.tech/api.html?shell#organisation-accounts-create
func (c *Client) CreateAccount(ctx context.Context, r CreateAccount) (*Account, error) {
var acc *Account
err := NewRequest().
WithClient(c.client).
WithBaseURL(c.baseURL+"/organisation/accounts").
WithMethod(http.MethodPost).
Exec(ctx, r, &acc)
if err != nil {
return nil, err
}
return acc, nil
}
// CreateAccount creates an account.
//
// Form 3 API docs: https://api-docs.form3.tech/api.html?shell#organisation-accounts-fetch
func (c *Client) FetchAccount(ctx context.Context, r FetchAccount) (*Account, error) {
var acc *Account
err := NewRequest().
WithClient(c.client).
WithBaseURL(c.baseURL+"/organisation/accounts/"+r.AccountID).
Exec(ctx, nil, &acc)
if err != nil {
return nil, err
}
return acc, nil
}
// DeleteAccount deletes an account.
//
// Form 3 API docs: https://api-docs.form3.tech/api.html?shell#organisation-accounts-delete
func (c *Client) DeleteAccount(ctx context.Context, r DeleteAccount) error {
err := NewRequest().
WithClient(c.client).
WithMethod(http.MethodDelete).
WithBaseURL(c.baseURL+"/organisation/accounts/"+r.AccountID+"?version="+strconv.Itoa(int(r.Version))).
Exec(ctx, nil, nil)
return err
}
// CreateAccount creates an account.
//
// Form 3 API docs: https://api-docs.form3.tech/api.html?shell#organisation-accounts-list
func (c *Client) ListAccounts(ctx context.Context, r ListAccounts) (*Accounts, error) {
const defaultPageSize = 100
if r.Page.Size == 0 {
r.Page.Size = defaultPageSize
}
url := fmt.Sprintf("%s/organisation/accounts?page[number]=%d&page[size]=%d",
c.baseURL, r.Page.Number, r.Page.Size)
res := &Accounts{
AccountData: make([]AccountData, r.Page.Size),
}
err := NewRequest().
WithClient(c.client).
WithBaseURL(url).
Exec(ctx, nil, &res)
if err != nil {
return nil, err
}
return res, nil
}