api: Add support for Environment Variables.

This adds support for controlling the basic configuration (user, API
key, etc.) of the Zulip API bindings via environment variables.

Fixes #3364.

Tweaked by tabbott to update variable names and document in README.md.
This commit is contained in:
Rohitt Vashishtha 2017-01-27 03:30:29 +05:30 committed by Tim Abbott
parent 46e7e05516
commit 863df67150
2 changed files with 23 additions and 1 deletions

View file

@ -44,7 +44,12 @@ Alternatively, you may explicitly use "--user", "--api-key", and
`--site` in our examples, which is especially useful when testing. If `--site` in our examples, which is especially useful when testing. If
you are running several bots which share a home directory, we you are running several bots which share a home directory, we
recommend using `--config` to specify the path to the `zuliprc` file recommend using `--config` to specify the path to the `zuliprc` file
for a specific bot. for a specific bot. Finally, you can control the defaults for all of
these variables using the environment variables `ZULIP_CONFIG`,
`ZULIP_API_KEY`, `ZULIP_EMAIL`, `ZULIP_SITE`, `ZULIP_CERT`,
`ZULIP_CERT_KEY`, and `ZULIP_CERT_BUNDLE`. Command-line options take
precedence over environment variables take precedence over the config
files.
The command line equivalents for other configuration options are: The command line equivalents for other configuration options are:

View file

@ -186,8 +186,25 @@ class Client(object):
if client is None: if client is None:
client = _default_client() client = _default_client()
# Fill values from Environment Variables if not available in Constructor
if config_file is None:
config_file = os.environ.get("ZULIP_CONFIG")
if api_key is None:
api_key = os.environ.get("ZULIP_API_KEY")
if email is None:
email = os.environ.get("ZULIP_EMAIL")
if site is None:
site = os.environ.get("ZULIP_SITE")
if client_cert is None:
client_cert = os.environ.get("ZULIP_CERT")
if client_cert_key is None:
client_cert_key = os.environ.get("ZULIP_CERT_KEY")
if cert_bundle is None:
cert_bundle = os.environ.get("ZULIP_CERT_BUNDLE")
if config_file is None: if config_file is None:
config_file = get_default_config_filename() config_file = get_default_config_filename()
if os.path.exists(config_file): if os.path.exists(config_file):
config = SafeConfigParser() config = SafeConfigParser()
with open(config_file, 'r') as f: with open(config_file, 'r') as f: