Python SDK reference

8 minute readReference

The following information is for the latest SDK version (6.x). The CloudBees platform requires that your installed SDK version be at least 6.x. Please install the latest SDK by following the instructions in the platform UI or in the SDK installation documentation.

Any updates to version 6.x are noted in the platform changelog.

Class Rox

from rox.server.rox_server import Rox

The Rox class provides an interface for the CloudBees platform to manage feature flags that control the behavior of your application. This is the central repository for your application’s flags. This class handles communications with the server to obtain the latest flag values, implement flag settings, and set up flag configurations. It works with a RoxContainer-derived object, which holds your application’s feature flags. The values in the provided container are marked as feature flags and display in the UI once the application is run.

This class also allows you to manage custom properties. These can be static settings of type string, boolean, integer, or double, or you can use a generator class to provide a custom property that is dependent upon code state. These generated properties must derive from CustomPropertyGenerator, which is a template class.

You can also use a RoxOptions object to configure some aspects of this class. You can set the verbosity level, custom platform, global freeze level, impression handler, and fetch handler.

Setup

Rox.setup(apiKey)
Rox.setup(apiKey, options)

Configures the Rox object to work with the provided application.

Parameter Modifier and type Description

apiKey

string

The environment-specific SDK key provided by the platform UI.

options

RoxOptions

A RoxOptions instance with the desired configuration for this application.

Returns a Future object representing the execution of the call. It is called when the SDK loads the configuration from the network for the first time.

Shutdown

Rox.shutdown()

Performs graceful shutdown of the Rox object; this closes and cleans all background tasks and threads. Use Shutdown only when Rox is set up successfully. Calling Shutdown when it is not possible results in a log warning. You can call Setup again after Shutdown. In that case, Register and SetCustomProperty then use a new Rox, and register the objects to the UI on the next setup call.

Register

Rox.register(container)
Rox.register(namespace, container)

Registers a feature flag container with the Rox client. The public member variables of this container become a flag in the UI and are named with the object name.

Parameter Modifier and type Description

namespace

string

The prefix namespace for all flags in this container (defaults to '').

container

object

An object that contains your application’s feature flags.

For example, assuming we have the following container:

from rox.server.flags.rox_flag import RoxFlag class MyContainer: def __init__(self): self.include_facebook = RoxFlag() self.include_github = RoxFlag();

And we register the container with the following code:

from rox.server.rox_server import Rox my_container = MyContainer() Rox.register('Login', myContainer) Rox.setup(<ROLLOUT_KEY>)

The flags and configurations in the UI have the names Login.includeFacebook and Login.includeGithub. This can be used to create several namespaces, each with its own group of flags and configurations.

If you do not require a namespace, set it to an empty string. You can also remove a namespace entirely, as it defaults to an empty string.

set_custom_boolean_property

Rox.set_custom_boolean_property(name, value)

Sets a custom Boolean property on the Rox client. The value can be either a Boolean value or a function that is invoked with the context passed to the flag is_enabled function. The function must return a Boolean value.

Parameter Modifier and type Description

name

string

The name of the property to create.

value

Boolean

function (context) → boolean

set_custom_string_property

Rox.set_custom_string_property(name, value)

Sets a custom string property on the Rox client. The value can be either a string value or a function that is invoked with the context passed to the flag is_enabled function. The function must return a string value.

Parameter Modifier and type Description

name

string

The name of the property to create.

value

string

function (context) → string

set_custom_int_property

Rox.set_custom_int_property(name, value)

Sets a custom integer property on the Rox client. The value can be either a string value or a function that is invoked with the context passed to the flag is_enabled function. The function must return an integer value.

Parameter Modifier and type Description

name

string

The name of the property to create.

value

int

function (context) → int

set_custom_semver_property

Rox.set_custom_semver_property(name, value)

Sets a custom semver property on the Rox client. Refer to the Semantic Versioning documentation for more information. The value can be either a string value or a function that is invoked with the context passed to the flag is_enabled function. The function must return a string value.

Parameter Modifier and type Description

name

string

The name of the property to create.

