|
| 1 | +import logging |
| 2 | +import typing |
| 3 | +from functools import reduce |
| 4 | + |
| 5 | +from open_feature.evaluation_context import EvaluationContext |
| 6 | +from open_feature.flag_evaluation import FlagEvaluationDetails, FlagType |
| 7 | +from open_feature.hook import Hook, HookContext, HookType |
| 8 | + |
| 9 | + |
| 10 | +def error_hooks( |
| 11 | + flag_type: FlagType, |
| 12 | + hook_context: HookContext, |
| 13 | + exception: Exception, |
| 14 | + hooks: typing.List[Hook], |
| 15 | + hints: typing.Optional[typing.Mapping] = None, |
| 16 | +): |
| 17 | + kwargs = {"hook_context": hook_context, "exception": exception, "hints": hints} |
| 18 | + _execute_hooks( |
| 19 | + flag_type=flag_type, hooks=hooks, hook_method=HookType.ERROR, **kwargs |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +def after_all_hooks( |
| 24 | + flag_type: FlagType, |
| 25 | + hook_context: HookContext, |
| 26 | + hooks: typing.List[Hook], |
| 27 | + hints: typing.Optional[typing.Mapping] = None, |
| 28 | +): |
| 29 | + kwargs = {"hook_context": hook_context, "hints": hints} |
| 30 | + _execute_hooks( |
| 31 | + flag_type=flag_type, hooks=hooks, hook_method=HookType.FINALLY_AFTER, **kwargs |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def after_hooks( |
| 36 | + flag_type: FlagType, |
| 37 | + hook_context: HookContext, |
| 38 | + details: FlagEvaluationDetails, |
| 39 | + hooks: typing.List[Hook], |
| 40 | + hints: typing.Optional[typing.Mapping] = None, |
| 41 | +): |
| 42 | + kwargs = {"hook_context": hook_context, "details": details, "hints": hints} |
| 43 | + _execute_hooks_unchecked( |
| 44 | + flag_type=flag_type, hooks=hooks, hook_method=HookType.AFTER, **kwargs |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def before_hooks( |
| 49 | + flag_type: FlagType, |
| 50 | + hook_context: HookContext, |
| 51 | + hooks: typing.List[Hook], |
| 52 | + hints: typing.Optional[typing.Mapping] = None, |
| 53 | +) -> EvaluationContext: |
| 54 | + kwargs = {"hook_context": hook_context, "hints": hints} |
| 55 | + executed_hooks = _execute_hooks_unchecked( |
| 56 | + flag_type=flag_type, hooks=hooks, hook_method=HookType.BEFORE, **kwargs |
| 57 | + ) |
| 58 | + filtered_hooks = list(filter(lambda hook: hook is not None, executed_hooks)) |
| 59 | + |
| 60 | + if filtered_hooks: |
| 61 | + return reduce(lambda a, b: a.merge(b), filtered_hooks) |
| 62 | + |
| 63 | + return EvaluationContext() |
| 64 | + |
| 65 | + |
| 66 | +def _execute_hooks( |
| 67 | + flag_type: FlagType, hooks: typing.List[Hook], hook_method: HookType, **kwargs |
| 68 | +) -> list: |
| 69 | + """ |
| 70 | + Run multiple hooks of any hook type. All of these hooks will be run through an |
| 71 | + exception check. |
| 72 | +
|
| 73 | + :param flag_type: particular type of flag |
| 74 | + :param hooks: a list of hooks |
| 75 | + :param hook_method: the type of hook that is being run |
| 76 | + :param kwargs: arguments that need to be provided to the hook method |
| 77 | + :return: a list of results from the applied hook methods |
| 78 | + """ |
| 79 | + if hooks: |
| 80 | + filtered_hooks = list( |
| 81 | + filter( |
| 82 | + lambda hook: hook.supports_flag_value_type(flag_type=flag_type), hooks |
| 83 | + ) |
| 84 | + ) |
| 85 | + return [ |
| 86 | + _execute_hook_checked(hook, hook_method, **kwargs) |
| 87 | + for hook in filtered_hooks |
| 88 | + ] |
| 89 | + return [] |
| 90 | + |
| 91 | + |
| 92 | +def _execute_hooks_unchecked( |
| 93 | + flag_type: FlagType, hooks, hook_method: HookType, **kwargs |
| 94 | +) -> list: |
| 95 | + """ |
| 96 | + Execute a single hook without checking whether an exception is thrown. This is |
| 97 | + used in the before and after hooks since any exception will be caught in the |
| 98 | + client. |
| 99 | +
|
| 100 | + :param flag_type: particular type of flag |
| 101 | + :param hooks: a list of hooks |
| 102 | + :param hook_method: the type of hook that is being run |
| 103 | + :param kwargs: arguments that need to be provided to the hook method |
| 104 | + :return: a list of results from the applied hook methods |
| 105 | + """ |
| 106 | + if hooks: |
| 107 | + filtered_hooks = list( |
| 108 | + filter( |
| 109 | + lambda hook: hook.supports_flag_value_type(flag_type=flag_type), hooks |
| 110 | + ) |
| 111 | + ) |
| 112 | + return [getattr(hook, hook_method.value)(**kwargs) for hook in filtered_hooks] |
| 113 | + |
| 114 | + return [] |
| 115 | + |
| 116 | + |
| 117 | +def _execute_hook_checked(hook: Hook, hook_method: HookType, **kwargs): |
| 118 | + """ |
| 119 | + Try and run a single hook and catch any exception thrown. This is used in the |
| 120 | + after all and error hooks since any error thrown at this point needs to be caught. |
| 121 | +
|
| 122 | + :param hook: a list of hooks |
| 123 | + :param hook_method: the type of hook that is being run |
| 124 | + :param kwargs: arguments that need to be provided to the hook method |
| 125 | + :return: the result of the hook method |
| 126 | + """ |
| 127 | + try: |
| 128 | + return getattr(hook, hook_method.value)(**kwargs) |
| 129 | + except Exception: # noqa |
| 130 | + logging.error(f"Exception when running {hook_method.value} hooks") |
0 commit comments