Skip to content

Commit

Permalink
Support GATEWAY_API_URL env var
Browse files Browse the repository at this point in the history
  • Loading branch information
threema-eduard committed Jul 30, 2024
1 parent a36e8ed commit c307886
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
********

Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions threema/gateway/bin/gateway_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c307886

Please sign in to comment.