|
1 | | -from openfeature.hook.hook import Hook, HookContext, HookData, HookHints, HookType |
2 | | -from openfeature.hook.logging_hook import LoggingHook |
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import typing |
| 4 | +from collections.abc import Mapping, MutableMapping, Sequence |
| 5 | +from datetime import datetime |
| 6 | +from enum import Enum |
| 7 | + |
| 8 | +from openfeature.evaluation_context import EvaluationContext |
| 9 | +from openfeature.flag_evaluation import FlagEvaluationDetails, FlagType, FlagValueType |
| 10 | + |
| 11 | +if typing.TYPE_CHECKING: |
| 12 | + from openfeature.client import ClientMetadata |
| 13 | + from openfeature.provider.metadata import Metadata |
3 | 14 |
|
4 | 15 | __all__ = [ |
5 | 16 | "Hook", |
6 | 17 | "HookContext", |
7 | 18 | "HookData", |
8 | 19 | "HookHints", |
9 | 20 | "HookType", |
10 | | - "LoggingHook", |
11 | 21 | "add_hooks", |
12 | 22 | "clear_hooks", |
13 | 23 | "get_hooks", |
|
16 | 26 | _hooks: list[Hook] = [] |
17 | 27 |
|
18 | 28 |
|
| 29 | +# https://openfeature.dev/specification/sections/hooks/#requirement-461 |
| 30 | +HookData = MutableMapping[str, typing.Any] |
| 31 | + |
| 32 | + |
| 33 | +class HookType(Enum): |
| 34 | + BEFORE = "before" |
| 35 | + AFTER = "after" |
| 36 | + FINALLY_AFTER = "finally_after" |
| 37 | + ERROR = "error" |
| 38 | + |
| 39 | + |
| 40 | +class HookContext: |
| 41 | + def __init__( # noqa: PLR0913 |
| 42 | + self, |
| 43 | + flag_key: str, |
| 44 | + flag_type: FlagType, |
| 45 | + default_value: FlagValueType, |
| 46 | + evaluation_context: EvaluationContext, |
| 47 | + client_metadata: ClientMetadata | None = None, |
| 48 | + provider_metadata: Metadata | None = None, |
| 49 | + hook_data: HookData | None = None, |
| 50 | + ): |
| 51 | + self.flag_key = flag_key |
| 52 | + self.flag_type = flag_type |
| 53 | + self.default_value = default_value |
| 54 | + self.evaluation_context = evaluation_context |
| 55 | + self.client_metadata = client_metadata |
| 56 | + self.provider_metadata = provider_metadata |
| 57 | + self.hook_data = hook_data or {} |
| 58 | + |
| 59 | + def __setattr__(self, key: str, value: typing.Any) -> None: |
| 60 | + if hasattr(self, key) and key in ( |
| 61 | + "flag_key", |
| 62 | + "flag_type", |
| 63 | + "default_value", |
| 64 | + "client_metadata", |
| 65 | + "provider_metadata", |
| 66 | + ): |
| 67 | + raise AttributeError(f"Attribute {key!r} is immutable") |
| 68 | + super().__setattr__(key, value) |
| 69 | + |
| 70 | + |
| 71 | +# https://openfeature.dev/specification/sections/hooks/#requirement-421 |
| 72 | +HookHintValue: typing.TypeAlias = ( |
| 73 | + bool |
| 74 | + | int |
| 75 | + | float |
| 76 | + | str |
| 77 | + | datetime |
| 78 | + | Sequence["HookHintValue"] |
| 79 | + | Mapping[str, "HookHintValue"] |
| 80 | +) |
| 81 | + |
| 82 | +HookHints = Mapping[str, HookHintValue] |
| 83 | + |
| 84 | + |
| 85 | +class Hook: |
| 86 | + def before( |
| 87 | + self, hook_context: HookContext, hints: HookHints |
| 88 | + ) -> EvaluationContext | None: |
| 89 | + """ |
| 90 | + Runs before flag is resolved. |
| 91 | +
|
| 92 | + :param hook_context: Information about the particular flag evaluation |
| 93 | + :param hints: An immutable mapping of data for users to |
| 94 | + communicate to the hooks. |
| 95 | + :return: An EvaluationContext. It will be merged with the |
| 96 | + EvaluationContext instances from other hooks, the client and API. |
| 97 | + """ |
| 98 | + return None |
| 99 | + |
| 100 | + def after( |
| 101 | + self, |
| 102 | + hook_context: HookContext, |
| 103 | + details: FlagEvaluationDetails[FlagValueType], |
| 104 | + hints: HookHints, |
| 105 | + ) -> None: |
| 106 | + """ |
| 107 | + Runs after a flag is resolved. |
| 108 | +
|
| 109 | + :param hook_context: Information about the particular flag evaluation |
| 110 | + :param details: Information about how the flag was resolved, |
| 111 | + including any resolved values. |
| 112 | + :param hints: A mapping of data for users to communicate to the hooks. |
| 113 | + """ |
| 114 | + pass |
| 115 | + |
| 116 | + def error( |
| 117 | + self, hook_context: HookContext, exception: Exception, hints: HookHints |
| 118 | + ) -> None: |
| 119 | + """ |
| 120 | + Run when evaluation encounters an error. Errors thrown will be swallowed. |
| 121 | +
|
| 122 | + :param hook_context: Information about the particular flag evaluation |
| 123 | + :param exception: The exception that was thrown |
| 124 | + :param hints: A mapping of data for users to communicate to the hooks. |
| 125 | + """ |
| 126 | + pass |
| 127 | + |
| 128 | + def finally_after( |
| 129 | + self, |
| 130 | + hook_context: HookContext, |
| 131 | + details: FlagEvaluationDetails[FlagValueType], |
| 132 | + hints: HookHints, |
| 133 | + ) -> None: |
| 134 | + """ |
| 135 | + Run after flag evaluation, including any error processing. |
| 136 | + This will always run. Errors will be swallowed. |
| 137 | +
|
| 138 | + :param hook_context: Information about the particular flag evaluation |
| 139 | + :param hints: A mapping of data for users to communicate to the hooks. |
| 140 | + """ |
| 141 | + pass |
| 142 | + |
| 143 | + def supports_flag_value_type(self, flag_type: FlagType) -> bool: |
| 144 | + """ |
| 145 | + Check to see if the hook supports the particular flag type. |
| 146 | +
|
| 147 | + :param flag_type: particular type of the flag |
| 148 | + :return: a boolean containing whether the flag type is supported (True) |
| 149 | + or not (False) |
| 150 | + """ |
| 151 | + return True |
| 152 | + |
| 153 | + |
19 | 154 | def add_hooks(hooks: list[Hook]) -> None: |
20 | 155 | global _hooks |
21 | 156 | _hooks = _hooks + hooks |
|
0 commit comments