Skip to content

Commit e83e0ab

Browse files
committed
Add error if no Infura key is found
1 parent 6d19f6f commit e83e0ab

File tree

5 files changed

+57
-52
lines changed

5 files changed

+57
-52
lines changed

conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import pytest
23
import time
34
import warnings

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"eth-hash[pycryptodome]>=0.2.0,<1.0.0",
7474
"eth-typing>=2.0.0,<3.0.0",
7575
"eth-utils>=1.3.0,<2.0.0",
76-
"ethpm>=0.1.4a12,<1.0.0",
76+
"ethpm>=0.1.4a13,<1.0.0",
7777
"hexbytes>=0.1.0,<1.0.0",
7878
"lru-dict>=1.1.6,<2.0.0",
7979
"requests>=2.16.0,<3.0.0",
Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import importlib
2-
import logging
2+
import os
33
import pytest
44

5-
from web3.auto import (
6-
infura,
5+
from eth_utils import (
6+
ValidationError,
7+
)
8+
9+
from web3.exceptions import (
10+
InfuraKeyNotFound,
711
)
812
from web3.providers import (
913
HTTPProvider,
@@ -14,6 +18,18 @@
1418
load_provider_from_environment,
1519
)
1620

21+
# Ugly hack to import infura now that API KEY is required
22+
os.environ['WEB3_INFURA_API_KEY'] = 'test'
23+
from web3.auto import ( # noqa E402 isort:skip
24+
infura,
25+
)
26+
27+
28+
@pytest.fixture(autouse=True)
29+
def delete_infura_key(monkeypatch):
30+
monkeypatch.delenv('INFURA_API_KEY', raising=False)
31+
monkeypatch.delenv('WEB3_INFURA_API_KEY', raising=False)
32+
1733

1834
@pytest.mark.parametrize(
1935
'uri, expected_type, expected_attrs',
@@ -38,75 +54,49 @@ def test_web3_auto_infura_empty_key(monkeypatch, caplog, environ_name):
3854
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https')
3955
monkeypatch.setenv(environ_name, '')
4056

41-
importlib.reload(infura)
42-
assert len(caplog.record_tuples) == 1
43-
logger, level, msg = caplog.record_tuples[0]
44-
assert 'WEB3_INFURA_PROJECT_ID' in msg
45-
assert level == logging.WARNING
46-
47-
w3 = infura.w3
48-
assert isinstance(w3.provider, HTTPProvider)
49-
assert getattr(w3.provider, 'endpoint_uri') == 'https://mainnet.infura.io/'
57+
with pytest.raises(InfuraKeyNotFound):
58+
importlib.reload(infura)
5059

5160

5261
@pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID'])
5362
def test_web3_auto_infura_deleted_key(monkeypatch, caplog, environ_name):
5463
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https')
64+
5565
monkeypatch.delenv(environ_name, raising=False)
5666

57-
importlib.reload(infura)
58-
assert len(caplog.record_tuples) == 1
59-
logger, level, msg = caplog.record_tuples[0]
60-
assert 'WEB3_INFURA_PROJECT_ID' in msg
61-
assert level == logging.WARNING
62-
63-
w3 = infura.w3
64-
assert isinstance(w3.provider, HTTPProvider)
65-
assert getattr(w3.provider, 'endpoint_uri') == 'https://mainnet.infura.io/'
67+
with pytest.raises(InfuraKeyNotFound):
68+
importlib.reload(infura)
6669

6770

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

72-
importlib.reload(infura)
73-
assert len(caplog.record_tuples) == 1
74-
logger, level, msg = caplog.record_tuples[0]
75-
assert 'WEB3_INFURA_PROJECT_ID' in msg
76-
assert level == logging.WARNING
77-
78-
w3 = infura.w3
79-
assert isinstance(w3.provider, WebsocketProvider)
80-
assert getattr(w3.provider, 'endpoint_uri') == 'wss://mainnet.infura.io/ws/'
75+
with pytest.raises(InfuraKeyNotFound):
76+
importlib.reload(infura)
8177

8278

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

87-
importlib.reload(infura)
88-
assert len(caplog.record_tuples) == 1
89-
logger, level, msg = caplog.record_tuples[0]
90-
assert 'WEB3_INFURA_PROJECT_ID' in msg
91-
assert level == logging.WARNING
92-
93-
w3 = infura.w3
94-
assert isinstance(w3.provider, WebsocketProvider)
95-
assert getattr(w3.provider, 'endpoint_uri') == 'wss://mainnet.infura.io/ws/'
83+
with pytest.raises(InfuraKeyNotFound):
84+
importlib.reload(infura)
9685

9786

9887
@pytest.mark.parametrize('environ_name', ['WEB3_INFURA_API_KEY', 'WEB3_INFURA_PROJECT_ID'])
9988
def test_web3_auto_infura(monkeypatch, caplog, environ_name):
10089
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'https')
10190
API_KEY = 'aoeuhtns'
91+
10292
monkeypatch.setenv(environ_name, API_KEY)
103-
expected_url = 'https://%s/v3/%s' % (infura.INFURA_MAINNET_DOMAIN, API_KEY)
10493

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

10897
w3 = infura.w3
10998
assert isinstance(w3.provider, HTTPProvider)
99+
expected_url = 'https://%s/v3/%s' % (infura.INFURA_MAINNET_DOMAIN, API_KEY)
110100
assert getattr(w3.provider, 'endpoint_uri') == expected_url
111101

112102

@@ -123,3 +113,12 @@ def test_web3_auto_infura_websocket_default(monkeypatch, caplog, environ_name):
123113
w3 = infura.w3
124114
assert isinstance(w3.provider, WebsocketProvider)
125115
assert getattr(w3.provider, 'endpoint_uri') == expected_url
116+
117+
118+
def test_web3_auto_infura_raises_error_with_nonexistent_scheme(monkeypatch):
119+
monkeypatch.setenv('WEB3_INFURA_API_KEY', 'test')
120+
monkeypatch.setenv('WEB3_INFURA_SCHEME', 'not-a-scheme')
121+
122+
error_msg = "Cannot connect to Infura with scheme 'not-a-scheme'"
123+
with pytest.raises(ValidationError, match=error_msg):
124+
importlib.reload(infura)

web3/auto/infura/endpoints.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import logging
21
import os
32

43
from eth_utils import (
54
ValidationError,
65
)
76

7+
from web3.exceptions import (
8+
InfuraKeyNotFound,
9+
)
10+
811
INFURA_MAINNET_DOMAIN = 'mainnet.infura.io'
912
INFURA_ROPSTEN_DOMAIN = 'ropsten.infura.io'
1013
INFURA_RINKEBY_DOMAIN = 'rinkeby.infura.io'
@@ -19,10 +22,9 @@ def load_api_key():
1922
key = os.environ.get('WEB3_INFURA_PROJECT_ID',
2023
os.environ.get('WEB3_INFURA_API_KEY', ''))
2124
if key == '':
22-
logging.getLogger('web3.auto.infura').warning(
23-
"No Infura Project ID found. Add environment variable WEB3_INFURA_PROJECT_ID to "
24-
"ensure continued API access after March 27th. "
25-
"New keys are available at https://infura.io/register"
25+
raise InfuraKeyNotFound(
26+
"No Infura Project ID found. Please ensure "
27+
"that the environment variable WEB3_INFURA_PROJECT_ID is set."
2628
)
2729
return key
2830

@@ -31,13 +33,9 @@ def build_infura_url(domain):
3133
scheme = os.environ.get('WEB3_INFURA_SCHEME', WEBSOCKET_SCHEME)
3234
key = load_api_key()
3335

34-
if key and scheme == WEBSOCKET_SCHEME:
36+
if scheme == WEBSOCKET_SCHEME:
3537
return "%s://%s/ws/v3/%s" % (scheme, domain, key)
36-
elif key and scheme == HTTP_SCHEME:
37-
return "%s://%s/v3/%s" % (scheme, domain, key)
38-
elif scheme == WEBSOCKET_SCHEME:
39-
return "%s://%s/ws/" % (scheme, domain)
4038
elif scheme == HTTP_SCHEME:
41-
return "%s://%s/%s" % (scheme, domain, key)
39+
return "%s://%s/v3/%s" % (scheme, domain, key)
4240
else:
4341
raise ValidationError("Cannot connect to Infura with scheme %r" % scheme)

web3/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,10 @@ class BlockNotFound(Exception):
142142
Raised when the block id used to lookup a block in a jsonrpc call cannot be found.
143143
"""
144144
pass
145+
146+
147+
class InfuraKeyNotFound(Exception):
148+
"""
149+
Raised when there is no Infura Project Id set.
150+
"""
151+
pass

0 commit comments

Comments
 (0)