-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
104 lines (81 loc) · 1.99 KB
/
options.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package rig
import (
"encoding/base64"
"fmt"
"net/http"
"os"
"connectrpc.com/connect"
"github.com/rigdev/rig-go-api/api/v1/authentication"
)
type Option interface {
apply(c *config)
}
func WithHost(host string) Option {
return &withHostOption{host: host}
}
type withHostOption struct {
host string
}
func (o *withHostOption) apply(c *config) {
c.host = o.host
}
func WithClientCredentials(cc ClientCredential) Option {
return &withClientCredentials{cc: cc}
}
type withClientCredentials struct {
cc ClientCredential
}
func (o *withClientCredentials) apply(c *config) {
c.login = &authentication.LoginRequest{
Method: &authentication.LoginRequest_ClientCredentials{
ClientCredentials: &authentication.ClientCredentials{
ClientId: o.cc.ClientID,
ClientSecret: o.cc.ClientSecret,
},
},
}
}
func WithSessionManager(sm SessionManager) Option {
return &withSessionManager{sm: sm}
}
type withSessionManager struct {
sm SessionManager
}
func (o *withSessionManager) apply(c *config) {
c.sm = o.sm
}
type Interceptor = connect.Interceptor
func WithInterceptors(ics ...Interceptor) Option {
return &withInterceptors{ics: ics}
}
type withInterceptors struct {
ics []Interceptor
}
func (o *withInterceptors) apply(c *config) {
c.ics = o.ics
}
func WithClient(client *http.Client) Option {
return &withClientOption{client: client}
}
type withClientOption struct {
client *http.Client
}
func (o *withClientOption) apply(c *config) {
c.hc = o.client
}
type WithBasicAuth struct {
cc ClientCredential
}
func WithBasicAuthOption(cc ClientCredential) Option {
return &WithBasicAuth{cc: cc}
}
func (o *WithBasicAuth) apply(c *config) {
if o.cc == (ClientCredential{}) {
o.cc = ClientCredential{
ClientID: os.Getenv("RIG_CLIENT_ID"),
ClientSecret: os.Getenv("RIG_CLIENT_SECRET"),
}
}
base64ClientCredentials := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", o.cc.ClientID, o.cc.ClientSecret)))
c.basicAuth = fmt.Sprintf("Basic %s", base64ClientCredentials)
}