-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
89 lines (76 loc) · 2.32 KB
/
client.go
File metadata and controls
89 lines (76 loc) · 2.32 KB
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
//go:generate go tool -modfile=go.tool.mod github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml ./api/openapi.json
package tripletex
import (
"fmt"
"net/http"
"time"
)
type TripletexClient struct {
token *Token
tokenDuration time.Duration
credentials Credentials
baseURL string
httpClient *http.Client
*ClientWithResponses
}
type Credentials struct {
ConsumerToken string // Application specific token
EmployeeToken string // Client specific token
clientId int64 // Used with [WithAccountantClient]
}
type Option func(*TripletexClient)
// WithHttpClient sets a custom http.Client. Defaults to [http.DefaultClient].
func WithHttpClient(client *http.Client) Option {
return func(tc *TripletexClient) {
tc.httpClient = client
}
}
// WithTokenDuration sets the token duration. Defaults to one month.
func WithTokenDuration(duration time.Duration) Option {
return func(tc *TripletexClient) {
tc.tokenDuration = duration
}
}
// WithBaseURLOption sets a custom base URL. Defaults to "https://tripletex.no/v2".
func WithBaseURLOption(baseURL string) Option {
return func(tc *TripletexClient) {
tc.baseURL = baseURL
}
}
// WithAccountantClient sets clientId as username for
// [TripletexClient.interceptAuth].
//
// See https://developer.tripletex.no/docs/documentation/authentication-and-tokens/#accountant-token
// for more details.
func WithAccountantClient(clientId int64) Option {
return func(tc *TripletexClient) {
tc.credentials.clientId = clientId
}
}
// Returns new [TripletexClient].
//
// You can reuse an already generated token and have it revalidated if it has
// expired, by using [TripletexClient.SetToken].
//
// You can provide options to customize the client behavior.
func New(credentials Credentials, options ...Option) *TripletexClient {
now := time.Now()
client := &TripletexClient{
baseURL: "https://tripletex.no/v2",
credentials: credentials,
tokenDuration: now.AddDate(0, 1, 0).Sub(now),
httpClient: http.DefaultClient,
}
for _, option := range options {
option(client)
}
c, err := NewClientWithResponses(
client.baseURL,
WithRequestEditorFn(client.interceptAuth),
WithHTTPClient(client.httpClient))
if err != nil {
panic(fmt.Errorf("tripletex: failed to create new client: %w", err))
}
client.ClientWithResponses = c
return client
}