Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: move api hooks methods to api module #169

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions open_feature/hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +0,0 @@
import typing

from open_feature.hooks.hook import Hook


_hooks: typing.List[Hook] = []


def add_api_hooks(hooks: typing.List[Hook]):
global _hooks
_hooks = _hooks + hooks


def clear_api_hooks():
global _hooks
_hooks = []


def api_hooks() -> typing.List[Hook]:
global _hooks
return _hooks
18 changes: 18 additions & 0 deletions open_feature/open_feature_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from open_feature.evaluation_context.evaluation_context import EvaluationContext
from open_feature.exception.exceptions import GeneralError
from open_feature.hooks.hook import Hook
federicobond marked this conversation as resolved.
Show resolved Hide resolved
from open_feature.open_feature_client import OpenFeatureClient
from open_feature.provider.metadata import Metadata
from open_feature.provider.no_op_provider import NoOpProvider
Expand All @@ -11,6 +12,8 @@

_evaluation_context = EvaluationContext()

_hooks: typing.List[Hook] = []


def get_client(
name: typing.Optional[str] = None, version: typing.Optional[str] = None
Expand Down Expand Up @@ -45,3 +48,18 @@ def set_evaluation_context(evaluation_context: EvaluationContext):
if evaluation_context is None:
raise GeneralError(error_message="No api level evaluation context")
_evaluation_context = evaluation_context


def add_hooks(hooks: typing.List[Hook]):
global _hooks
_hooks = _hooks + hooks


def clear_hooks():
global _hooks
_hooks = []


def get_hooks() -> typing.List[Hook]:
global _hooks
return _hooks
3 changes: 1 addition & 2 deletions open_feature/open_feature_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from open_feature.flag_evaluation.flag_type import FlagType
from open_feature.flag_evaluation.reason import Reason
from open_feature.flag_evaluation.resolution_details import FlagResolutionDetails
from open_feature.hooks import api_hooks
from open_feature.hooks.hook import Hook
from open_feature.hooks.hook_context import HookContext
from open_feature.hooks.hook_support import (
Expand Down Expand Up @@ -259,7 +258,7 @@ def evaluate_flag_details(
# in the flag evaluation
# before: API, Client, Invocation, Provider
merged_hooks = (
api_hooks()
api.get_hooks()
+ self.hooks
+ evaluation_hooks
+ self.provider.get_provider_hooks()
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class MyHook(Hook):


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

# or configure them in the client
client = OpenFeatureClient()
Expand Down
18 changes: 0 additions & 18 deletions tests/hooks/test_init.py

This file was deleted.

20 changes: 20 additions & 0 deletions tests/test_open_feature_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest.mock import MagicMock

import pytest

from open_feature.evaluation_context.evaluation_context import EvaluationContext
from open_feature.hooks.hook import Hook
from open_feature.exception.error_code import ErrorCode
from open_feature.exception.exceptions import GeneralError
from open_feature.open_feature_api import (
Expand All @@ -10,6 +13,9 @@
get_provider_metadata,
get_evaluation_context,
set_evaluation_context,
get_hooks,
add_hooks,
clear_hooks,
)
from open_feature.provider.metadata import Metadata
from open_feature.provider.no_op_provider import NoOpProvider
Expand Down Expand Up @@ -97,3 +103,17 @@ def test_should_successfully_set_evaluation_context_for_api():
assert global_evaluation_context
assert global_evaluation_context.targeting_key == evaluation_context.targeting_key
assert global_evaluation_context.attributes == evaluation_context.attributes


def test_should_add_hooks_to_api_hooks():
# Given
hook_1 = MagicMock(spec=Hook)
hook_2 = MagicMock(spec=Hook)
clear_hooks()

# When
add_hooks([hook_1])
add_hooks([hook_2])

# Then
assert get_hooks() == [hook_1, hook_2]
6 changes: 3 additions & 3 deletions tests/test_open_feature_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import pytest

from open_feature.open_feature_api import add_hooks, clear_hooks
from open_feature.exception.error_code import ErrorCode
from open_feature.exception.exceptions import OpenFeatureError
from open_feature.flag_evaluation.reason import Reason
from open_feature.hooks import clear_api_hooks, add_api_hooks
from open_feature.hooks.hook import Hook
from open_feature.open_feature_client import OpenFeatureClient
from open_feature.provider.no_op_provider import NoOpProvider
Expand Down Expand Up @@ -149,9 +149,9 @@ def test_should_return_client_metadata_with_name():

def test_should_call_api_level_hooks(no_op_provider_client):
# Given
clear_api_hooks()
clear_hooks()
api_hook = MagicMock(spec=Hook)
add_api_hooks([api_hook])
add_hooks([api_hook])

# When
no_op_provider_client.get_boolean_details(flag_key="Key", default_value=True)
Expand Down