-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
135 lines (112 loc) · 4.48 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package gosdk
import (
"context"
"log/slog"
"time"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"google.golang.org/grpc"
"github.com/nebius/gosdk/conn"
)
// Option is an interface combining all options for [New] func.
type Option interface {
option()
}
// WithCredentials enables client authentication. By default, [NoCredentials] is used.
//
// [Credentials] are applied under the following conditions:
// - The outgoing gRPC metadata does not already include authorization information.
// - The list of gRPC call options does not contain [github.com/nebius/gosdk/auth.DisableAuth].
//
// If either of these conditions is not met, the provided credentials will not be used.
func WithCredentials(creds Credentials) Option {
return optionCredentials{creds: creds}
}
// WithLogger enables logging in the SDK. By default [NoopHandler] is used.
func WithLogger(logger *slog.Logger) Option {
return WithLoggerHandler(logger.Handler())
}
// WithLoggerHandler enables logging in the SDK. By default [NoopHandler] is used.
func WithLoggerHandler(handler slog.Handler) Option {
return optionLogger{handler: handler}
}
// WithLoggingOptions allows you to specify additional configurations for the grpc-ecosystem logging interceptor.
func WithLoggingOptions(opts ...logging.Option) Option {
return optionLoggingOptions(opts)
}
// WithDialOptions allows you to specify additional grpc dial options for all services.
// You can use [conn.WithAddressDialOptions] to use different options for a specific [conn.Address].
func WithDialOptions(opts ...grpc.DialOption) Option {
return optionDialOpts(opts)
}
// WithResolvers customizes service address resolution.
func WithResolvers(resolvers ...conn.Resolver) Option {
return optionResolvers(resolvers)
}
// WithDomain changes the default "api.nebius.cloud:443" domain.
func WithDomain(domain string) Option {
return optionDomain(domain)
}
// WithAddressTemplate customizes service address resolution.
func WithAddressTemplate(find string, replace string) Option {
return optionAddressTemplate{
find: find,
replace: replace,
}
}
// WithExplicitInit alters the behavior of the [New] constructor.
// If explicitInit is false (the default), [SDK.Init] is automatically called by [New].
// If explicitInit is true, you must manually call [SDK.Init] yourself.
// This option is useful for separating the [SDK] instantiation from I/O operations.
func WithExplicitInit(explicitInit bool) Option {
return optionExplicitInit(explicitInit)
}
// WithInit adds an extra fn, which will be called on init [SDK].
func WithInit(fn func(context.Context, *SDK) error) Option {
return optionInit(fn)
}
// WithTimeout changes timeout for all requests. The default is 1 minute.
func WithTimeout(timeout time.Duration) Option {
return optionTimeout(timeout)
}
type (
optionCredentials struct{ creds Credentials }
optionLogger struct{ handler slog.Handler }
optionLoggingOptions []logging.Option
optionDialOpts []grpc.DialOption
optionResolvers []conn.Resolver
optionDomain string
optionAddressTemplate struct {
find string
replace string
}
optionExplicitInit bool
optionInit func(context.Context, *SDK) error
optionTimeout time.Duration
)
func (optionCredentials) option() {}
func (optionLogger) option() {}
func (optionLoggingOptions) option() {}
func (optionDialOpts) option() {}
func (optionResolvers) option() {}
func (optionDomain) option() {}
func (optionAddressTemplate) option() {}
func (optionExplicitInit) option() {}
func (optionInit) option() {}
func (optionTimeout) option() {}
type NoopHandler struct{}
func (NoopHandler) Enabled(context.Context, slog.Level) bool { return false }
func (NoopHandler) Handle(context.Context, slog.Record) error { return nil }
func (h NoopHandler) WithAttrs([]slog.Attr) slog.Handler { return h }
func (h NoopHandler) WithGroup(string) slog.Handler { return h }
type slogAdapter struct {
log *slog.Logger
}
func (a slogAdapter) Log(ctx context.Context, level logging.Level, msg string, fields ...any) {
a.log.Log(ctx, slog.Level(level), msg, fields...)
}
func unaryLoggingInterceptor(log *slog.Logger, opts ...logging.Option) grpc.UnaryClientInterceptor {
return logging.UnaryClientInterceptor(slogAdapter{log: log}, opts...)
}
func streamLoggingInterceptor(log *slog.Logger, opts ...logging.Option) grpc.StreamClientInterceptor {
return logging.StreamClientInterceptor(slogAdapter{log: log}, opts...)
}