A library to manage app toggles
Project description
App toggles library
This library has been created taking into account the work done by Pete Hodgson published in https://martinfowler.com/.
How to use it
The example below uses a sample feature to include a cancellation link in a email so the user can cancel the sales order.
- Create the /tmp/test_app_toggles.json file with the following content:
{
"isOrderCancellationEnabled": true,
"isAutoRefundEnable": false
}
- Run the app
import app_toggles
provider = app_toggles.JsonToggleProvider("/tmp/test_app_toggles.json")
toggles = app_toggles.Toggles(provider=provider)
@toggles.toggle_decision
def usage_of_order_cancellation_email(get_toggles, when_on, when_off):
order_cancellation_enabled, auto_refund_enabled = get_toggles(
["isOrderCancellationEnabled", "isAutoRefundEnable"]
)
if order_cancellation_enabled and auto_refund_enabled:
return when_on
return when_off
def create_email_body(client_name: str, sales_order_number: str) -> str:
header = f"""
Dear {client_name},
Your order number {sales_order_number} has bee approved.
"""
footer = """
Cheers,
Your company team
"""
cancellation_text = usage_of_order_cancellation_email(
when_on=f"To cancel your order follow this link: http://cancel/{sales_order_number}",
when_off="",
)
return f"""
{header}
{cancellation_text}
{footer}
"""
body = create_email_body("Gustavo", "2342937")
print(body)
- Feel free to change the value of the toggles in the JSON file and see how the email body changes
How to extend this library
The library exposes a "Provider" interface. You can create your own interface to interact with external services that manage feature flags. Below you' ll find a sample code used to retrieve toggles stored in memory.
DON'T FORGET TO CREATE A PR SO WE CAN ADD THE IMPLEMENTATION YOU'VE CREATED!
import typing
import app_toggles
class InMemoryProvider(app_toggles.Provider):
toggles = {
"isOrderCancellationEnabled": True,
"isAutoRefundEnable": True
}
def get_toggles(self, toggle_names: typing.List[str]) -> typing.Tuple[bool]:
missing_toogles = [toggle for toggle in toggle_names if toggle not in InMemoryProvider.toggles]
if missing_toogles:
raise app_toggles.ToggleNotFoundError(f"The follwing toggles where not found: {', '.join(missing_toogles)}")
return [
toggle_value
for toggle_name, toggle_value in InMemoryProvider.toggles.items()
if toggle_name in toggle_names
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file app_toggles-0.1.1.tar.gz
.
File metadata
- Download URL: app_toggles-0.1.1.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.1 CPython/3.10.4 Darwin/22.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89d423c3519b9f879f8a01ac8c5932591d6fdb9eccc821ed2150255195a06092 |
|
MD5 | 3b3e4902297c6e1aecec8ec70178dc7c |
|
BLAKE2b-256 | 5ce01ee2a4205853bddb931a17a722eb7690705ac02c0f95af4e4b5493bdf469 |
File details
Details for the file app_toggles-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: app_toggles-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.1 CPython/3.10.4 Darwin/22.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef4f62f934efcc0c31480eaaa687ffc80fac269663afcfac282fe72af16caea1 |
|
MD5 | ed9d5e7b7337cb8062b75e41827dc8b2 |
|
BLAKE2b-256 | d7d227524b5764ce8478c2e487941dd833b20edcaf051b9648ab4a9ac4c54559 |