-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathnotification.py
More file actions
227 lines (173 loc) · 6.57 KB
/
notification.py
File metadata and controls
227 lines (173 loc) · 6.57 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""Notification Module"""
import json
from enum import Enum
class Type(Enum):
"""Notification Type."""
SPLIT_UPDATE = 'SPLIT_UPDATE'
SPLIT_KILL = 'SPLIT_KILL'
SEGMENT_UPDATE = 'SEGMENT_UPDATE'
CONTROL = 'CONTROL'
class Control(Enum):
"""Control Type."""
STREAMING_PAUSED = 'STREAMING_PAUSED'
STREAMING_RESUMED = 'STREAMING_RESUMED'
STREAMING_DISABLED = 'STREAMING_DISABLED'
class ControlNotification(object): # pylint: disable=too-many-instance-attributes
"""ControlNotification model object."""
def __init__(self, channel, notification_type, control_type):
"""
Class constructor.
:param channel: Channel of incoming notification
:type channel: str
:param notification_type: Type of incoming notification
:type notification_type: str
:param control_type: Control type of incoming CONTROL notification.
:type control_type: str
"""
self._channel = channel
self._notification_type = Type(notification_type)
self._control_type = Control(control_type)
@property
def channel(self):
return self._channel
@property
def control_type(self):
return self._control_type
@property
def notification_type(self):
return self._notification_type
class SegmentChangeNotification(object): # pylint: disable=too-many-instance-attributes
"""SegmentChangeNotification model object."""
def __init__(self, channel, notification_type, change_number, segment_name):
"""
Class constructor.
:param channel: Channel of incoming notification
:type channel: str
:param notification_type: Type of incoming notification
:type notification_type: str
:param change_number: ChangeNumber of incoming notification.
:type change_number: int
:param segment_name: Segment Name of incoming notification.
:type segment_name: str
"""
self._channel = channel
self._notification_type = Type(notification_type)
self._change_number = change_number
self._segment_name = segment_name
@property
def channel(self):
return self._channel
@property
def change_number(self):
return self._change_number
@property
def notification_type(self):
return self._notification_type
@property
def segment_name(self):
return self._segment_name
class SplitChangeNotification(object): # pylint: disable=too-many-instance-attributes
"""SplitChangeNotification model object."""
def __init__(self, channel, notification_type, change_number):
"""
Class constructor.
:param channel: Channel of incoming notification
:type channel: str
:param notification_type: Type of incoming notification
:type notification_type: str
:param change_number: ChangeNumber of incoming notification.
:type change_number: int
"""
self._channel = channel
self._notification_type = Type(notification_type)
self._change_number = change_number
@property
def channel(self):
return self._channel
@property
def change_number(self):
return self._change_number
@property
def notification_type(self):
return self._notification_type
class SplitKillNotification(object): # pylint: disable=too-many-instance-attributes
"""SplitKillNotification model object."""
def __init__(self, channel, notification_type, change_number, default_treatment, split_name):
"""
Class constructor.
:param channel: Channel of incoming notification
:type channel: str
:param notification_type: Type of incoming notification
:type notification_type: str
:param change_number: ChangeNumber of incoming notification.
:type change_number: int
:param default_treatment: Default treatment of incoming SPLIT_KILL notification.
:type default_treatment: str
:param split_name: Split Name of incoming SPLIT or SPLIT_KILL notification.
:type split_name: str
"""
self._channel = channel
self._notification_type = Type(notification_type)
self._change_number = change_number
self._default_treatment = default_treatment
self._split_name = split_name
@property
def channel(self):
return self._channel
@property
def change_number(self):
return self._change_number
@property
def default_treatment(self):
return self._default_treatment
@property
def notification_type(self):
return self._notification_type
@property
def split_name(self):
return self._split_name
class SdkInternalEventNotification(object): # pylint: disable=too-many-instance-attributes
"""SdkInternalEventNotification model object."""
def __init__(self, internal_event, metadata):
"""
Class constructor.
:param internal_event: internal event object
:type channel: SdkInternalEvent
:param metadata: metadata associated with event
:type change_number: EventsMetadata
"""
self._internal_event = internal_event
self._metadata = metadata
@property
def internal_event(self):
return self._internal_event
@property
def metadata(self):
return self._metadata
_NOTIFICATION_MAPPERS = {
Type.SPLIT_UPDATE: lambda c, d: SplitChangeNotification(c, Type.SPLIT_UPDATE, d['changeNumber']),
Type.SPLIT_KILL: lambda c, d: SplitKillNotification(c, Type.SPLIT_KILL, d['changeNumber'], d['defaultTreatment'], d['splitName']),
Type.SEGMENT_UPDATE: lambda c, d: SegmentChangeNotification(c, Type.SEGMENT_UPDATE, d['changeNumber'], d['segmentName']),
Type.CONTROL: lambda c, d: ControlNotification(c, Type.CONTROL, d['controlType'])
}
def wrap_notification(raw_data, channel):
"""
Parse notification from raw notification payload
:param raw_data: data
:type raw_data: str
:param channel: Channel of incoming notification
:type channel: str
"""
try:
if channel is None:
raise ValueError("channel cannot be None.")
raw_data = json.loads(raw_data)
notification_type = Type(raw_data['type'])
mapper = _NOTIFICATION_MAPPERS[notification_type]
return mapper(channel, raw_data)
except ValueError:
raise ValueError("Wrong notification type received.")
except KeyError:
raise KeyError("Could not parse notification.")
except TypeError:
raise TypeError("Wrong JSON format.")