Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ linters:
#- decorder # checks declaration order and count of types, constants, variables and functions
#- gci # controls golang package import order and makes it always deterministic
#- ginkgolinter # [if you use ginkgo/gomega] enforces standards of using ginkgo and gomega
- godox # detects FIXME, TODO and other comment keywords
# - godox # detects FIXME, TODO and other comment keywords
#- goheader # checks is file header matches to pattern
- interfacebloat # checks the number of methods inside an interface
- ireturn # accept interfaces, return concrete types
Expand Down
41 changes: 12 additions & 29 deletions sdk/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,31 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

type Option func(*config)
// Functional option for SDK constructor
type Option func(*options)

// Internal config struct for building SDK options.
type config struct {
token grpc.DialOption
clientCredentials grpc.DialOption
tls grpc.DialOption
}

func (c *config) build() []grpc.DialOption {
var opts []grpc.DialOption

if c.clientCredentials != nil {
opts = append(opts, c.clientCredentials)
}

if c.token != nil {
opts = append(opts, c.token)
}

opts = append(opts, c.tls)

return opts
// Internal configuration for SDK, Option functions update these
type options struct {
dialOptions []grpc.DialOption
}

// WithInsecureConn returns an Option that sets up an http connection.
func WithInsecureConn() Option {
return func(c *config) {
c.tls = grpc.WithTransportCredentials(insecure.NewCredentials())
return func(c *options) {
c.dialOptions = append(c.dialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
}

// WithToken returns an Option that sets up authentication with a access token.
// WithToken returns an Option that sets up authentication with an OAuth2 access token.
func WithToken(token string) Option {
return func(c *config) {
c.token = grpc.WithPerRPCCredentials(nil)
return func(c *options) {
c.dialOptions = append(c.dialOptions, grpc.WithPerRPCCredentials(nil))
}
}

// WithClientCredentials returns an Option that sets up authentication with client credentials.
func WithClientCredentials(clientID, clientSecret string) Option {
return func(c *config) {
c.clientCredentials = grpc.WithPerRPCCredentials(nil)
return func(c *options) {
c.dialOptions = append(c.dialOptions, grpc.WithPerRPCCredentials(nil))
}
}
19 changes: 11 additions & 8 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

const (
ErrGrpcDialFailed = Error("failed to dial grpc endpoint")
ErrNotImplemented = Error("function not implemented")
ErrShutdownFailed = Error("failed to shutdown sdk")
)

Expand All @@ -34,18 +35,17 @@ type SDK struct {
Authorization authorization.AuthorizationServiceClient
}

// New returns a new SDK to connect to the platform Endpoint as configured
// by the opts Options
func New(platformEndpoint string, opts ...Option) (*SDK, error) {
// Set default options
cfg := &config{
tls: grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")),
cfg := &options{
dialOptions: []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, ""))},
}

// Apply options
for _, opt := range opts {
opt(cfg)
}

conn, err := grpc.Dial(platformEndpoint, cfg.build()...)
conn, err := grpc.Dial(platformEndpoint, cfg.dialOptions...)
if err != nil {
return nil, errors.Join(ErrGrpcDialFailed, err)
}
Expand All @@ -62,7 +62,7 @@ func New(platformEndpoint string, opts ...Option) (*SDK, error) {
}

// Close closes the underlying grpc.ClientConn.
func (s SDK) Close() error {
func (s *SDK) Close() error {
if err := s.conn.Close(); err != nil {
return errors.Join(ErrShutdownFailed, err)
}
Expand All @@ -75,6 +75,9 @@ func (s SDK) Conn() *grpc.ClientConn {
}

// ExchangeToken exchanges a access token for a new token. https://datatracker.ietf.org/doc/html/rfc8693
//
// WARNING: Not yet implemented
func (s SDK) TokenExchange(token string) (string, error) {
return "", nil
// TODO Store the token type of dialOptions during initialization
return "", ErrNotImplemented
}