From 20e18562925f5e8dd15fe794adeb7ac9cbe0909a Mon Sep 17 00:00:00 2001 From: "Mahadik, Mukul Chandrakant" Date: Thu, 28 Mar 2024 21:45:28 -0700 Subject: [PATCH] Reading push.json values from environment variables Push.json.sample just has 4 key-value pairs which can be changed to ENV variables internally. Hence, need to write code to read from Env variables. Changing emission/net/ext_service/push/notify_interface.py to read from env variables. Using helper class config.py, based on existing template. First reading from .json file if exists, else read from env variables. If env variables are all None, then say that values not configured. --- emission/net/ext_service/push/config.py | 37 +++++++++++++++++++ .../net/ext_service/push/notify_interface.py | 6 +-- 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 emission/net/ext_service/push/config.py diff --git a/emission/net/ext_service/push/config.py b/emission/net/ext_service/push/config.py new file mode 100644 index 000000000..61a9946c0 --- /dev/null +++ b/emission/net/ext_service/push/config.py @@ -0,0 +1,37 @@ +import json +import logging +import os + +def get_config_data_from_env(): + config_data_env = { + "provider": os.getenv("PUSH_PROVIDER"), + "server_auth_token": os.getenv("PUSH_SERVER_AUTH_TOKEN"), + "app_package_name": os.getenv("PUSH_APP_PACKAGE_NAME"), + "ios_token_format": os.getenv("PUSH_IOS_TOKEN_FORMAT") + } + return config_data_env + +def get_config_data(): + try: + config_file = open('conf/net/ext_service/push.json') + ret_val = json.load(config_file) + config_file.close() + except: + logging.debug("net.ext_service.push.json not configured, checking environment variables...") + ret_val = get_config_data_from_env() + # Check if all PUSH environment variables are not set + if (not any(ret_val.values())): + raise TypeError + return ret_val + +try: + config_data = get_config_data() +except: + logging.debug("All push environment variables are set to None") + +def get_config(): + return config_data + +def reload_config(): + global config_data + config_data = get_config_data() diff --git a/emission/net/ext_service/push/notify_interface.py b/emission/net/ext_service/push/notify_interface.py index 6b94857f6..6d8332c1b 100644 --- a/emission/net/ext_service/push/notify_interface.py +++ b/emission/net/ext_service/push/notify_interface.py @@ -11,15 +11,15 @@ import logging import importlib +import emission.net.ext_service.push.config as pc + # Note that the URL is hardcoded because the API endpoints are not standardized. # If we change a push provider, we will need to modify to match their endpoints. # Hardcoding will remind us of this :) # We can revisit this if push providers eventually decide to standardize... try: - push_config_file = open('conf/net/ext_service/push.json') - push_config = json.load(push_config_file) - push_config_file.close() + push_config = pc.get_config_data() except: logging.warning("push service not configured, push notifications not supported")