-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
67 lines (56 loc) · 1.45 KB
/
client.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
package fincode
import (
"context"
"os"
ht "github.com/ogen-go/ogen/http"
"github.com/pepabo/fincode-go/api"
)
var _ api.SecuritySource = (*clientConfig)(nil)
type clientConfig struct {
endpoint string
apiSecretKey string
httpClient ht.Client
}
// Option is a functional option for the fincode client.
type Option func(*clientConfig) error
// Endpoint sets the endpoint of the fincode API.
func Endpoint(e string) Option {
return func(c *clientConfig) error {
c.endpoint = e
return nil
}
}
// APISecretKey sets the API secret key for the fincode API.
func APISecretKey(k string) Option {
return func(c *clientConfig) error {
c.apiSecretKey = k
return nil
}
}
// WithHTTPClient specifies http client to use.
func WithHTTPClient(client ht.Client) Option {
return func(c *clientConfig) error {
c.httpClient = client
return nil
}
}
func (c *clientConfig) BearerAuth(ctx context.Context, operationName string) (api.BearerAuth, error) {
return api.BearerAuth{Token: c.apiSecretKey}, nil
}
// New creates a new fincode client.
func New(opts ...Option) (*api.Client, error) {
c := &clientConfig{
endpoint: "https://api.fincode.jp/v1",
apiSecretKey: os.Getenv("FINCODE_API_SECRET_KEY"),
}
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
var copts []api.ClientOption
if c.httpClient != nil {
copts = append(copts, api.WithClient(c.httpClient))
}
return api.NewClient(c.endpoint, c, copts...)
}