-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinapi.go
72 lines (64 loc) · 1.75 KB
/
finapi.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
package finapi
import (
"context"
"net/http"
"time"
)
// ClientConfig holds the configuration values for the finAPI client.
type ClientConfig struct {
Endpoint string
ClientID string
ClientSecret string
}
// Client bundles the different API services and holds the configuration.
type Client struct {
*APIClient
clientID string
clientSecret string
}
// NewClient creates a new finAPI client.
func NewClient(cfg ClientConfig) *Client {
apiClient := NewAPIClient(&Configuration{
Servers: ServerConfigurations{
{
URL: cfg.Endpoint,
},
},
HTTPClient: &http.Client{
Timeout: 55 * time.Second,
},
})
return &Client{
APIClient: apiClient,
clientID: cfg.ClientID,
clientSecret: cfg.ClientSecret,
}
}
// NewClientContext creates a new context for general client actions like creating users.
func (c *Client) NewClientContext() (context.Context, error) {
token, _, err := c.AuthorizationApi.GetToken(context.Background()).
GrantType("client_credentials").
ClientId(c.clientID).
ClientSecret(c.clientSecret).
Execute()
if err != nil {
return nil, err
}
ctx := context.WithValue(context.Background(), ContextAccessToken, token.AccessToken)
return ctx, nil
}
// NewUserContext creates a context for user based actions like connection new banks, gettig transactions etc.
func (c *Client) NewUserContext(finAPIUserID string, password string) (context.Context, error) {
token, _, err := c.AuthorizationApi.GetToken(context.Background()).
GrantType("password").
ClientId(c.clientID).
ClientSecret(c.clientSecret).
Username(finAPIUserID).
Password(password).
Execute()
if err != nil {
return nil, err
}
ctx := context.WithValue(context.Background(), ContextAccessToken, token.AccessToken)
return ctx, nil
}