configcat

package module
v5.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2020 License: MIT Imports: 13 Imported by: 0

README

ConfigCat SDK for Go

https://configcat.com

ConfigCat SDK for Go provides easy integration for your application to ConfigCat.

ConfigCat is a feature flag and configuration management service that lets you separate releases from deployments. You can turn your features ON/OFF using ConfigCat Dashboard even after they are deployed. ConfigCat lets you target specific groups of users based on region, email or any other custom user attribute.

ConfigCat is a hosted feature flag service. Manage feature toggles across frontend, backend, mobile, desktop apps. Alternative to LaunchDarkly. Management app + feature flag SDKs.

Build Status Go Report Card codecov GoDoc License

Getting started

1. Install the package with go
go get github.com/configcat/go-sdk/v5
2. Go to Connect your application tab to get your SDK Key:

SDK-KEY

3. Import the ConfigCat client package to your application
import "github.com/configcat/go-sdk/v5"
4. Create a ConfigCat client instance:
client := configcat.NewClient("#YOUR-SDK-KEY#")
5. Get your setting value:
isMyAwesomeFeatureEnabled, ok := client.GetValue("isMyAwesomeFeatureEnabled", false).(bool)
if ok && isMyAwesomeFeatureEnabled {
    DoTheNewThing()
} else {
    DoTheOldThing()
}

Or use the async SDKs:

client.GetValueAsync("isMyAwesomeFeatureEnabled", false, func(result interface{}) {
    isMyAwesomeFeatureEnabled, ok := result.(bool)
    if ok && isMyAwesomeFeatureEnabled {
        DoTheNewThing()
    } else {
        DoTheOldThing()
    }
})
6. Close ConfigCat client on application exit:
client.Close()

Getting user specific setting values with Targeting

Using this feature, you will be able to get different setting values for different users in your application by passing a User Object to the getValue() function.

Read more about Targeting here.

user := configcat.NewUser("#USER-IDENTIFIER#")

isMyAwesomeFeatureEnabled, ok := client.GetValueForUser("isMyAwesomeFeatureEnabled", user, false).(bool)
if ok && isMyAwesomeFeatureEnabled {
    DoTheNewThing()
} else {
    DoTheOldThing()
}

Polling Modes

The ConfigCat SDK supports 3 different polling mechanisms to acquire the setting values from ConfigCat. After latest setting values are downloaded, they are stored in the internal cache then all requests are served from there. Read more about Polling Modes and how to use them at ConfigCat Docs.

Need help?

https://configcat.com/support

Contributing

Contributions are welcome.

About ConfigCat

Documentation

Overview

