This repository was archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounts.go
95 lines (79 loc) · 2.17 KB
/
accounts.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
package sbanken
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/engvik/sbanken-go/internal/transport"
)
// Account represents an account.
// Sbanken API documentation: https://publicapi.sbanken.no/openapi/apibeta/index.html#/Accounts
type Account struct {
ID string `json:"accountId"`
Name string `json:"name"`
Type string `json:"accountType"`
Number string `json:"accountNumber"`
OwnerCustomerID string `json:"ownerCustomerId"`
Available float32 `json:"available"`
Balance float32 `json:"balance"`
CreditLimit float32 `json:"creditLimit"`
}
// ListAccounts lists the accounts.
func (c *Client) ListAccounts(ctx context.Context) ([]Account, error) {
url := fmt.Sprintf("%s/v2/Accounts", c.bankBaseURL)
res, sc, err := c.transport.Request(ctx, &transport.HTTPRequest{
Method: http.MethodGet,
URL: url,
})
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
data := struct {
Accounts []Account `json:"items"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.Accounts, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return nil, &Error{
"ListAccounts",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.Accounts, nil
}
// ReadAccount reads an account. The accountID are required.
func (c *Client) ReadAccount(ctx context.Context, accountID string) (Account, error) {
if accountID == "" {
return Account{}, ErrMissingAccountID
}
url := fmt.Sprintf("%s/v2/Accounts/%s", c.bankBaseURL, accountID)
res, sc, err := c.transport.Request(ctx, &transport.HTTPRequest{
Method: http.MethodGet,
URL: url,
})
if err != nil {
return Account{}, fmt.Errorf("request: %w", err)
}
data := struct {
Account Account `json:"item"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.Account, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return data.Account, &Error{
"ReadAccount",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.Account, nil
}