-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathregister.py
92 lines (80 loc) · 2.81 KB
/
register.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
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
import json
from datetime import datetime, timezone
from platform import python_implementation, python_version
import requests
import yggdrasil_engine
from requests.exceptions import InvalidHeader, InvalidSchema, InvalidURL, MissingSchema
from UnleashClient.constants import (
APPLICATION_HEADERS,
CLIENT_SPEC_VERSION,
REGISTER_URL,
SDK_NAME,
SDK_VERSION,
)
from UnleashClient.utils import LOGGER, log_resp_info
# pylint: disable=broad-except
def register_client(
url: str,
app_name: str,
instance_id: str,
connection_id: str,
metrics_interval: int,
headers: dict,
custom_options: dict,
supported_strategies: dict,
request_timeout: int,
) -> bool:
"""
Attempts to register client with unleash server.
Notes:
* If unsuccessful (i.e. not HTTP status code 202), 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 metrics_interval:
:param headers:
:param custom_options:
:param supported_strategies:
:param request_timeout:
:return: true if registration successful, false if registration unsuccessful or exception.
"""
registration_request = {
"appName": app_name,
"instanceId": instance_id,
"connectionId": connection_id,
"sdkVersion": f"{SDK_NAME}:{SDK_VERSION}",
"strategies": [*supported_strategies],
"started": datetime.now(timezone.utc).isoformat(),
"interval": metrics_interval,
"platformName": python_implementation(),
"platformVersion": python_version(),
"yggdrasilVersion": yggdrasil_engine.__yggdrasil_core_version__,
"specVersion": CLIENT_SPEC_VERSION,
}
try:
LOGGER.info("Registering unleash client with unleash @ %s", url)
LOGGER.info("Registration request information: %s", registration_request)
resp = requests.post(
url + REGISTER_URL,
data=json.dumps(registration_request),
headers={**headers, **APPLICATION_HEADERS},
timeout=request_timeout,
**custom_options,
)
if resp.status_code != 202:
log_resp_info(resp)
LOGGER.warning(
"Unleash Client registration failed due to unexpected HTTP status code."
)
return False
LOGGER.info("Unleash Client successfully registered!")
return True
except (MissingSchema, InvalidSchema, InvalidHeader, InvalidURL) as exc:
LOGGER.exception(
"Unleash Client registration failed fatally due to exception: %s", exc
)
raise exc
except requests.RequestException as exc:
LOGGER.exception("Unleash Client registration failed due to exception: %s", exc)
return False