sdk

package module
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2024 License: MIT Imports: 12 Imported by: 7

README

Flipt Go SDK

Go Reference

The Flipt Go SDK supports developing applications in Go against Flipt. It also supports the ability to access the resource management APIs and other systems, such as authentication and metadata.

The SDK supports both Flipts gRPC and HTTP RPC APIs. A majority of this client is generated directly from Flipt's protobuf definitions. The Flipt SDK Generator can be found locally within this repository.

Dependencies

  • Go >= v1.20

Client / Server Version Compatibility

client ⌄ / server › <= 1.19.* >= 1.20.0
0.1.* ✓*
>= 0.2.*

* Backwards compatible, but can only access the "default" namespace.

Get the SDK

go get go.flipt.io/flipt/sdk/go

Construct and Authenticate the SDK

Constructing an SDK client is easy.

  1. Pick your transport of choice. Bothgrpc or http are sub-packages with respective implementations.
  2. Pass a constructed Transport implementation to sdk.New(...).
  3. Optionally pass in a sdk.ClientAuthenticationProvider to authenticate your RPC calls.
package main

import (
 sdk "go.flipt.io/flipt/sdk/go"
 sdkgrpc "go.flipt.io/flipt/sdk/go/grpc"
 grpc "google.golang.org/grpc"
)

func main() {
 token := sdk.StaticTokenAuthenticationProvider("a-flipt-client-token")

 conn := grpc.Dial("localhost:9090")
 transport := sdkgrpc.NewTransport(conn)

 client := sdk.New(transport, sdk.WithAuthenticationProvider(token))
}

Documentation

Overview

Package sdk is the official Flipt Go SDK.

The SDK exposes the various RPC for interfacing with a remote Flipt instance. Both HTTP and gRPC protocols are supported via this unified Go API.

The main entrypoint within this package is New, which takes an instance of Transport and returns an instance of SDK. Transport implementations can be found in the following sub-packages:

GRPC Transport

The following is an example of creating and instance of the SDK using the gRPC transport.

func main() {
	conn := grpc.Dial("localhost:9090")
	transport := grpc.NewTransport(conn)
	client := sdk.New(transport)
}

HTTP Transport

The following is an example of creating an instance of the SDK using the HTTP transport.

func main() {
	transport := http.NewTransport("http://localhost:8080")
	client := sdk.New(transport)
}

Authenticating the SDK

The remote procedure calls mades by this SDK are authenticated via a ClientAuthenticationProvider implementation. This can be supplied to New via the WithAuthenticationProvider option. Note that each of these methods will only work if the target Flipt server instance has the authentication method enabled.

Currently, there are three implementations:

