|
| 1 | +# Extension Model |
| 2 | + |
| 3 | +> The BDK Extension Mechanism is still an experimental feature, contracts might be subject to **breaking changes** |
| 4 | +> in following versions. |
| 5 | +
|
| 6 | +## Overview |
| 7 | + |
| 8 | +The Extension API is available through the module `symphony.bdk.core.extension` but other modules might be required |
| 9 | +depending on what your extension needs to use. |
| 10 | + |
| 11 | +## Registering Extensions |
| 12 | + |
| 13 | +Extensions are registered _programmatically_ via the `ExtensionService`: |
| 14 | + |
| 15 | +```python |
| 16 | + async with SymphonyBdk(config) as bdk: |
| 17 | + extension_service = bdk.extensions() |
| 18 | + extension_service.register(MyExtensionType) |
| 19 | +``` |
| 20 | + |
| 21 | +## Service Provider Extension |
| 22 | + |
| 23 | +A _Service Provider_ extension is a specific type of extension loaded on demand when calling the |
| 24 | +`ExtensionService#service(MyExtensionType)` method. |
| 25 | + |
| 26 | +To make your extension _Service Provider_, your extension definition must implement the method `get_service(self)`: |
| 27 | + |
| 28 | +```python |
| 29 | +# The Service implementation class. |
| 30 | +class MyBdkExtensionService: |
| 31 | + def say_hello(self, name): |
| 32 | + print(f"Hello, {name}!") |
| 33 | + |
| 34 | + |
| 35 | +# The Extension definition class. |
| 36 | +class MyBdkExtension: |
| 37 | + def __init__(self): |
| 38 | + self._service = MyBdkExtensionService() |
| 39 | + |
| 40 | + def get_service(self): |
| 41 | + return self._service |
| 42 | + |
| 43 | + |
| 44 | +# Usage example. |
| 45 | +async def run(): |
| 46 | + config = BdkConfigLoader.load_from_symphony_dir("config.yaml") |
| 47 | + |
| 48 | + async with SymphonyBdk(config) as bdk: |
| 49 | + extension_service = bdk.extensions() |
| 50 | + extension_service.register(MyBdkExtensionService) |
| 51 | + |
| 52 | + service = extension_service.service(MyBdkExtensionService) |
| 53 | + service.say_hello("Symphony") |
| 54 | + |
| 55 | + |
| 56 | +asyncio.run(run()) |
| 57 | +``` |
| 58 | + |
| 59 | +## BDK Aware Extensions |
| 60 | + |
| 61 | +The BDK Extension Model allows extensions to access to some core objects such as the configuration or the api clients. |
| 62 | +Developers that wish to use these objects are free to implement a set of abstract base classes all suffixed with |
| 63 | +the `Aware` keyword. |
| 64 | + |
| 65 | +If an extension do not extend one of `Aware` classes but implements the corresponding method, the latter will be used as |
| 66 | +the `ExtensionService` uses duck typing internally. |
| 67 | + |
| 68 | +### `BdkConfigAware` |
| 69 | + |
| 70 | +The abc `symphony.bdk.core.extension.BdkConfigAware` allows extensions to read the `BdkConfig`: |
| 71 | + |
| 72 | +```python |
| 73 | +class MyBdkExtension(BdkConfigAware): |
| 74 | + def __init__(self): |
| 75 | + self._config = None |
| 76 | + |
| 77 | + def set_config(self, config): |
| 78 | + self._config = config |
| 79 | +``` |
| 80 | + |
| 81 | +### `BdkApiClientFactoryAware` |
| 82 | + |
| 83 | +The abc `symphony.bdk.core.extension.BdkApiClientFactoryAware` can be used by extensions that need to use |
| 84 | +the `ApiClientFactory`: |
| 85 | + |
| 86 | +```python |
| 87 | +class MyBdkExtension(BdkApiClientFactoryAware): |
| 88 | + def __init__(self): |
| 89 | + self._api_client_factory = None |
| 90 | + |
| 91 | + def set_api_client_factory(self, api_client_factory): |
| 92 | + self._api_client_factory = api_client_factory |
| 93 | +``` |
| 94 | + |
| 95 | +### `BdkAuthenticationAware` |
| 96 | + |
| 97 | +The abc `symphony.bdk.core.extension.BdkAuthenticationAware` can be used by extensions that need to rely on the service |
| 98 | +account authentication session (`AuthSession`), which provides the `sessionToken` and |
| 99 | +`keyManagerToken` that are used to call the Symphony's APIs: |
| 100 | + |
| 101 | +```python |
| 102 | +class MyBdkExtension(BdkAuthenticationAware): |
| 103 | + def __init__(self): |
| 104 | + self._auth_session = None |
| 105 | + |
| 106 | + def set_auth_session(self, auth_session): |
| 107 | + self._auth_session = auth_session |
| 108 | +``` |
| 109 | + |
| 110 | +## Retry |
| 111 | + |
| 112 | +In order to leverage the retry mechanism your service class should have the field `self._retry_config` of type |
| 113 | +`BdkRetryConfig` and each function that needs a retry mechanism can use the `@retry` decorator. This decorator will |
| 114 | +reuse the config declared in `self._retry_config`. |
| 115 | + |
| 116 | +The default retry mechanism is defined here: [`refresh_session_if_unauthorized`](../_autosummary/symphony.bdk.core.retry.strategy.refresh_session_if_unauthorized.html). |
| 117 | +It retries on connection errors (more precisely `ClientConnectionError` and `TimeoutError`) and |
| 118 | +on the following HTTP status codes: |
| 119 | +* 401 |
| 120 | +* 429 |
| 121 | +* codes greater than or equal to 500. |
| 122 | + |
| 123 | +In case of unauthorized, it will call `await self._auth_session.refresh()` before retrying. |
| 124 | + |
| 125 | +Following is a sample code to show how it can be used: |
| 126 | +```python |
| 127 | +from symphony.bdk.core.extension import BdkExtensionServiceProvider, BdkAuthenticationAware, BdkConfigAware |
| 128 | +from symphony.bdk.core.retry import retry |
| 129 | + |
| 130 | +class MyExtension(BdkConfigAware, BdkAuthenticationAware, BdkExtensionServiceProvider): |
| 131 | + def __init__(self): |
| 132 | + self._config = None |
| 133 | + self._auth_session = None |
| 134 | + |
| 135 | + def set_config(self, config): |
| 136 | + self._config = config |
| 137 | + |
| 138 | + def set_bot_session(self, auth_session): |
| 139 | + self._auth_session = auth_session |
| 140 | + |
| 141 | + def get_service(self): |
| 142 | + return MyService(self._config.retry, self._auth_session) |
| 143 | + |
| 144 | + |
| 145 | +class MyService: |
| 146 | + def __init__(self, retry_config, auth_session): |
| 147 | + self._retry_config = retry_config # used by the @retry decorator |
| 148 | + self._auth_session = auth_session # default retry logic will call refresh on self._auth_session |
| 149 | + |
| 150 | + @retry |
| 151 | + async def my_service_method(self): |
| 152 | + pass # do stuff which will be retried |
| 153 | +``` |
| 154 | + |
| 155 | +Retry conditions and mechanism can be customized as follows: |
| 156 | +```python |
| 157 | +async def my_retry_mechanism(retry_state): |
| 158 | + """Function used by the retry decorator to check if a function call has to be retried. |
| 159 | +
|
| 160 | + :param retry_state: current retry state, of type RetryCallState: https://tenacity.readthedocs.io/en/latest/#retrycallstate |
| 161 | + :return: True if we want to retry, False otherwise |
| 162 | + """ |
| 163 | + if retry_state.outcome.failed: |
| 164 | + exception = retry_state.outcome.exception() # exception that lead to the failure |
| 165 | + if condition_on_exception(exception): |
| 166 | + # do stuff to recover the exception |
| 167 | + # method args can be accessed as follows: retry_state.args |
| 168 | + return True # return True to retry the function |
| 169 | + return False # return False to not retry and make function call fail |
| 170 | + |
| 171 | +class MyService: |
| 172 | + def __init__(self, retry_config, auth_session): |
| 173 | + self._retry_config = retry_config # used by the @retry decorator |
| 174 | + self._auth_session = auth_session # default retry logic will call refresh on self._auth_session |
| 175 | + |
| 176 | + @retry(retry=my_retry_mechanism) |
| 177 | + async def my_service_method(self): |
| 178 | + pass # do stuff which will be retried |
| 179 | +``` |
0 commit comments