Skip to content
Merged
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
20 changes: 20 additions & 0 deletions ldclient/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Note that the same class can also be imported from the ``ldclient.client`` submodule.
"""

import copy
import warnings
from dataclasses import dataclass
from threading import Event
Expand Down Expand Up @@ -472,6 +473,25 @@ def copy_with_new_sdk_key(self, new_sdk_key: str) -> 'Config':
big_segments=self.__big_segments,
)

def with_wrapper_information(self, wrapper_name: Optional[str], wrapper_version: Optional[str] = None) -> 'Config':
"""Returns a new ``Config`` instance that is the same as this one, except for having different wrapper information.

This is intended for use by wrapper libraries, such as the LaunchDarkly OpenFeature providers, which need to
identify themselves rather than the application that configured the client.

The new instance is a shallow copy: it shares the objects the original configuration references, such as the
feature store, the logger, and the HTTP configuration. Mutating one of those objects affects both
configurations.

:param wrapper_name: an identifying name for the wrapper being used; see :py:attr:`~wrapper_name`
:param wrapper_version: the version of the wrapper being used; see :py:attr:`~wrapper_version`
"""
updated = copy.copy(self)
updated.__wrapper_name = wrapper_name
updated.__wrapper_version = wrapper_version

return updated

# for internal use only - probably should be part of the client logic
def get_default(self, key, default):
return default if key not in self.__defaults else self.__defaults[key]
Expand Down
21 changes: 21 additions & 0 deletions ldclient/testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ def test_copy_config():
assert new_config.stream is False


def test_with_wrapper_information():
config = Config(sdk_key="SDK_KEY", stream=False, wrapper_name="original", wrapper_version="1.0.0")

wrapped = config.with_wrapper_information("wrapper", "2.0.0")
assert wrapped.wrapper_name == "wrapper"
assert wrapped.wrapper_version == "2.0.0"
assert wrapped.sdk_key == "SDK_KEY"
assert wrapped.stream is False

assert config.wrapper_name == "original"
assert config.wrapper_version == "1.0.0"


def test_with_wrapper_information_defaults_the_version():
config = Config(sdk_key="SDK_KEY", wrapper_name="original", wrapper_version="1.0.0")

wrapped = config.with_wrapper_information("wrapper")
assert wrapped.wrapper_name == "wrapper"
assert wrapped.wrapper_version is None


def test_can_set_valid_poll_interval():
config = Config(sdk_key="SDK_KEY", poll_interval=31)
assert config.poll_interval == 31
Expand Down
Loading