Describe the bug
TransactionServiceApi.from_ethereum_client ignores SAFE_TRANSACTION_SERVICE_API_KEY, so no Authorization header is sent even when the variable is set. SafeBaseAPI.from_ethereum_client passes api_key=None explicitly (safe_eth/safe/api/base_api.py:69-75), which overrides the default in TransactionServiceApi.__init__ (api_key=os.environ.get("SAFE_TRANSACTION_SERVICE_API_KEY"), transaction_service_api.py:66).
safe-cli builds its Transaction Service client this way, so in tx-service mode it keeps asking for the variable even when it is exported, and requests are sent without the key.
A related effect: because the default is a default argument, the variable is read once at import time, so setting it after safe_eth is imported has no effect on the constructor either.
To Reproduce
import os
from unittest import mock
os.environ["SAFE_TRANSACTION_SERVICE_API_KEY"] = "key-set-before-import"
from safe_eth.eth import EthereumNetwork
from safe_eth.safe.api import TransactionServiceApi
client = mock.MagicMock()
client.get_network.return_value = EthereumNetwork.SEPOLIA
api = TransactionServiceApi.from_ethereum_client(client)
print(api.api_key, api._get_request_headers().get("Authorization"))
os.environ["SAFE_TRANSACTION_SERVICE_API_KEY"] = "key-set-after-import"
print(TransactionServiceApi(EthereumNetwork.SEPOLIA).api_key)
Output on main (416a252):
None None
key-set-before-import
Expected behavior
from_ethereum_client uses the key from SAFE_TRANSACTION_SERVICE_API_KEY when no api_key is passed, and the variable is read when the client is created: key-set-before-import Bearer key-set-before-import, then key-set-after-import.
Additional context
safe-cli 1.9.0; we worked around it by setting safe_tx_service.api_key on the operator after creating it. I have a fix with tests and will open a PR.
Describe the bug
TransactionServiceApi.from_ethereum_clientignoresSAFE_TRANSACTION_SERVICE_API_KEY, so noAuthorizationheader is sent even when the variable is set.SafeBaseAPI.from_ethereum_clientpassesapi_key=Noneexplicitly (safe_eth/safe/api/base_api.py:69-75), which overrides the default inTransactionServiceApi.__init__(api_key=os.environ.get("SAFE_TRANSACTION_SERVICE_API_KEY"),transaction_service_api.py:66).safe-cli builds its Transaction Service client this way, so in tx-service mode it keeps asking for the variable even when it is exported, and requests are sent without the key.
A related effect: because the default is a default argument, the variable is read once at import time, so setting it after
safe_ethis imported has no effect on the constructor either.To Reproduce
Output on
main(416a252):Expected behavior
from_ethereum_clientuses the key fromSAFE_TRANSACTION_SERVICE_API_KEYwhen noapi_keyis passed, and the variable is read when the client is created:key-set-before-import Bearer key-set-before-import, thenkey-set-after-import.Additional context
safe-cli 1.9.0; we worked around it by setting
safe_tx_service.api_keyon the operator after creating it. I have a fix with tests and will open a PR.