Clients- Heksher clients
- class AsyncHeksherClient(service_url: str, update_interval: int, context_features: Sequence[str], *, http_client_args: dict[str, ...] = None)[source]
An asyncrhonous client for Heksher. Updates and declares settings in the background using asyncio tasks.
- Parameters:
service_url – The URL of the Heksher service.
update_interval – The interval in seconds between updates.
context_features – The context features to check for existence.
http_client_args – Additional keyword arguments to pass to the underlying async HTTPX client.
- async set_as_main()[source]
Sets the client as the main client for heksher. Will wait until all currently defined settings are declared, and then will wait for an initial update.
If the client is used as an async context manager, this method will be called on entry.
If an error occurs during the declarations or the initial update, the exception will be raised.
Warning
If an error occurs in the update or declarations loops after the method returned, the exception will not be raised, instead the exception will be logged.
async with AsyncHeksherClient(...) as client: ...
- async aclose()[source]
Closes the client. This will cancel all pending tasks.
If the client is used as an async context manager, this method will be called on exit.
- async close()[source]
Deprecated since version 0.2.0: Use
aclose()instead.Legacy alias for
aclose().
- async reload()[source]
Updates all setting rules. This occurs automatically every
update_intervalseconds afterset_as_main()is called, but calling this method will perform a manual reload. Will also reset the update timer.If an error occurs during the update, the exception will be raised.
- modification_lock: Lock[source]
A lock that can be used to prevent concurrent modification of setting values. To ensure that no modifications are made to settings, acquire this lock.
Warning
Acquiring this lock will block any modification of setting values, including from the context that acquired it. meaning that the following snippet will deadlock:
async with client.modification_lock: await client.reload()
- set_defaults(**kwargs: str | ContextVar[str])[source]
Sets the default context feature values. The keys of **kwargs should be context feature names. The values can be either an
str, in which case default value for the context feature will be the string. Alternatively, the default value may be a context variable, in which case the value will be fetched dynamically.
- async get_settings() Dict[str, ...][source]
Queries the Heksher service for all the settings declared on it (not just by this client). Returns a dict that maps setting names to their data. Setting data has the following attributes:
- track_contexts(**context_values: str | Collection[str])[source]
Tracks specific context feature values. When rules are queried from heksher, only rules that fully match all the tracked context features will be returned.
The keys of **context_values should be context feature names. The values can be either a single string, in which case the context feature will be tracked with the value only, or the value may be a collection of strings, in which case the context feature will be tracked with all of the strings as its value. Alternatively, a value may be the constant
TRACK_ALL, in which case all values of the context feature will be tracked.
- class ThreadHeksherClient(service_url: str, update_interval: int, context_features: Sequence[str], *, http_client_args: dict[str, ...] = None)[source]
A synchronous client for Heksher. Updates and declares settings in the background using a separate thread.
Warning
Since the declarations and updates are performed in a separate thread, any errors that occur during these operations will not be raised. Instead, the error will be logged.
- Parameters:
service_url – The URL of the Heksher service.
update_interval – The interval in seconds between updates.
context_features – The context features to check for existence.
http_client_args – Additional keyword arguments to pass to the underlying HTTPX client.
- set_as_main()[source]
Sets the client as the main client for heksher. Will wait until all currently defined settings are declared, and then will wait for an initial update.
If the client is used as a context manager, this method will be called on entry.
with ThreadHeksherClient(...) as client: ...
- close()[source]
Closes the client. This will close the background thread.
If the client is used as a context manager, this method will be called on exit.
- reload()[source]
Updates all setting rules. This occurs automatically every
update_intervalseconds afterset_as_main()is called, but calling this method will perform a manual reload. Will also reset the update timer.
- modification_lock: Lock[source]
A lock that can be used to prevent concurrent modification of setting values. To ensure that no modifications are made to settings, acquire this lock.
Warning
Acquiring this lock will block any modification of setting values, including from the context that acquired it. meaning that the following snippet will deadlock:
with client.modification_lock: client.reload()
- set_defaults(**kwargs: str | ContextVar[str])[source]
Sets the default context feature values. The keys of **kwargs should be context feature names. The values can be either an
str, in which case default value for the context feature will be the string. Alternatively, the default value may be a context variable, in which case the value will be fetched dynamically.
- get_settings() Dict[str, ...][source]
Queries the Heksher service for all the settings declared on it (not just by this client). Returns a dict that maps setting names to their data. Setting data has the following attributes:
- track_contexts(**context_values: str | Collection[str])[source]
Tracks specific context feature values. When rules are queried from heksher, only rules that fully match all the tracked context features will be returned.
The keys of **context_values should be context feature names. The values can be either a single string, in which case the context feature will be tracked with the value only, or the value may be a collection of strings, in which case the context feature will be tracked with all of the strings as its value. Alternatively, a value may be the constant
TRACK_ALL, in which case all values of the context feature will be tracked.