- StaticTokenAuthenticationProvider(https://www.flipt.io/docs/authentication/methods#static-token):

This provider sets a static Flipt client token via the Authentication header with the Bearer scheme.

func main() {
	provider := sdk.StaticTokenAuthenticationProvider("some-flipt-token")
	client := sdk.New(transport, sdk.WithAuthenticationProvider(provider))
}

- JWTAuthenticationProvider(https://www.flipt.io/docs/authentication/methods#json-web-tokens):

This provider sets a pre-generated JSON web-token via the Authentication header with the JWT scheme.

func main() {
	provider := sdk.JWTAuthenticationProvider("some-flipt-jwt")
	client := sdk.New(transport, sdk.WithAuthenticationProvider(provider))
}

- KubernetesAuthenticationProvider(https://www.flipt.io/docs/authentication/methods#kubernetes):

This automatically uses the service account token on the host and exchanges it with Flipt for a Flipt client token credential. The credential is then used to authenticate requests, again via the Authentication header and the Bearer scheme. It ensures that the client token is not-expired and requests fresh tokens automatically without intervention. Use this method to automatically authenticate your application with a Flipt deployed into the same Kubernetes cluster.

func main() {
    provider := sdk.NewKuberntesAuthenticationProvider(transport)
    client := sdk.New(transport, sdk.WithAuthenticationProvider(provider))
}

SDK Services

The Flipt SDK is split into four sections Flipt, Auth, Meta, and Evaluation. Each of which provides access to different parts of the Flipt system.

Flipt Service

The Flipt service is the core Flipt API service. This service provides access to the Flipt resource CRUD APIs.

client := sdk.New(transport).Flipt()

Flipt resources can be accessed and managed directly.

flag, err := client.GetFlag(ctx, &flipt.GetFlagRequest{
	NamespaceKey: "my_namespace",
	Key: "my_flag_key",
})
if err != nil {
	panic(err)
}

fmt.Println(flag.Name)
fmt.Println(flag.Description)

Evaluation Service

The Evaluation service provides access to the Flipt evaluation APIs.

client := sdk.New(transport).Evaluation()

The Evaluation service provides three methods for evaluating a flag for a given entity: Boolean, Variant, and Batch.

Boolean

The Boolean method returns a response containing a boolean value indicating whether or not the flag is enabled for the given entity. Learn more about the Boolean flag type: <https://www.flipt.io/docs/concepts#boolean-flags>

resp, err := client.Boolean(ctx, &evaluation.EvaluationRequest{
	NamespaceKey: "my_namespace",
	FlagKey: "my_flag_key",
	EntityId: "my_entity_id",
})
if err != nil {
	panic(err)
}

fmt.Println(resp.Enabled)

Variant

The Variant method returns a response containing the variant key for the given entity. Learn more about the Variant flag type: <https://www.flipt.io/docs/concepts#variant-flags>

resp, err := client.Variant(ctx, &evaluation.EvaluationRequest{
	NamespaceKey: "my_namespace",
	FlagKey: "my_flag_key",
	EntityId: "my_entity_id",
})
if err != nil {
	panic(err)
}

fmt.Println(resp.VariantKey)
fmt.Println(resp.VariantAttachment) Optional

Batch

The Batch method returns a response containing the evaluation results for a batch of requests. These requests can be for a mix of boolean and variant flags.

resp, err := client.Batch(ctx, &evaluation.BatchRequest{
	RequestId: "my_request_id",
	Requests: []*evaluation.EvaluationRequest{
		{
			NamespaceKey: "my_namespace",
			FlagKey: "my_flag_key",
			EntityId: "my_entity_id",
		}
	},
})
if err != nil {
	panic(err)
}

for _, result := range resp.Responses {
	fmt.Println(result.Type)
 }

Index

Examples

Constants

View Source
const DefaultNamespace = "default"

DefaultNamespace is the default namespace created in Flipt. This namespace is protected and will always be present. Omitting a namespace key leads to this namespace being referenced.

Variables

This section is empty.

Functions

This section is empty.

Types

type Analytics added in v0.11.0

type Analytics struct {
	// contains filtered or unexported fields
}

func (*Analytics) GetFlagEvaluationsCount added in v0.11.0

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

func (Auth) AuthenticationMethodGithubService added in v0.6.0

func (s Auth) AuthenticationMethodGithubService() *AuthenticationMethodGithubService

func (Auth) AuthenticationMethodKubernetesService

func (s Auth) AuthenticationMethodKubernetesService() *AuthenticationMethodKubernetesService

func (Auth) AuthenticationMethodOIDCService

func (s Auth) AuthenticationMethodOIDCService() *AuthenticationMethodOIDCService

func (Auth) AuthenticationMethodTokenService

func (s Auth) AuthenticationMethodTokenService() *AuthenticationMethodTokenService

func (Auth) AuthenticationService

func (s Auth) AuthenticationService() *AuthenticationService

func (Auth) PublicAuthenticationService

func (s Auth) PublicAuthenticationService() *PublicAuthenticationService

type AuthClient

type AuthClient interface {
	PublicAuthenticationServiceClient() auth.PublicAuthenticationServiceClient
	AuthenticationServiceClient() auth.AuthenticationServiceClient
	AuthenticationMethodTokenServiceClient() auth.AuthenticationMethodTokenServiceClient
	AuthenticationMethodOIDCServiceClient() auth.AuthenticationMethodOIDCServiceClient
	AuthenticationMethodKubernetesServiceClient() auth.AuthenticationMethodKubernetesServiceClient
	AuthenticationMethodGithubServiceClient() auth.AuthenticationMethodGithubServiceClient
}

type AuthenticationMethodGithubService added in v0.6.0

type AuthenticationMethodGithubService struct {
	// contains filtered or unexported fields
}

func (*AuthenticationMethodGithubService) AuthorizeURL added in v0.6.0

func (*AuthenticationMethodGithubService) Callback added in v0.6.0

type AuthenticationMethodKubernetesService

type AuthenticationMethodKubernetesService struct {
	// contains filtered or unexported fields
}

func (*AuthenticationMethodKubernetesService) VerifyServiceAccount

type AuthenticationMethodOIDCService

type AuthenticationMethodOIDCService struct {
	// contains filtered or unexported fields
}

func (*AuthenticationMethodOIDCService) AuthorizeURL

func (*AuthenticationMethodOIDCService) Callback

type AuthenticationMethodTokenService

type AuthenticationMethodTokenService struct {
	// contains filtered or unexported fields
}

func (*AuthenticationMethodTokenService) CreateToken

type AuthenticationService

type AuthenticationService struct {
	// contains filtered or unexported fields
}

func (*AuthenticationService) DeleteAuthentication

func (*AuthenticationService) ExpireAuthenticationSelf

func (x *AuthenticationService) ExpireAuthenticationSelf(ctx context.Context, v *auth.ExpireAuthenticationSelfRequest) error

func (*AuthenticationService) GetAuthentication

func (*AuthenticationService) GetAuthenticationSelf

func (x *AuthenticationService) GetAuthenticationSelf(ctx context.Context) (*auth.Authentication, error)

func (*AuthenticationService) ListAuthentications

type ClientAuthenticationProvider added in v0.9.0

type ClientAuthenticationProvider interface {
	Authentication(context.Context) (string, error)
}

ClientAuthenticationProvider is a type which when requested provides a client authentication which can be used to authenticate RPC/API calls invoked through the SDK.

type ClientTokenProvider

type ClientTokenProvider interface {
	ClientToken() (string, error)
}

ClientTokenProvider is a type which when requested provides a client token which can be used to authenticate RPC/API calls invoked through the SDK. Deprecated: Use ClientAuthenticationProvider instead.

type Evaluation added in v0.4.0

type Evaluation struct {
	// contains filtered or unexported fields
}

func (*Evaluation) Batch added in v0.4.0

func (*Evaluation) Boolean added in v0.4.0

Example
// see the following subpackages for transport implementations:
// - grpc
// - http
var transport Transport

client := New(transport)

client.Evaluation().Boolean(context.Background(), &evaluation.EvaluationRequest{
	NamespaceKey: "my_namespace",
	FlagKey:      "my_flag",
	EntityId:     "my_entity",
	Context:      map[string]string{"some": "context"},
})
Output:

func (*Evaluation) Variant added in v0.4.0

Example
// see the following subpackages for transport implementations:
// - grpc
// - http
var transport Transport

client := New(transport)

client.Evaluation().Variant(context.Background(), &evaluation.EvaluationRequest{
	NamespaceKey: "my_namespace",
	FlagKey:      "my_flag",
	EntityId:     "my_entity",
	Context:      map[string]string{"some": "context"},
})
Output:

type Flipt

type Flipt struct {
	// contains filtered or unexported fields
}

func (*Flipt) CreateConstraint

func (x *Flipt) CreateConstraint(ctx context.Context, v *flipt.CreateConstraintRequest) (*flipt.Constraint, error)

func (*Flipt) CreateDistribution

func (x *Flipt) CreateDistribution(ctx context.Context, v *flipt.CreateDistributionRequest) (*flipt.Distribution, error)

func (*Flipt) CreateFlag

func (x *Flipt) CreateFlag(ctx context.Context, v *flipt.CreateFlagRequest) (*flipt.Flag, error)

func (*Flipt) CreateNamespace added in v0.2.0

func (x *Flipt) CreateNamespace(ctx context.Context, v *flipt.CreateNamespaceRequest) (*flipt.Namespace, error)

func (*Flipt) CreateRollout added in v0.4.0

func (x *Flipt) CreateRollout(ctx context.Context, v *flipt.CreateRolloutRequest) (*flipt.Rollout, error)

func (*Flipt) CreateRule

func (x *Flipt) CreateRule(ctx context.Context, v *flipt.CreateRuleRequest) (*flipt.Rule, error)

func (*Flipt) CreateSegment

func (x *Flipt) CreateSegment(ctx context.Context, v *flipt.CreateSegmentRequest) (*flipt.Segment, error)

func (*Flipt) CreateVariant

func (x *Flipt) CreateVariant(ctx context.Context, v *flipt.CreateVariantRequest) (*flipt.Variant, error)

func (*Flipt) DeleteConstraint

func (x *Flipt) DeleteConstraint(ctx context.Context, v *flipt.DeleteConstraintRequest) error

func (*Flipt) DeleteDistribution

func (x *Flipt) DeleteDistribution(ctx context.Context, v *flipt.DeleteDistributionRequest) error

func (*Flipt) DeleteFlag

func (x *Flipt) DeleteFlag(ctx context.Context, v *flipt.DeleteFlagRequest) error

func (*Flipt) DeleteNamespace added in v0.2.0

func (x *Flipt) DeleteNamespace(ctx context.Context, v *flipt.DeleteNamespaceRequest) error

func (*Flipt) DeleteRollout added in v0.4.0

func (x *Flipt) DeleteRollout(ctx context.Context, v *flipt.DeleteRolloutRequest) error

func (*Flipt) DeleteRule

func (x *Flipt) DeleteRule(ctx context.Context, v *flipt.DeleteRuleRequest) error

func (*Flipt) DeleteSegment

func (x *Flipt) DeleteSegment(ctx context.Context, v *flipt.DeleteSegmentRequest) error

func (*Flipt) DeleteVariant

func (x *Flipt) DeleteVariant(ctx context.Context, v *flipt.DeleteVariantRequest) error

func (*Flipt) Evaluate

func (*Flipt) GetFlag

func (x *Flipt) GetFlag(ctx context.Context, v *flipt.GetFlagRequest) (*flipt.Flag, error)

func (*Flipt) GetNamespace added in v0.2.0

func (x *Flipt) GetNamespace(ctx context.Context, v *flipt.GetNamespaceRequest) (*flipt.Namespace, error)

func (*Flipt) GetRollout added in v0.4.0

func (x *Flipt) GetRollout(ctx context.Context, v *flipt.GetRolloutRequest) (*flipt.Rollout, error)

func (*Flipt) GetRule

func (x *Flipt) GetRule(ctx context.Context, v *flipt.GetRuleRequest) (*flipt.Rule, error)

func (*Flipt) GetSegment

func (x *Flipt) GetSegment(ctx context.Context, v *flipt.GetSegmentRequest) (*flipt.Segment, error)

func (*Flipt) ListFlags

func (x *Flipt) ListFlags(ctx context.Context, v *flipt.ListFlagRequest) (*flipt.FlagList, error)

func (*Flipt) ListNamespaces added in v0.2.0

func (x *Flipt) ListNamespaces(ctx context.Context, v *flipt.ListNamespaceRequest) (*flipt.NamespaceList, error)

func (*Flipt) ListRollouts added in v0.4.0

func (x *Flipt) ListRollouts(ctx context.Context, v *flipt.ListRolloutRequest) (*flipt.RolloutList, error)

func (*Flipt) ListRules

func (x *Flipt) ListRules(ctx context.Context, v *flipt.ListRuleRequest) (*flipt.RuleList, error)

func (*Flipt) ListSegments

func (x *Flipt) ListSegments(ctx context.Context, v *flipt.ListSegmentRequest) (*flipt.SegmentList, error)

func (*Flipt) OrderRollouts added in v0.4.0

func (x *Flipt) OrderRollouts(ctx context.Context, v *flipt.OrderRolloutsRequest) error

func (*Flipt) OrderRules

func (x *Flipt) OrderRules(ctx context.Context, v *flipt.OrderRulesRequest) error

func (*Flipt) UpdateConstraint

func (x *Flipt) UpdateConstraint(ctx context.Context, v *flipt.UpdateConstraintRequest) (*flipt.Constraint, error)

func (*Flipt) UpdateDistribution

func (x *Flipt) UpdateDistribution(ctx context.Context, v *flipt.UpdateDistributionRequest) (*flipt.Distribution, error)

func (*Flipt) UpdateFlag

func (x *Flipt) UpdateFlag(ctx context.Context, v *flipt.UpdateFlagRequest) (*flipt.Flag, error)

func (*Flipt) UpdateNamespace added in v0.2.0

func (x *Flipt) UpdateNamespace(ctx context.Context, v *flipt.UpdateNamespaceRequest) (*flipt.Namespace, error)

func (*Flipt) UpdateRollout added in v0.4.0

func (x *Flipt) UpdateRollout(ctx context.Context, v *flipt.UpdateRolloutRequest) (*flipt.Rollout, error)

func (*Flipt) UpdateRule

func (x *Flipt) UpdateRule(ctx context.Context, v *flipt.UpdateRuleRequest) (*flipt.Rule, error)

func (*Flipt) UpdateSegment

func (x *Flipt) UpdateSegment(ctx context.Context, v *flipt.UpdateSegmentRequest) (*flipt.Segment, error)

func (*Flipt) UpdateVariant

func (x *Flipt) UpdateVariant(ctx context.Context, v *flipt.UpdateVariantRequest) (*flipt.Variant, error)

type JWTAuthenticationProvider added in v0.9.0

type JWTAuthenticationProvider string

JWTAuthenticationProvider is a string which is supplied as a JWT client authentication on each RPC which requires authentication.

func (JWTAuthenticationProvider) Authentication added in v0.9.0

func (p JWTAuthenticationProvider) Authentication(context.Context) (string, error)

Authentication returns the underlying string that is the JWTAuthenticationProvider.

type KubernetesAuthenticationProvider added in v0.10.0

type KubernetesAuthenticationProvider struct {
	// contains filtered or unexported fields
}

KubernetesAuthenticationProvider is an implementation of ClientAuthenticationProvider which automatically uses the service account token from the environment and exchanges it with Flipt for a client token. This provider keeps the client token up to date and refreshes it for a new client token before expiry. It re-reads the service account token as Kubernetes can and will refresh this token, as it also has its own expiry.

func NewKubernetesAuthenticationProvider added in v0.10.0

func NewKubernetesAuthenticationProvider(transport Transport, opts ...KubernetesAuthenticationProviderOption) *KubernetesAuthenticationProvider

NewKubernetesAuthenticationProvider constructs and configures a new KubernetesAuthenticationProvider using the provided transport.

func (*KubernetesAuthenticationProvider) Authentication added in v0.10.0

func (k *KubernetesAuthenticationProvider) Authentication(ctx context.Context) (string, error)

Authentication returns the authentication header string to be used for a request by the client SDK. It is generated via exchanging the local service account token with Flipt for a client token. The token is then formatted appropriately for use in the Authentication header as a bearer token.

type KubernetesAuthenticationProviderOption added in v0.10.0

type KubernetesAuthenticationProviderOption func(*KubernetesAuthenticationProvider)

KubernetesAuthenticationProviderOption is a functional option for configuring KubernetesAuthenticationProvider.

func WithKubernetesExpiryLeeway added in v0.10.0

func WithKubernetesExpiryLeeway(d time.Duration) KubernetesAuthenticationProviderOption

WithKubernetesExpiryLeeway configures the duration leeway for deciding when to refresh the client token. The default is 10 seconds, which ensures that tokens are automatically refreshed when their is less that 10 seconds of lifetime left on the previously fetched client token.

func WithKubernetesServiceAccountTokenPath added in v0.10.0

func WithKubernetesServiceAccountTokenPath(p string) KubernetesAuthenticationProviderOption

WithKubernetesServiceAccountTokenPath sets the path on the host to locate the kubernetes service account. The KubernetesAuthenticationProvider uses the default location set by Kubernetes. This option lets you override that if your path happens to differ.

type Meta

type Meta struct {
	// contains filtered or unexported fields
}

func (*Meta) GetConfiguration

func (x *Meta) GetConfiguration(ctx context.Context) (*httpbody.HttpBody, error)

func (*Meta) GetInfo

func (x *Meta) GetInfo(ctx context.Context) (*httpbody.HttpBody, error)

type Option

type Option func(*SDK)

Option is a functional option which configures the Flipt SDK.

func WithAuthenticationProvider added in v0.9.0

func WithAuthenticationProvider(p ClientAuthenticationProvider) Option

WithAuthenticationProviders returns an Option which configures any supplied SDK with the provided ClientAuthenticationProvider.

func WithClientTokenProvider

func WithClientTokenProvider(p ClientTokenProvider) Option

WithClientTokenProviders returns an Option which configures any supplied SDK with the provided ClientTokenProvider. Deprecated: Use WithAuthenticationProvider instead.

type PublicAuthenticationService

type PublicAuthenticationService struct {
	// contains filtered or unexported fields
}

func (*PublicAuthenticationService) ListAuthenticationMethods

type SDK

type SDK struct {
	// contains filtered or unexported fields
}

SDK is the definition of Flipt's Go SDK. It depends on a pluggable transport implementation and exposes a consistent API surface area across both transport implementations. It also provides consistent client-side instrumentation and authentication lifecycle support.

func New

func New(t Transport, opts ...Option) SDK

New constructs and configures a Flipt SDK instance from the provided Transport implementation and options.

Example
// see the following subpackages for transport implementations:
// - grpc
// - http
var transport Transport

client := New(transport)

client.Flipt().GetFlag(context.Background(), &flipt.GetFlagRequest{
	NamespaceKey: "my_namespace",
	Key:          "my_flag",
})
Output:

func (SDK) Analytics added in v0.11.0

func (s SDK) Analytics() *Analytics

func (SDK) Auth

func (s SDK) Auth() *Auth

func (SDK) Evaluation added in v0.4.0

func (s SDK) Evaluation() *Evaluation

func (SDK) Flipt

func (s SDK) Flipt() *Flipt

func (SDK) Meta

func (s SDK) Meta() *Meta

type StaticClientTokenProvider

type StaticClientTokenProvider string

StaticClientTokenProvider is a string which is supplied as a static client token on each RPC which requires authentication. Deprecated: Use StaticTokenAuthenticationProvider instead.

func (StaticClientTokenProvider) ClientToken

func (p StaticClientTokenProvider) ClientToken() (string, error)

ClientToken returns the underlying string that is the StaticClientTokenProvider. Deprecated: Use StaticTokenAuthenticationProvider instead.

type StaticTokenAuthenticationProvider added in v0.9.0

type StaticTokenAuthenticationProvider string

StaticTokenAuthenticationProvider is a string which is supplied as a static client authentication on each RPC which requires authentication.

func (StaticTokenAuthenticationProvider) Authentication added in v0.9.0

Authentication returns the underlying string that is the StaticTokenAuthenticationProvider.

type Transport

type Transport interface {
	AnalyticsClient() analytics.AnalyticsServiceClient
	AuthClient() AuthClient
	EvaluationClient() evaluation.EvaluationServiceClient
	FliptClient() flipt.FliptClient
	MetaClient() meta.MetadataServiceClient
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL