Skip to content

Commit

Permalink
Allow usage of Infura's v3 API
Browse files Browse the repository at this point in the history
  • Loading branch information
kclowes committed Feb 20, 2019
1 parent 4f63b2f commit 5856aac
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
52 changes: 44 additions & 8 deletions tests/core/providers/test_auto_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,74 @@ def test_load_provider_from_env(monkeypatch, uri, expected_type, expected_attrs)
assert getattr(provider, attr) == val


@pytest.mark.parametrize('environ_name', ['INFURA_API_KEY', 'WEB3_INFURA_API_KEY'])
@pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID'])
def test_web3_auto_infura_empty_key(monkeypatch, caplog, environ_name):
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https')
monkeypatch.setenv(environ_name, '')

importlib.reload(infura)
assert len(caplog.record_tuples) == 1
logger, level, msg = caplog.record_tuples[0]
assert 'WEB3_INFURA_API_KEY' in msg
assert 'WEB3_INFURA_PROJECT_ID' in msg
assert level == logging.WARNING

w3 = infura.w3
assert isinstance(w3.provider, HTTPProvider)
assert getattr(w3.provider, 'endpoint_uri') == 'https://mainnet.infura.io/'


@pytest.mark.parametrize('environ_name', ['INFURA_API_KEY', 'WEB3_INFURA_API_KEY'])
@pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID'])
def test_web3_auto_infura_deleted_key(monkeypatch, caplog, environ_name):
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https')
monkeypatch.delenv(environ_name, raising=False)

importlib.reload(infura)
assert len(caplog.record_tuples) == 1
logger, level, msg = caplog.record_tuples[0]
assert 'WEB3_INFURA_API_KEY' in msg
assert 'WEB3_INFURA_PROJECT_ID' in msg
assert level == logging.WARNING

w3 = infura.w3
assert isinstance(w3.provider, HTTPProvider)
assert getattr(w3.provider, 'endpoint_uri') == 'https://mainnet.infura.io/'


@pytest.mark.parametrize('environ_name', ['INFURA_API_KEY', 'WEB3_INFURA_API_KEY'])
@pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID'])
def test_web3_auto_infura_websocket_empty_key(monkeypatch, caplog, environ_name):
monkeypatch.setenv(environ_name, '')

importlib.reload(infura)
assert len(caplog.record_tuples) == 1
logger, level, msg = caplog.record_tuples[0]
assert 'WEB3_INFURA_PROJECT_ID' in msg
assert level == logging.WARNING

w3 = infura.w3
assert isinstance(w3.provider, WebsocketProvider)
assert getattr(w3.provider, 'endpoint_uri') == 'wss://mainnet.infura.io/ws/'


@pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID'])
def test_web3_auto_infura_websocket_deleted_key(monkeypatch, caplog, environ_name):
monkeypatch.delenv(environ_name, raising=False)

importlib.reload(infura)
assert len(caplog.record_tuples) == 1
logger, level, msg = caplog.record_tuples[0]
assert 'WEB3_INFURA_PROJECT_ID' in msg
assert level == logging.WARNING

w3 = infura.w3
assert isinstance(w3.provider, WebsocketProvider)
assert getattr(w3.provider, 'endpoint_uri') == 'wss://mainnet.infura.io/ws/'


@pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID'])
def test_web3_auto_infura(monkeypatch, caplog, environ_name):
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https')
API_KEY = 'aoeuhtns'
monkeypatch.setenv(environ_name, API_KEY)
expected_url = 'https://%s/%s' % (infura.INFURA_MAINNET_DOMAIN, API_KEY)
expected_url = 'https://%s/v3/%s' % (infura.INFURA_MAINNET_DOMAIN, API_KEY)

importlib.reload(infura)
assert len(caplog.record_tuples) == 0
Expand All @@ -80,10 +110,16 @@ def test_web3_auto_infura(monkeypatch, caplog, environ_name):
assert getattr(w3.provider, 'endpoint_uri') == expected_url


def test_web3_auto_infura_websocket_default(caplog):
@pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID'])
def test_web3_auto_infura_websocket_default(monkeypatch, caplog, environ_name):
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'wss')
API_KEY = 'aoeuhtns'
monkeypatch.setenv(environ_name, API_KEY)
expected_url = 'wss://%s/ws/v3/%s' % (infura.INFURA_MAINNET_DOMAIN, API_KEY)

importlib.reload(infura)
assert len(caplog.record_tuples) == 0

w3 = infura.w3
assert isinstance(w3.provider, WebsocketProvider)
assert getattr(w3.provider, 'endpoint_uri') == 'wss://mainnet.infura.io/ws'
assert getattr(w3.provider, 'endpoint_uri') == expected_url
2 changes: 1 addition & 1 deletion web3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
if sys.version_info < (3, 5):
raise EnvironmentError(
"Python 3.5 or above is required. "
"Note that support for Python 3.5 will be remove in web3.py v5")
"Note that support for Python 3.5 will be removed in web3.py v5")

from eth_account import Account # noqa: E402
from web3.main import Web3 # noqa: E402
Expand Down
26 changes: 14 additions & 12 deletions web3/auto/infura/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@


def load_api_key():
# at web3py v5, drop old variable name INFURA_API_KEY
key = os.environ.get(
'WEB3_INFURA_API_KEY',
os.environ.get('INFURA_API_KEY', '')
)
# in web3py v6 remove outdated WEB3_INFURA_API_KEY
key = os.environ.get('WEB3_INFURA_PROJECT_ID',
os.environ.get('WEB3_INFURA_API_KEY', ''))
if key == '':
logging.getLogger('web3.auto.infura').warning(
"No Infura API Key found. Add environment variable WEB3_INFURA_API_KEY to ensure "
"continued API access. New keys are available at https://infura.io/register"
"No Infura Project ID found. Add environment variable WEB3_INFURA_PROJECT_ID to "
" ensure continued API access after March 27th. "
"New keys are available at https://infura.io/register"
)
return key


def build_infura_url(domain):
scheme = os.environ.get('WEB3_INFURA_SCHEME', WEBSOCKET_SCHEME)

if scheme == WEBSOCKET_SCHEME:
# websockets doesn't use the API key (yet?)
return "%s://%s/ws" % (scheme, domain)
key = load_api_key()

if key and scheme == WEBSOCKET_SCHEME:
return "%s://%s/ws/v3/%s" % (scheme, domain, key)
elif key and scheme == HTTP_SCHEME:
return "%s://%s/v3/%s" % (scheme, domain, key)
elif scheme == WEBSOCKET_SCHEME:
return "%s://%s/ws/" % (scheme, domain)
elif scheme == HTTP_SCHEME:
key = load_api_key()
return "%s://%s/%s" % (scheme, domain, key)
else:
raise ValidationError("Cannot connect to Infura with scheme %r" % scheme)

0 comments on commit 5856aac

Please sign in to comment.