|
| 1 | +import json |
| 2 | +from datetime import datetime, timezone |
| 3 | +from platform import python_implementation, python_version |
| 4 | + |
| 5 | +import niquests as requests |
| 6 | +import yggdrasil_engine |
| 7 | +from niquests.exceptions import InvalidHeader, InvalidSchema, InvalidURL, MissingSchema |
| 8 | + |
| 9 | +from ...constants import ( |
| 10 | + APPLICATION_HEADERS, |
| 11 | + CLIENT_SPEC_VERSION, |
| 12 | + REGISTER_URL, |
| 13 | + SDK_NAME, |
| 14 | + SDK_VERSION, |
| 15 | +) |
| 16 | +from ...utils import LOGGER, log_resp_info |
| 17 | + |
| 18 | + |
| 19 | +# pylint: disable=broad-except |
| 20 | +async def register_client( |
| 21 | + url: str, |
| 22 | + app_name: str, |
| 23 | + instance_id: str, |
| 24 | + connection_id: str, |
| 25 | + metrics_interval: int, |
| 26 | + headers: dict, |
| 27 | + custom_options: dict, |
| 28 | + supported_strategies: dict, |
| 29 | + request_timeout: int, |
| 30 | +) -> bool: |
| 31 | + """ |
| 32 | + Attempts to register client with unleash server asynchronously. |
| 33 | +
|
| 34 | + Notes: |
| 35 | + * If unsuccessful (i.e. not HTTP status code 202), exception will be caught and logged. |
| 36 | + This is to allow "safe" error handling if unleash server goes down. |
| 37 | +
|
| 38 | + :param url: |
| 39 | + :param app_name: |
| 40 | + :param instance_id: |
| 41 | + :param metrics_interval: |
| 42 | + :param headers: |
| 43 | + :param custom_options: |
| 44 | + :param supported_strategies: |
| 45 | + :param request_timeout: |
| 46 | + :return: true if registration successful, false if registration unsuccessful or exception. |
| 47 | + """ |
| 48 | + registration_request = { |
| 49 | + "appName": app_name, |
| 50 | + "instanceId": instance_id, |
| 51 | + "connectionId": connection_id, |
| 52 | + "sdkVersion": f"{SDK_NAME}:{SDK_VERSION}", |
| 53 | + "strategies": [*supported_strategies], |
| 54 | + "started": datetime.now(timezone.utc).isoformat(), |
| 55 | + "interval": metrics_interval, |
| 56 | + "platformName": python_implementation(), |
| 57 | + "platformVersion": python_version(), |
| 58 | + "yggdrasilVersion": yggdrasil_engine.__yggdrasil_core_version__, |
| 59 | + "specVersion": CLIENT_SPEC_VERSION, |
| 60 | + } |
| 61 | + |
| 62 | + try: |
| 63 | + LOGGER.info("Registering unleash client with unleash @ %s", url) |
| 64 | + LOGGER.info("Registration request information: %s", registration_request) |
| 65 | + |
| 66 | + async with requests.AsyncSession() as session: |
| 67 | + resp = await session.post( |
| 68 | + url + REGISTER_URL, |
| 69 | + data=json.dumps(registration_request), |
| 70 | + headers={**headers, **APPLICATION_HEADERS}, |
| 71 | + timeout=request_timeout, |
| 72 | + **custom_options, |
| 73 | + ) |
| 74 | + |
| 75 | + if resp.status_code not in {200, 202}: |
| 76 | + log_resp_info(resp) |
| 77 | + LOGGER.warning( |
| 78 | + "Unleash Client registration failed due to unexpected HTTP status code: %s", |
| 79 | + resp.status_code, |
| 80 | + ) |
| 81 | + return False |
| 82 | + |
| 83 | + LOGGER.info("Unleash Client successfully registered!") |
| 84 | + |
| 85 | + return True |
| 86 | + except (MissingSchema, InvalidSchema, InvalidHeader, InvalidURL) as exc: |
| 87 | + LOGGER.exception( |
| 88 | + "Unleash Client registration failed fatally due to exception: %s", exc |
| 89 | + ) |
| 90 | + raise exc |
| 91 | + except requests.RequestException as exc: |
| 92 | + LOGGER.exception("Unleash Client registration failed due to exception: %s", exc) |
| 93 | + |
| 94 | + return False |
0 commit comments