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
/
customers.go
63 lines (53 loc) · 1.54 KB
/
customers.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
package sbanken
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/engvik/sbanken-go/internal/transport"
)
// Customer represents a customer.
// Sbanken API documentation: https://publicapi.sbanken.no/openapi/apibeta/index.html#/Customers
type Customer struct {
CustomerID string `json:"customerID"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
EmailAddress string `json:"emailAddress"`
DateOfBirth string `json:"dateOfBirth"`
PostalAddress Address `json:"postalAddress"`
StreetAddress Address `json:"streetAddress"`
PhoneNumbers []PhoneNumber `json:"phoneNumbers"`
}
// PhoneNumber represents a customer phone number.
type PhoneNumber struct {
CountryCode string `json:"countryCode"`
Number string `json:"number"`
}
// GetCustomer lists customer information.
func (c *Client) GetCustomer(ctx context.Context) (Customer, error) {
url := fmt.Sprintf("%s/v2/Customers", c.bankBaseURL)
res, sc, err := c.transport.Request(ctx, &transport.HTTPRequest{
Method: http.MethodGet,
URL: url,
})
if err != nil {
return Customer{}, fmt.Errorf("request: %w", err)
}
data := struct {
Customer `json:"item"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.Customer, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return Customer{}, &Error{
"Customers",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.Customer, nil
}