generator

string

function (context) -> string

set_custom_float_property

Rox.set_custom_float_property(name, value)

Sets a custom float property on the Rox client. The value can be either a string value or a function that is invoked with the context passed to the flag is_enabled function. The function must return a float value.

Parameter Modifier and type Description

name

string

The property name

value

float

function (context) → float

set_context

Rox.set_context(context)

Sets a global context that is available to all flag evaluations.

fetch

Rox.fetch()

Creates a network request for the latest configuration.

Returns

Returns a Future object representing the execution of the call.

set_userspace_unhandled_error_handler

def user_unhandled_error_invoker(errorDetails): print('Unhandled error. trigger type: {}, source: {}, exception: {}.'.format(errorDetails.exception_trigger, errorDetails.exception_source, errorDetails.exception)) Rox.set_userspace_unhandled_error_handler(user_unhandled_error_invoker)

A handler for all user function errors to assist in debugging and to understand unexpected flag values. In case this function is not set, errors are logged to the logger.

CloudBees recommends that you wrap all user methods with try-catch to handle errors while in the relevant context, and not after.

The user_unhandled_error_invoker function must accept a single argument of type rox.core.error_handling.userspace_unhandled_error_invoker.UserspaceUnhandledErrorArgs. Error details are available in the following properties:

Property Modifier and type Description

exception_source

function

The handler function that raises the error.

exception_trigger

ExceptionTrigger

The area of functionality where the error originates.

exception

Exception

The exception that has occurred in the identified exception_source.

The exception_trigger is one of the following values:

class ExceptionTrigger(Enum): DYNAMIC_PROPERTIES_RULE = 1, CONFIGURATION_FETCHED_HANDLER = 2, IMPRESSION_HANDLER = 3, CUSTOM_PROPERTY_GENERATOR = 4

Class RoxOptions

from roxserver.rox_options import RoxOptions

RoxOptions covers configuration options for the Rox client, including the verbosity of the logging. Create instances of this class using RoxOptions.Builder.

The following example sets up a new RoxOptions object. This object sets the version, the logger, provides an impression handler, and includes a fetch handler.

from rox.server.rox import Rox from rox.server.rox_options import RoxOptions # setup configuration_fetched_handler in the options object options = RoxOptions( version="1.0.4" fetch_interval=60 logger=application_log configuration_fetched_handler=lambda sender, e: # TODO: add things to do when configuration was fetched impression_handler=lambda sender, e: # TODO: add things to do on impression dynamic_property_rule_handler=lambda property, context: # TODO: return dynamic rule for properties hosting="eu" ) cancel_event = Rox.setup('<key>', options).result();

version

Sets the version of the service running the feature management SDK as a semver string. Refer to the Semantic Versioning documentation for more information.

fetch_interval

Sets the polling interval (in seconds) for fetching the configuration from the platform storage service. The default setting is 60 seconds, and the minimum time setting is 30 seconds.

logger

Sets the logger to be used for logging.

The logger must implement the following:

def debug(self, message, ex=None) def error(self, message, ex=None) def warn(self, message, ex=None)

configuration_fetched_handler

Set the configuration event handler, to add actions after configurations are fetched.

Arguments to be passed into the configuration_fetched_handler include:

Property Description
fetcher_status

enum indicating the fetch result, can be one of APPLIED_FROM_EMBEDDED, APPLIED_FROM_LOCAL_STORAGE, APPLIED_FROM_NETWORK, or ERROR_FETCHED_FAILED

creation_date

The time the fetched configurations are created and signed.

error_details

enum indicating which error has occurred while fetching and setting up the new configurations. Can be one of CORRUPTED_JSON, EMPTY_JSON, SIGNATURE_VERIFICATION_ERROR, NETWORK_ERROR, MISMATCH_APP_KEY, UNKNOWN, or NO_ERROR.

impression_handler

Sets the impression event handler, to add actions after an impression.

Arguments to be passed into the impression_handler include:

Property Description
reporting_value

Object that contains name (string), value (string) and targeting (boolean).

context