ConfigCat SDK for Go (https://configcat.com)

Index

Constants

View Source
const (
	// Fetched indicates that a new configuration was fetched.
	Fetched fetchStatus = 0
	// NotModified indicates that the current configuration is not modified.
	NotModified fetchStatus = 1
	// Failure indicates that the current configuration fetch is failed.
	Failure fetchStatus = 2
)
View Source
const (
	LogLevelPanic = LogLevel(logrus.PanicLevel)
	LogLevelFatal = LogLevel(logrus.FatalLevel)
	LogLevelError = LogLevel(logrus.ErrorLevel)
	LogLevelWarn  = LogLevel(logrus.WarnLevel)
	LogLevelInfo  = LogLevel(logrus.InfoLevel)
	LogLevelDebug = LogLevel(logrus.DebugLevel)
	LogLevelTrace = LogLevel(logrus.TraceLevel)
)

Define the logrus log levels

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is an object for handling configurations provided by ConfigCat.

func NewClient

func NewClient(sdkKey string) *Client

NewClient initializes a new ConfigCat Client with the default configuration. The sdkKey parameter is mandatory.

func NewCustomClient

func NewCustomClient(sdkKey string, config ClientConfig) *Client

NewCustomClient initializes a new ConfigCat Client with advanced configuration. The sdkKey parameter is mandatory.

func (*Client) Close

func (client *Client) Close()

Close shuts down the client, after closing, it shouldn't be used

func (*Client) GetAllKeys

func (client *Client) GetAllKeys() ([]string, error)

GetAllKeys retrieves all the setting keys.

func (*Client) GetAllKeysAsync

func (client *Client) GetAllKeysAsync(completion func(result []string, err error))

GetAllKeysAsync retrieves all the setting keys asynchronously.

func (*Client) GetAllVariationIds added in v5.1.0

func (client *Client) GetAllVariationIds() ([]string, error)

GetAllVariationIds returns the Variation IDs synchronously as []string from the configuration.

func (*Client) GetAllVariationIdsAsync added in v5.1.0

func (client *Client) GetAllVariationIdsAsync(completion func(result []string, err error))

GetAllVariationIdsAsync reads and sends a Variation ID asynchronously to a callback function as []string from the configuration.

func (*Client) GetAllVariationIdsAsyncForUser added in v5.1.0

func (client *Client) GetAllVariationIdsAsyncForUser(user *User, completion func(result []string, err error))

GetAllVariationIdsAsyncForUser reads and sends a Variation ID asynchronously to a callback function as []string from the configuration. Optional user argument can be passed to identify the caller.

func (*Client) GetAllVariationIdsForUser added in v5.1.0

func (client *Client) GetAllVariationIdsForUser(user *User) ([]string, error)

GetAllVariationIdsForUser returns the Variation IDs synchronously as []string from the configuration. Optional user argument can be passed to identify the caller.

func (*Client) GetKeyAndValue added in v5.1.0

func (client *Client) GetKeyAndValue(variationId string) (string, interface{})

GetKeyAndValue returns the key of a setting and its value identified by the given Variation ID.

func (*Client) GetKeyAndValueAsync added in v5.1.0

func (client *Client) GetKeyAndValueAsync(variationId string, completion func(key string, value interface{}))

GetAllVariationIdsAsyncForUser reads and sends the key of a setting and its value identified by the given Variation ID asynchronously to a callback function as (string, interface{}) from the configuration.

func (*Client) GetValue

func (client *Client) GetValue(key string, defaultValue interface{}) interface{}

GetValue returns a value synchronously as interface{} from the configuration identified by the given key.

func (*Client) GetValueAsync

func (client *Client) GetValueAsync(key string, defaultValue interface{}, completion func(result interface{}))

GetValueAsync reads and sends a value asynchronously to a callback function as interface{} from the configuration identified by the given key.

func (*Client) GetValueAsyncForUser

func (client *Client) GetValueAsyncForUser(key string, defaultValue interface{}, user *User, completion func(result interface{}))

GetValueAsyncForUser reads and sends a value asynchronously to a callback function as interface{} from the configuration identified by the given key. Optional user argument can be passed to identify the caller.

func (*Client) GetValueForUser

func (client *Client) GetValueForUser(key string, defaultValue interface{}, user *User) interface{}

GetValueForUser returns a value synchronously as interface{} from the configuration identified by the given key. Optional user argument can be passed to identify the caller.

func (*Client) GetVariationId added in v5.1.0

func (client *Client) GetVariationId(key string, defaultVariationId string) string

GetVariationId returns a Variation ID synchronously as string from the configuration identified by the given key.

func (*Client) GetVariationIdAsync added in v5.1.0

func (client *Client) GetVariationIdAsync(key string, defaultVariationId string, completion func(result string))

GetVariationIdAsync reads and sends a Variation ID asynchronously to a callback function as string from the configuration identified by the given key.

func (*Client) GetVariationIdAsyncForUser added in v5.1.0

func (client *Client) GetVariationIdAsyncForUser(key string, defaultVariationId string, user *User, completion func(result string))

GetVariationIdAsyncForUser reads and sends a Variation Id asynchronously to a callback function as string from the configuration identified by the given key. Optional user argument can be passed to identify the caller.

func (*Client) GetVariationIdForUser added in v5.1.0

func (client *Client) GetVariationIdForUser(key string, defaultVariationId string, user *User) string

GetVariationIdForUser returns a Variation ID synchronously as string from the configuration identified by the given key. Optional user argument can be passed to identify the caller.

func (*Client) Refresh

func (client *Client) Refresh()

Refresh initiates a force refresh synchronously on the cached configuration.

func (*Client) RefreshAsync

func (client *Client) RefreshAsync(completion func())

RefreshAsync initiates a force refresh asynchronously on the cached configuration.

type ClientConfig

type ClientConfig struct {
	// Base logger used to create new loggers
	Logger Logger
	// The custom cache implementation used to store the configuration.
	Cache ConfigCache
	// The maximum time how long at most the synchronous calls (e.g. client.get(...)) should block the caller.
	// If it's 0 then the caller will be blocked in case of sync calls, until the operation succeeds or fails.
	MaxWaitTimeForSyncCalls time.Duration
	// The maximum wait time for a http response.
	HttpTimeout time.Duration
	// The base ConfigCat CDN url.
	BaseUrl string
	// The custom http transport object.
	Transport http.RoundTripper
	// The refresh mode of the cached configuration.
	Mode RefreshMode
}

ClientConfig describes custom configuration options for the Client.

type ConfigCache

type ConfigCache interface {
	// get reads the configuration from the cache.
	Get() (string, error)
	// set writes the configuration into the cache.
	Set(value string) error
}

ConfigCache is a cache API used to make custom cache implementations.

type LogLevel

type LogLevel uint32

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})

	Debugln(args ...interface{})
	Infoln(args ...interface{})
	Warnln(args ...interface{})
	Errorln(args ...interface{})
}

Logger defines the interface this library logs with.

func DefaultLogger

func DefaultLogger(level LogLevel) Logger

DefaultLogger creates the default logger with specified log level (logrus.New()).

type RefreshMode

type RefreshMode interface {
	// contains filtered or unexported methods
}

RefreshMode is a base for refresh mode configurations.

func AutoPoll

func AutoPoll(interval time.Duration) RefreshMode

AutoPoll creates an auto polling refresh mode.

func AutoPollWithChangeListener

func AutoPollWithChangeListener(
	interval time.Duration,
	changeListener func()) RefreshMode

AutoPollWithChangeListener creates an auto polling refresh mode with change listener callback.

func LazyLoad

func LazyLoad(cacheInterval time.Duration, useAsyncRefresh bool) RefreshMode

LazyLoad creates a lazy loading refresh mode.

func ManualPoll

func ManualPoll() RefreshMode

ManualPoll creates a manual loading refresh mode.

type User

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

User is an object containing attributes to properly identify a given user for rollout evaluation.

func NewUser

func NewUser(identifier string) *User

NewUser creates a new user object. The identifier argument is mandatory.

func NewUserWithAdditionalAttributes

func NewUserWithAdditionalAttributes(identifier string, email string, country string, custom map[string]string) *User

NewUserWithAdditionalAttributes creates a new user object with additional attributes. The identifier argument is mandatory.

func (*User) GetAttribute

func (user *User) GetAttribute(key string) string

GetAttribute retrieves a user attribute identified by a key.

Jump to

Keyboard shortcuts

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