Skip to content

Commit c516a8e

Browse files
feat: add close function for Client (#8)
* feat: add close function for Client * style: format python code (pep8) * Update client.py Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent ac77af4 commit c516a8e

2 files changed

Lines changed: 44 additions & 19 deletions

File tree

demo.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@
1212
sync_mode='pooling',
1313
refresh_interval=3)
1414

15-
client = fp.Client('server-b8b7c58417680d9e76b1b8454326f357296b5003',
16-
# Server Side SDK Key for your project and environment
17-
config)
18-
19-
# create one user
20-
# key is for percentage rollout, normally use userId as key
21-
user = fp.User('00001', {'userId': '00001'})
22-
23-
# Toggle you want to use
24-
TOGGLE_KEY = 'feature_toggle02'
25-
26-
# get toggle result for this user
27-
is_open = client.value(TOGGLE_KEY, user, default=False)
28-
print('feature for this user is: ' + str(is_open))
29-
30-
is_open_detail = client.value_detail(TOGGLE_KEY, user, default=False)
31-
print('detail: ' + str(is_open_detail.reason))
32-
print('rule index: ' + str(is_open_detail.rule_index))
15+
# Server Side SDK Key for your project and environment
16+
SDK_KEY = 'server-b8b7c58417680d9e76b1b8454326f357296b5003'
17+
with fp.Client(SDK_KEY, config) as client:
18+
# create one user
19+
# key is for percentage rollout, normally use userId as key
20+
user = fp.User('00001', {'userId': '00001'})
21+
22+
# Toggle you want to use
23+
TOGGLE_KEY = 'feature_toggle02'
24+
25+
# get toggle result for this user
26+
is_open = client.value(TOGGLE_KEY, user, default=False)
27+
print('feature for this user is: ' + str(is_open))
28+
29+
is_open_detail = client.value_detail(TOGGLE_KEY, user, default=False)
30+
print('detail: ' + str(is_open_detail.reason))
31+
print('rule index: ' + str(is_open_detail.rule_index))

featureprobe/client.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import logging
1516
import time
1617
from typing import Any
1718

@@ -24,17 +25,42 @@
2425

2526

2627
class Client:
28+
__logger = logging.getLogger('FeatureProbe')
29+
2730
def __init__(self, sdk_key: str, config: Config = Config()):
2831
if empty_str(sdk_key):
2932
raise ValueError('sdk key must not be blank')
3033
context = Context(sdk_key, config)
3134
self._event_processor = config.event_processor_creator(context)
3235
self._data_repo = config.data_repository_creator(context)
33-
config.synchronizer_creator(context, self._data_repo).sync()
36+
self._synchronizer = config.synchronizer_creator(
37+
context, self._data_repo)
38+
self._synchronizer.sync()
39+
40+
def __enter__(self):
41+
return self
42+
43+
def __exit__(self, exc_type, exc_val, exc_tb):
44+
"""Alias for :func:`~featureprobe.Client.close`
45+
46+
Usage::
47+
48+
>>> import featureprobe as fp
49+
>>> with fp.Client('key_000') as client:
50+
>>> ...
51+
>>> # client will be closed here
52+
"""
53+
self.close()
3454

3555
def flush(self):
3656
self._event_processor.flush()
3757

58+
def close(self):
59+
Client.__logger.info('Closing FeatureProbe Client')
60+
self._synchronizer.close()
61+
self.flush()
62+
self._data_repo.close()
63+
3864
def value(self, toggle_key: str, user: User, default) -> Any:
3965
toggle = self._data_repo.get_toggle(toggle_key)
4066
segments = self._data_repo.get_all_segment()

0 commit comments

Comments
 (0)