Skip to content

Commit a7da26f

Browse files
authored
refactor!: move api hooks methods to api module (#169)
* refactor!: move api hooks methods to api module Signed-off-by: Federico Bond <[email protected]> * refactor!: rename api-level hook methods Signed-off-by: Federico Bond <[email protected]> --------- Signed-off-by: Federico Bond <[email protected]>
1 parent 9e1bcb3 commit a7da26f

File tree

7 files changed

+44
-46
lines changed

7 files changed

+44
-46
lines changed

open_feature/hooks/__init__.py

-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +0,0 @@
1-
import typing
2-
3-
from open_feature.hooks.hook import Hook
4-
5-
6-
_hooks: typing.List[Hook] = []
7-
8-
9-
def add_api_hooks(hooks: typing.List[Hook]):
10-
global _hooks
11-
_hooks = _hooks + hooks
12-
13-
14-
def clear_api_hooks():
15-
global _hooks
16-
_hooks = []
17-
18-
19-
def api_hooks() -> typing.List[Hook]:
20-
global _hooks
21-
return _hooks

open_feature/open_feature_api.py

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from open_feature.evaluation_context.evaluation_context import EvaluationContext
44
from open_feature.exception.exceptions import GeneralError
5+
from open_feature.hooks.hook import Hook
56
from open_feature.open_feature_client import OpenFeatureClient
67
from open_feature.provider.metadata import Metadata
78
from open_feature.provider.no_op_provider import NoOpProvider
@@ -11,6 +12,8 @@
1112

1213
_evaluation_context = EvaluationContext()
1314

15+
_hooks: typing.List[Hook] = []
16+
1417

1518
def get_client(
1619
name: typing.Optional[str] = None, version: typing.Optional[str] = None
@@ -45,3 +48,18 @@ def set_evaluation_context(evaluation_context: EvaluationContext):
4548
if evaluation_context is None:
4649
raise GeneralError(error_message="No api level evaluation context")
4750
_evaluation_context = evaluation_context
51+
52+
53+
def add_hooks(hooks: typing.List[Hook]):
54+
global _hooks
55+
_hooks = _hooks + hooks
56+
57+
58+
def clear_hooks():
59+
global _hooks
60+
_hooks = []
61+
62+
63+
def get_hooks() -> typing.List[Hook]:
64+
global _hooks
65+
return _hooks

open_feature/open_feature_client.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from open_feature.flag_evaluation.flag_type import FlagType
1616
from open_feature.flag_evaluation.reason import Reason
1717
from open_feature.flag_evaluation.resolution_details import FlagResolutionDetails
18-
from open_feature.hooks import api_hooks
1918
from open_feature.hooks.hook import Hook
2019
from open_feature.hooks.hook_context import HookContext
2120
from open_feature.hooks.hook_support import (
@@ -259,7 +258,7 @@ def evaluate_flag_details(
259258
# in the flag evaluation
260259
# before: API, Client, Invocation, Provider
261260
merged_hooks = (
262-
api_hooks()
261+
api.get_hooks()
263262
+ self.hooks
264263
+ evaluation_hooks
265264
+ self.provider.get_provider_hooks()

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ class MyHook(Hook):
150150

151151

152152
# set global hooks at the API-level
153-
from open_feature.hooks import add_api_hooks
154-
add_api_hooks([MyHook()])
153+
from open_feature.open_feature_api import add_hooks
154+
add_hooks([MyHook()])
155155

156156
# or configure them in the client
157157
client = OpenFeatureClient()

tests/hooks/test_init.py

-18
This file was deleted.

tests/test_open_feature_api.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from unittest.mock import MagicMock
2+
13
import pytest
24

35
from open_feature.evaluation_context.evaluation_context import EvaluationContext
6+
from open_feature.hooks.hook import Hook
47
from open_feature.exception.error_code import ErrorCode
58
from open_feature.exception.exceptions import GeneralError
69
from open_feature.open_feature_api import (
@@ -10,6 +13,9 @@
1013
get_provider_metadata,
1114
get_evaluation_context,
1215
set_evaluation_context,
16+
get_hooks,
17+
add_hooks,
18+
clear_hooks,
1319
)
1420
from open_feature.provider.metadata import Metadata
1521
from open_feature.provider.no_op_provider import NoOpProvider
@@ -97,3 +103,17 @@ def test_should_successfully_set_evaluation_context_for_api():
97103
assert global_evaluation_context
98104
assert global_evaluation_context.targeting_key == evaluation_context.targeting_key
99105
assert global_evaluation_context.attributes == evaluation_context.attributes
106+
107+
108+
def test_should_add_hooks_to_api_hooks():
109+
# Given
110+
hook_1 = MagicMock(spec=Hook)
111+
hook_2 = MagicMock(spec=Hook)
112+
clear_hooks()
113+
114+
# When
115+
add_hooks([hook_1])
116+
add_hooks([hook_2])
117+
118+
# Then
119+
assert get_hooks() == [hook_1, hook_2]

tests/test_open_feature_client.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import pytest
44

5+
from open_feature.open_feature_api import add_hooks, clear_hooks
56
from open_feature.exception.error_code import ErrorCode
67
from open_feature.exception.exceptions import OpenFeatureError
78
from open_feature.flag_evaluation.reason import Reason
8-
from open_feature.hooks import clear_api_hooks, add_api_hooks
99
from open_feature.hooks.hook import Hook
1010
from open_feature.open_feature_client import OpenFeatureClient
1111
from open_feature.provider.no_op_provider import NoOpProvider
@@ -149,9 +149,9 @@ def test_should_return_client_metadata_with_name():
149149

150150
def test_should_call_api_level_hooks(no_op_provider_client):
151151
# Given
152-
clear_api_hooks()
152+
clear_hooks()
153153
api_hook = MagicMock(spec=Hook)
154-
add_api_hooks([api_hook])
154+
add_hooks([api_hook])
155155

156156
# When
157157
no_op_provider_client.get_boolean_details(flag_key="Key", default_value=True)

0 commit comments

Comments
 (0)