Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: v6 migration guide #328

Open
wants to merge 6 commits into
base: chore/swap-core-for-yggdrasil
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

This is the Python client for [Unleash](https://github.com/unleash/unleash). It implements [Client Specifications 1.0](https://docs.getunleash.io/client-specification) and checks compliance based on spec in [unleash/client-specifications](https://github.com/Unleash/client-specification)

> **Migrating to v6**
>
> If you use custom strategies or access the `features` property on the Unleash Client, read the complete [migration guide](./v6_MIGRATION_GUIDE.md) before upgrading to v6.


What it supports:
* Default activation strategies using 32-bit [Murmurhash3](https://en.wikipedia.org/wiki/MurmurHash)
* Custom strategies
Expand Down
30 changes: 7 additions & 23 deletions docs/customstrategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,18 @@ Implementing a custom strategy
#######################################

- Set up a custom strategy in Unleash. Note down the name - you'll need this exact value to ensure we're loading the custom strategy correctly.
- Create a custom strategy object by sub-classing the Strategy object.
- Create a custom strategy object, with an apply method.

.. code-block:: python

from UnleashClient.strategies.Strategies import Strategy

class CatTest(Strategy):
def load_provisioning(self) -> list:
return [x.strip() for x in self.parameters["sound"].split(',')]

def apply(self, context: dict = None) -> bool:
"""
Turn on if I'm a cat.

:return:
"""
class CatTest:
def apply(self, parameters: dict, context: dict = None) -> bool:
default_value = False

parameters = [x.strip() for x in parameters["sound"].split(",")]

if "sound" in context.keys():
default_value = context["sound"] in self.parsed_provisioning
default_value = context["sound"] in parameters

return default_value

Expand All @@ -34,7 +26,7 @@ Implementing a custom strategy

.. code-block:: python

my_custom_strategies = {"amIACat": CatTest}
my_custom_strategies = {"amIACat": CatTest()}

- When initializing UnleashClient, provide the custom strategy dictionary.

Expand All @@ -43,11 +35,3 @@ Implementing a custom strategy
unleash_client = UnleashClient(URL, APP_NAME, custom_strategies=my_custom_strategies)

- Fire up Unleash! You can now use the "amIACat" strategy in a feature toggle.

Migrating your custom strategies from Strategy from v2.x.x to v3.x.x (for fun and profit)
#########################################################################################
To get support for for constraints in your custom strategy, take the following steps:

- Instead of overriding the `__call__()` method, override the `apply()` method. (In practice, you can just rename the method!)
- ???
- Profit!
62 changes: 62 additions & 0 deletions v6_MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Migrating to Unleash-Client-Python 6.0.0

This guide highlights the key changes you should be aware of when upgrading to v6.0.0 of the Unleash client.

## Direct access to feature toggles
sighphyre marked this conversation as resolved.
Show resolved Hide resolved

Direct access to the feature toggle objects through `UnleashClient.features` has been removed. All classes related to the internal representation of feature toggles are no longer publicly accessible in the SDK.
sighphyre marked this conversation as resolved.
Show resolved Hide resolved

The SDK now provides an `UnleashClient.feature_definitions()` method, which returns a list of feature toggle names, their type, and the project they're bound to.
sighphyre marked this conversation as resolved.
Show resolved Hide resolved

## Changes to custom strategies

Custom strategies have undergone some changes that require updates to their implementations. This is a strict requirement: any strategy that does not implement the correct interface will throw an exception at startup.
sighphyre marked this conversation as resolved.
Show resolved Hide resolved

The interface changes are as follows:

- Strategies no longer inherit from a base class.
- The apply method now accepts a second parameter, parameters. In legacy versions, this functionality was managed by the load_provisioning method.
sighphyre marked this conversation as resolved.
Show resolved Hide resolved

Here is an example of a legacy strategy:

``` python
class CatStrategy(Strategy):
def load_provisioning(self) -> list:
return [x.strip() for x in self.parameters["sound"].split(",")]

def apply(self, context: dict = None) -> bool:
default_value = False

if "sound" in context.keys():
default_value = context["sound"] in self.parsed_provisioning

return default_value
```

This is now written as:

``` python
class CatStrategy:
def apply(self, parameters: dict, context: dict = None) -> bool:
default_value = False

parameters = [x.strip() for x in parameters["sound"].split(",")]
sighphyre marked this conversation as resolved.
Show resolved Hide resolved

if "sound" in context.keys():
default_value = context["sound"] in parameters

return default_value

```

Strategies are now mounted as an instance rather than a class object when configuring the SDK:

``` python

custom_strategies_dict = {"amIACat": CatStrategy()}

unleash_client = UnleashClient(
"some_unleash_url", "some_app_name", custom_strategies=custom_strategies_dict
)

```
Loading