diff --git a/CHANGELOG.md b/CHANGELOG.md index 05e36ad..e807ab5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## [Unreleased] + + - Fix Ropsten 403 errors + + ## [20191107] - API key refactoring diff --git a/pyetheroll/etheroll.py b/pyetheroll/etheroll.py index b65d370..dc34e9d 100644 --- a/pyetheroll/etheroll.py +++ b/pyetheroll/etheroll.py @@ -37,6 +37,9 @@ # as we still want some very outdate data to get wiped at some point "expire_after": 30 * 24 * 60 * 60, } +REQUESTS_HEADERS = { + "User-Agent": "https://github.com/AndreMiras/pyetheroll", +} def abi_definitions(contract_abi, typ): @@ -60,6 +63,15 @@ def merge_logs(bet_logs, bet_results_logs): return merged_logs +def update_user_agent(headers): + """ + Default `requests` user agent is blocked on Ropsten, refs: + - https://github.com/corpetty/py-etherscan-api/issues/70 + - https://www.reddit.com/r/etherscan/comments/dtg8xl/ + """ + return dict(headers, **REQUESTS_HEADERS) + + class Etheroll: _etheroll = None @@ -97,6 +109,8 @@ def __init__( self.etherscan_contract_api = ChainEtherscanContract( address=self.contract_address, api_key=self.etherscan_api_key ) + self.etherscan_contract_api.http.headers = update_user_agent( + self.etherscan_contract_api.http.headers) self.contract_abi = json.loads( self.etherscan_contract_api.get_abi() ) @@ -268,6 +282,8 @@ def get_transaction_page( etherscan_account_api = self.ChainEtherscanAccount( address=address, api_key=self.etherscan_api_key ) + etherscan_account_api.http.headers = update_user_agent( + etherscan_account_api.http.headers) sort = "desc" try: transactions = etherscan_account_api.get_transaction_page( @@ -592,6 +608,8 @@ def get_balance(self, address): etherscan_account_api = self.ChainEtherscanAccount( address=address, api_key=self.etherscan_api_key ) + etherscan_account_api.http.headers = update_user_agent( + etherscan_account_api.http.headers) balance_wei = int(etherscan_account_api.get_balance()) balance_eth = round(balance_wei / 1e18, ROUND_DIGITS) return balance_eth diff --git a/tests/test_etheroll.py b/tests/test_etheroll.py index afe6eec..f6f693b 100644 --- a/tests/test_etheroll.py +++ b/tests/test_etheroll.py @@ -1128,6 +1128,7 @@ def test_get_transaction_page(self): contract_address = "0x048717Ea892F23Fb0126F00640e2b18072efd9D2" expected_transactions = mock.sentinel m_ChainEtherscanAccount = mock.Mock(spec=EtherscanAccount) + m_ChainEtherscanAccount.return_value.http.headers = {} m_ChainEtherscanAccount.return_value.get_transaction_page = mock.Mock( return_value=expected_transactions )