diff --git a/README.rst b/README.rst index 5f0ef2e..bf88e4c 100644 --- a/README.rst +++ b/README.rst @@ -69,6 +69,18 @@ the Threema gateway. Run the following command to see usage information: $ threema-gateway --help +Setting a different API url +--------------------------- + +The default MsgApi URL points to https://msgapi.threema.ch/. + +If you are a Threema OnPrem customer or have another reason +to use a different MsgApi endpoint, you may set an environment variable as follows: + +.. code-block:: bash + + $ GATEWAY_API_URL=https://onprem.myinstance.tld/msgapi threema-gateway ... + Examples ******** diff --git a/tests/test_cli.py b/tests/test_cli.py index 1300952..dad599f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -252,7 +252,7 @@ async def test_invalid_id(self, cli): @pytest.mark.asyncio async def test_insufficient_credits(self, cli): with pytest.raises(subprocess.CalledProcessError) as exc_info: - id_, secret = pytest.msgapi['msgapi']['nocredit_id'],\ + id_, secret = pytest.msgapi['msgapi']['nocredit_id'], \ pytest.msgapi['msgapi']['secret'] await cli('send-simple', 'ECHOECHO', id_, secret, input='!') assert 'Insufficient credits' in exc_info.value.output diff --git a/threema/gateway/bin/gateway_client.py b/threema/gateway/bin/gateway_client.py index 2672a08..7a9f49d 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):