This repository has been 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 pathcards.go
59 lines (50 loc) · 1.37 KB
/
cards.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
package sbanken
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/engvik/sbanken-go/internal/transport"
)
// Card represents a card.
// Sbanken API documentation: https://publicapi.sbanken.no/openapi/apibeta/index.html#/Cards/Cards_List
type Card struct {
ID string `json:"cardId"`
Number string `json:"cardNumber"`
ExpiryDate string `json:"expiryDate"`
Status string `json:"status"`
Type string `json:"cardType"`
ProductCode string `json:"productCode"`
AccountNumber string `json:"accountNumber"`
AccountOwner string `json:"accountOwner"`
CustomerID string `json:"customerId"`
VersionNumber int `json:"cardVersionNumber"`
}
// ListCards lists the cards.
func (c *Client) ListCards(ctx context.Context) ([]Card, error) {
url := fmt.Sprintf("%s/v2/Cards", 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 {
Cards []Card `json:"items"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.Cards, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return data.Cards, &Error{
"ListCards",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.Cards, nil
}