33# you may not use this file except in compliance with the License.
44# You may obtain a copy of the License at
55#
6- # http ://www.apache.org/licenses/LICENSE-2.0
6+ # https ://www.apache.org/licenses/LICENSE-2.0
77#
88# Unless required by applicable law or agreed to in writing, software
99# distributed under the License is distributed on an "AS IS" BASIS,
1515
1616from typing import Optional , Any
1717
18- from optimizely import exceptions as optimizely_exception
1918from optimizely import logger as optimizely_logger
2019from optimizely .helpers .enums import Errors , OdpManagerConfig , OdpSegmentsCacheConfig
2120from optimizely .helpers .validator import are_odp_data_types_valid
@@ -56,13 +55,8 @@ def __init__(
5655 )
5756 self .segment_manager = OdpSegmentManager (segments_cache , logger = self .logger )
5857
59- if event_manager :
60- self .event_manager = event_manager
61- else :
62- self .event_manager = OdpEventManager (self .logger )
63-
58+ self .event_manager = self .event_manager or OdpEventManager (self .logger )
6459 self .segment_manager .odp_config = self .odp_config
65- self .event_manager .start (self .odp_config )
6660
6761 def fetch_qualified_segments (self , user_id : str , options : list [str ]) -> Optional [list [str ]]:
6862 if not self .enabled or not self .segment_manager :
@@ -94,17 +88,18 @@ def send_event(self, type: str, action: str, identifiers: dict[str, str], data:
9488 identifiers: A dictionary for identifiers.
9589 data: A dictionary for associated data. The default event data will be added to this data
9690 before sending to the ODP server.
97-
98- Raises custom exception if error is detected.
9991 """
10092 if not self .enabled or not self .event_manager :
101- raise optimizely_exception .OdpNotEnabled (Errors .ODP_NOT_ENABLED )
93+ self .logger .error (Errors .ODP_NOT_ENABLED )
94+ return
10295
10396 if self .odp_config .odp_state () == OdpConfigState .NOT_INTEGRATED :
104- raise optimizely_exception .OdpNotIntegrated (Errors .ODP_NOT_INTEGRATED )
97+ self .logger .error (Errors .ODP_NOT_INTEGRATED )
98+ return
10599
106100 if not are_odp_data_types_valid (data ):
107- raise optimizely_exception .OdpInvalidData (Errors .ODP_INVALID_DATA )
101+ self .logger .error (Errors .ODP_INVALID_DATA )
102+ return
108103
109104 self .event_manager .send_event (type , action , identifiers , data )
110105
@@ -122,5 +117,14 @@ def update_odp_config(self, api_key: Optional[str], api_host: Optional[str],
122117 if self .segment_manager :
123118 self .segment_manager .reset ()
124119
125- if self .event_manager :
120+ if not self .event_manager :
121+ return
122+
123+ if self .event_manager .is_running :
126124 self .event_manager .update_config ()
125+ elif self .odp_config .odp_state () == OdpConfigState .INTEGRATED :
126+ self .event_manager .start (self .odp_config )
127+
128+ def close (self ) -> None :
129+ if self .enabled and self .event_manager :
130+ self .event_manager .stop ()
0 commit comments