forked from Unleash/unleash-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.py
47 lines (38 loc) · 1.54 KB
/
features.py
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
import requests
from UnleashClient.constants import REQUEST_TIMEOUT, FEATURES_URL
from UnleashClient.utils import LOGGER, log_resp_info
# pylint: disable=broad-except
def get_feature_toggles(url: str,
app_name: str,
instance_id: str,
custom_headers: dict,
custom_options: dict) -> dict:
"""
Retrieves feature flags from unleash central server.
Notes:
* If unsuccessful (i.e. not HTTP status code 200), exception will be caught and logged.
This is to allow "safe" error handling if unleash server goes down.
:param url:
:param app_name:
:param instance_id:
:param custom_headers:
:param custom_options:
:return: Feature flags if successful, empty dict if not.
"""
try:
LOGGER.info("Getting feature flag.")
headers = {
"UNLEASH-APPNAME": app_name,
"UNLEASH-INSTANCEID": instance_id
}
resp = requests.get(url + FEATURES_URL,
headers={**custom_headers, **headers},
timeout=REQUEST_TIMEOUT, **custom_options)
if resp.status_code != 200:
log_resp_info(resp)
LOGGER.warning("Unleash Client feature fetch failed due to unexpected HTTP status code.")
raise Exception("Unleash Client feature fetch failed!")
return resp.json()
except Exception as exc:
LOGGER.exception("Unleash Client feature fetch failed due to exception: %s", exc)
return {}