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
/
sbanken.go
57 lines (46 loc) · 1.4 KB
/
sbanken.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
// Package sbanken provides an easy way to work with the Sbanken API.
//
// Sbanken API information: https://sbanken.no/bruke/utviklerportalen/
// Sbanken API documentation: https://publicapi.sbanken.no/openapi/apibeta/index.html
package sbanken
import (
"context"
"fmt"
"net/http"
"github.com/engvik/sbanken-go/internal/transport"
)
const VERSION = "1.2.0"
type transportClient interface {
Authorize(context.Context) error
Request(context.Context, *transport.HTTPRequest) ([]byte, int, error)
}
// Client represents an Sbanken client.
type Client struct {
bankBaseURL string
transport transportClient
}
// NewClient returns a new Sbanken client. If httpClient is nil, http.DefaultClient will be used.
func NewClient(ctx context.Context, cfg *Config, httpClient *http.Client) (*Client, error) {
if err := cfg.validate(); err != nil {
return nil, fmt.Errorf("validate: %w", err)
}
userAgent := cfg.UserAgent
if userAgent == "" {
userAgent = fmt.Sprintf("sbanken-go/%s (github.com/engvik/sbanken-go)", VERSION)
}
tCfg := &transport.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
UserAgent: userAgent,
}
c := &Client{
bankBaseURL: "https://publicapi.sbanken.no/apibeta/api",
transport: transport.New(ctx, tCfg, httpClient),
}
if !cfg.skipAuth {
if err := c.transport.Authorize(ctx); err != nil {
return c, fmt.Errorf("Authorize: %w", err)
}
}
return c, nil
}