-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlistener.py
More file actions
122 lines (97 loc) · 4.39 KB
/
listener.py
File metadata and controls
122 lines (97 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""Impression listener module."""
import abc
class ImpressionListenerException(Exception):
"""Custom Exception for Impression Listener."""
pass
class ImpressionListener(object, metaclass=abc.ABCMeta):
"""Impression listener interface."""
@abc.abstractmethod
def log_impression(self, data):
"""
Accept and impression generated after an evaluation for custom user handling.
:param data: Impression data in a dictionary format.
:type data: dict
"""
pass
class ImpressionListenerBase(ImpressionListener): # pylint: disable=too-few-public-methods
"""
Impression listener safe-execution wrapper.
Wrapper in charge of building all the data that client would require in case
of adding some logic with the treatment and impression results.
"""
impression_listener = None
def __init__(self, impression_listener, sdk_metadata):
"""
Class Constructor.
:param impression_listener: User provided impression listener.
:type impression_listener: ImpressionListener
:param sdk_metadata: SDK version, instance name & IP
:type sdk_metadata: splitio.client.util.SdkMetadata
"""
self.impression_listener = impression_listener
self._metadata = sdk_metadata
def _construct_data(self, impression, attributes):
data = {}
data['impression'] = impression
data['attributes'] = attributes
data['sdk-language-version'] = self._metadata.sdk_version
data['instance-id'] = self._metadata.instance_name
return data
def log_impression(self, impression, attributes=None):
pass
class ImpressionListenerWrapper(ImpressionListenerBase): # pylint: disable=too-few-public-methods
"""
Impression listener safe-execution wrapper.
Wrapper in charge of building all the data that client would require in case
of adding some logic with the treatment and impression results.
"""
def __init__(self, impression_listener, sdk_metadata):
"""
Class Constructor.
:param impression_listener: User provided impression listener.
:type impression_listener: ImpressionListener
:param sdk_metadata: SDK version, instance name & IP
:type sdk_metadata: splitio.client.util.SdkMetadata
"""
ImpressionListenerBase.__init__(self, impression_listener, sdk_metadata)
def log_impression(self, impression, attributes=None):
"""
Send an impression to the user-provided listener.
:param impression: Imression data
:type impression: dict
:param attributes: User provided attributes when calling get_treatment(s)
:type attributes: dict
"""
data = self._construct_data(impression, attributes)
try:
self.impression_listener.log_impression(data)
except Exception as exc: # pylint: disable=broad-except
raise ImpressionListenerException('Error in log_impression user\'s method is throwing exceptions') from exc
class ImpressionListenerWrapperAsync(ImpressionListenerBase): # pylint: disable=too-few-public-methods
"""
Impression listener safe-execution wrapper.
Wrapper in charge of building all the data that client would require in case
of adding some logic with the treatment and impression results.
"""
def __init__(self, impression_listener, sdk_metadata):
"""
Class Constructor.
:param impression_listener: User provided impression listener.
:type impression_listener: ImpressionListener
:param sdk_metadata: SDK version, instance name & IP
:type sdk_metadata: splitio.client.util.SdkMetadata
"""
ImpressionListenerBase.__init__(self, impression_listener, sdk_metadata)
async def log_impression(self, impression, attributes=None):
"""
Send an impression to the user-provided listener.
:param impression: Imression data
:type impression: dict
:param attributes: User provided attributes when calling get_treatment(s)
:type attributes: dict
"""
data = self._construct_data(impression, attributes)
try:
await self.impression_listener.log_impression(data)
except Exception as exc: # pylint: disable=broad-except
raise ImpressionListenerException('Error in log_impression user\'s method is throwing exceptions') from exc