Skip to content

Commit

Permalink
Updates user agent to fix 403 on Ropsten
Browse files Browse the repository at this point in the history
Ropsten seems to be blacklisting `python-requests` user agent, refs:
- AndreMiras/EtherollApp#156
- https://www.reddit.com/r/etherscan/comments/dtg8xl/
- corpetty/py-etherscan-api#70
  • Loading branch information
AndreMiras committed Nov 8, 2019
1 parent 406570a commit 5275dfd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## [Unreleased]

- Fix Ropsten 403 errors


## [20191107]

- API key refactoring
Expand Down
18 changes: 18 additions & 0 deletions pyetheroll/etheroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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()
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/test_etheroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 5275dfd

Please sign in to comment.