From 631a609f87c59640b0e251ed56c182601b988eb0 Mon Sep 17 00:00:00 2001 From: Eduard Portmann Date: Wed, 21 Aug 2024 01:25:52 +0200 Subject: [PATCH] Add support for custom Gateway API URL endpoints in CLI --- README.rst | 15 +++++++++++++++ threema/gateway/bin/gateway_client.py | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/README.rst b/README.rst index a613625..03f3552 100644 --- a/README.rst +++ b/README.rst @@ -67,6 +67,21 @@ the Threema gateway. Run the following command to see usage information: $ threema-gateway --help +Gateway API Endpoint +-------------------- + +The default Gateway API Endpoint URL used is https://msgapi.threema.ch/. + +If you are a Threema OnPrem customer or have another reason to use a different +Gateway API Endpoint, you may override the URL as follows: + +.. code-block:: bash + + $ export GATEWAY_API_URL=https://onprem.myinstance.tld/msgapi + +Any following calls to ``threema-gateway`` will then use the supplied Gateway +API Endpoint URL. + Examples ******** diff --git a/threema/gateway/bin/gateway_client.py b/threema/gateway/bin/gateway_client.py index 4b3731b..532b67f 100755 --- a/threema/gateway/bin/gateway_client.py +++ b/threema/gateway/bin/gateway_client.py @@ -36,12 +36,24 @@ # Apply mock URL when starting CLI in debug mode _test_port = os.environ.get('THREEMA_TEST_API') +_api_url = os.environ.get('GATEWAY_API_URL') if _test_port is not None: + if _api_url is not None: + raise RuntimeError('GATEWAY_API_URL cannot be set alongside THREEMA_TEST_API') _mock_url = 'http://{}:{}'.format('127.0.0.1', _test_port) Connection.urls = {key: value.replace('https://msgapi.threema.ch', _mock_url) for key, value in Connection.urls.items()} click.echo(('WARNING: Currently running in test mode!' 'The Threema Gateway Server will not be contacted!'), err=True) +else: + if _api_url is not None: + if not _api_url.startswith('https://'): + raise RuntimeError('GATEWAY_API_URL must begin with "https://"') + Connection.urls = {key: value.replace( + 'https://msgapi.threema.ch', + _api_url.rstrip('/') + ) + for key, value in Connection.urls.items()} class _MockConnection(AioRunMixin):