def register_client(url: str, app_name: str, instance_id: str, metrics_interval: int, custom_headers: dict, custom_options: dict, supported_strategies: dict) -> 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 custom_headers: :param custom_options: :param supported_strategies: :return: true if registration successful, false if registration unsuccessful or exception. """ registation_request = { "appName": app_name, "instanceId": instance_id, "sdkVersion": "{}:{}".format(SDK_NAME, SDK_VERSION), "strategies": [*supported_strategies], "started": datetime.now(timezone.utc).isoformat(), "interval": metrics_interval } try: LOGGER.info("Registering unleash client with unleash @ %s", url) LOGGER.info("Registration request information: %s", registation_request) resp = requests.post(url + REGISTER_URL, data=json.dumps(registation_request), headers={ **custom_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 Exception: LOGGER.exception( "Unleash Client registration failed due to exception: %s", Exception) return False
def get_feature_toggles(url: str, app_name: str, instance_id: str, custom_headers: dict, custom_options: dict, project: str = None) -> 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: :param project: :return: Feature flags if successful, empty dict if not. """ try: LOGGER.info("Getting feature flag.") headers = { "UNLEASH-APPNAME": app_name, "UNLEASH-INSTANCEID": instance_id } base_url = f"{url}{FEATURES_URL}" base_params = {} if project: base_params = {'project': project} resp = requests.get(base_url, headers={ **custom_headers, **headers }, params=base_params, 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 {}
def send_metrics(url: str, request_body: dict, custom_headers: dict, custom_options: dict) -> bool: """ Attempts to send metrics to Unleash server Notes: * If unsuccessful (i.e. not HTTP status code 200), message will be logged :param url: :param app_name: :param instance_id: :param metrics_interval: :param custom_headers: :param custom_options: :return: true if registration successful, false if registration unsuccessful or exception. """ try: LOGGER.info("Sending messages to with unleash @ %s", url) LOGGER.info("unleash metrics information: %s", request_body) resp = requests.post(url + METRICS_URL, data=json.dumps(request_body), headers={ **custom_headers, **APPLICATION_HEADERS }, timeout=REQUEST_TIMEOUT, **custom_options) if resp.status_code != 202: log_resp_info(resp) LOGGER.warning("Unleash CLient metrics submission failed.") return False LOGGER.info("Unleash Client metrics successfully sent!") return True except Exception: LOGGER.exception( "Unleash Client metrics submission failed dye to exception: %s", Exception) return False
def get_feature_toggles(url: str, app_name: str, instance_id: str, custom_headers: 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: :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) if resp.status_code != 200: LOGGER.warning("unleash feature fetch failed!") raise Exception("unleash feature fetch failed!") return json.loads(resp.content) except Exception: LOGGER.exception("Unleash feature fetch failed!") return {}
def send_metrics(url, request_body, custom_headers): """ Attempts to send metrics to Unleash server Notes: * If unsuccessful (i.e. not HTTP status code 200), message will be logged :param url: :param app_name: :param instance_id: :param metrics_interval: :param custom_headers: :return: true if registration successful, false if registration unsuccessful or exception. """ try: LOGGER.info("Sending messages to with unleash @ %s", url) LOGGER.info("unleash metrics information: %s", request_body) headers = APPLICATION_HEADERS.copy() headers.update(custom_headers) resp = requests.post(url + METRICS_URL, data=json.dumps(request_body), headers=headers, timeout=REQUEST_TIMEOUT) if resp.status_code != 202: LOGGER.warning("unleash metrics submission failed.") return False LOGGER.info("unleash metrics successfully sent!") return True except Exception: LOGGER.exception("unleash metrics failed to send.") return False
def get_feature_toggles(url: str, app_name: str, instance_id: str, custom_headers: dict, custom_options: dict, project: str = None, cached_etag: str = '') -> Tuple[dict, str]: """ 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: :param project: :param cached_etag: :return: (Feature flags, etag) if successful, ({},'') if not """ try: LOGGER.info("Getting feature flag.") headers = { "UNLEASH-APPNAME": app_name, "UNLEASH-INSTANCEID": instance_id } if cached_etag: headers['If-None-Match'] = cached_etag base_url = f"{url}{FEATURES_URL}" base_params = {} if project: base_params = {'project': project} resp = requests.get(base_url, headers={ **custom_headers, **headers }, params=base_params, timeout=REQUEST_TIMEOUT, **custom_options) if resp.status_code not in [200, 304]: log_resp_info(resp) LOGGER.warning( "Unleash Client feature fetch failed due to unexpected HTTP status code." ) raise Exception("Unleash Client feature fetch failed!") etag = '' if 'etag' in resp.headers.keys(): etag = resp.headers['etag'] if resp.status_code == 304: return None, etag return resp.json(), etag except Exception as exc: LOGGER.exception( "Unleash Client feature fetch failed due to exception: %s", exc) return {}, ''