The context in which the impression is called. This is a merged context containing the global context and the call’s context.

dynamic_property_rule_handler

The dynamic custom property generator is called when an explicit custom property definition does not exist on the client side. If you do not set the dynamic_property_rule_handler, the default method is activated, and it attempts to extract the property value from context.

def __default_dynamic_handler(self, prop_name, context): if context: return context[prop_name] return None

roxy_url

A Roxy URL for microservices automated testing and local development.

hosting

Sets the hosting location.

Class RoxFlag

from rox.server.flags.rox_flag import RoxFlag

RoxFlag is a Boolean feature flag. It can be either true or false.

is_enabled

flag.is_enabled(Context = null)

Returns true if the flag is enabled.

name

flag.name

The name of the flag.

Class RoxString

from rox.core.entities.rox_string import RoxString

RoxString is a feature flag object that accepts a list of possible string values and a default value. Use this list when selecting new values for the feature, and the value can be overridden via the UI.

Use as in the following example:

title_colors_options = ['Black', 'Blue', 'Green'] title_colors_flag = RoxString('Black', title_colors_options) if title_colors_flag.get_value() == 'Black': # set the title color to black if title_colors_flag.get_value() == 'Green': # set the title color to green

get_value

flag.get_value(context=None):

Returns the RoxString value considering the given context.

Class RoxInt

from rox.core.entities.rox_int import RoxInt

RoxInt is a feature flag object that accepts a list of possible integer values and a default value. Use this list when selecting new values for the feature, and the value can be overridden via the UI.

Use as in the following example:

title_size_flag = RoxInt(14, [14, 18, 24]) title_size = title_size_flag.get_int()

get_int

flag.get_int(context=None)

Returns the RoxInt value considering the given context.

get_value

flag.get_value(context=None)

Returns the RoxInt value considering the given context.

get_value calls get_int().

Class RoxDouble

from rox.core.entities.rox_double import RoxDouble

RoxDouble is a feature flag object that accepts a list of possible double values and a default value. Use this list when selecting new values for the feature, and the value can be overridden via the UI.

Use as in the following example:

price_options_flag = RoxDouble(19.99, [19.99, 29.99, 40.5]) price = price_options_flag.get_double()

get_double

flag.get_double(context=None)

Returns the RoxDouble value considering the given context.

get_value

flag.get_value(context=None)

Returns the RoxDouble value considering the given context.

get_value calls get_double().

Rox.DynamicApi()

Rox.dynamic_api()

An alternative of using a container with register. This allows you to evaluate flags without having a static container. The dynamic API creates flags as if they were registered, including sending them to the UI.

isEnabled

Evaluate a Boolean flag.

is_enabled(name, defaultValue, context = None);

Name

Type

Description

name

string

The flag name (required — throws an error if null).

defaultValue

boolean

The default value is used if there are no configurations set in the UI.

context

object

This is a merged context containing both the global context and the call context.

value

Evaluate a string flag

value(name, defaultValue, options = [], context = None);

Name

Type

Description

Name

String

The flag name (required — throws an error if null).

defaultValue

String

The default value is used if there are no configurations in the UI, or if the value is set to default in the UI.

options

String

All alternative values to use in the UI. You can change these values in the UI.

context

Object

This is a merged context containing both the global context and the call context.

get_int

Evaluate an int flag

get_int(name, defaultValue, options = [], context = None);
Name Type Description

name

string

the flag’s name - throws an error if none

defaultValue

int

The default value is used if there are no configurations in the UI, or if the value is set to default in the UI.

variations

int[]

Alternative values to use in the UI (these can be changed via the UI).

context

object

This is a merged context containing both the global context and the call context.

get_double

Evaluate a double flag

get_double(name, defaultValue, options = [], context = None);
Name Type Description

name

string

the flag’s name - throws an error if none

defaultValue

double

The default value is used if there are no configurations in the UI, or if the value is set to default in the UI.

variations

double[]

Alternative values to use in the UI (these can be changed via the UI).

context

object

This is a merged context for flag evaluation, containing both the global context and the call context.