Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove WEB3_INFURA_API_KEY environment variable in favor of WEB3_INFURA_PROJECT_ID #2634

Merged
merged 2 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/2634.breaking-change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove ``WEB3_INFURA_API_KEY`` environment variable in favor of ``WEB3_INFURA_PROJECT_ID``. Change ``InfuraKeyNotFound`` exception to ``InfuraProjectIdNotFound``
71 changes: 23 additions & 48 deletions tests/core/providers/test_auto_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)

from web3.exceptions import (
InfuraKeyNotFound,
InfuraProjectIdNotFound,
)
from web3.providers import (
HTTPProvider,
Expand All @@ -22,7 +22,7 @@
)

# Ugly hack to import infura now that API KEY is required
os.environ["WEB3_INFURA_API_KEY"] = "test"
os.environ["WEB3_INFURA_PROJECT_ID"] = "test"
from web3.auto import ( # noqa E402 isort:skip
infura,
)
Expand All @@ -31,7 +31,6 @@
@pytest.fixture(autouse=True)
def delete_environment_variables(monkeypatch):
monkeypatch.delenv("WEB3_INFURA_PROJECT_ID", raising=False)
monkeypatch.delenv("WEB3_INFURA_API_KEY", raising=False)
monkeypatch.delenv("WEB3_INFURA_API_SECRET", raising=False)
monkeypatch.delenv("WEB3_INFURA_SCHEME", raising=False)

Expand Down Expand Up @@ -69,57 +68,42 @@ def test_get_dev_ipc_path(monkeypatch, tmp_path):
assert path == uri


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

with pytest.raises(InfuraKeyNotFound):
with pytest.raises(InfuraProjectIdNotFound):
importlib.reload(infura)


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

monkeypatch.delenv(environ_name, raising=False)
monkeypatch.delenv("WEB3_INFURA_PROJECT_ID", raising=False)

with pytest.raises(InfuraKeyNotFound):
with pytest.raises(InfuraProjectIdNotFound):
importlib.reload(infura)


@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, "")
def test_web3_auto_infura_websocket_empty_key(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "")

with pytest.raises(InfuraKeyNotFound):
with pytest.raises(InfuraProjectIdNotFound):
importlib.reload(infura)


@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)
def test_web3_auto_infura_websocket_deleted_key(monkeypatch):
monkeypatch.delenv("WEB3_INFURA_PROJECT_ID", raising=False)

with pytest.raises(InfuraKeyNotFound):
with pytest.raises(InfuraProjectIdNotFound):
importlib.reload(infura)


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura(monkeypatch, caplog, environ_name):
def test_web3_auto_infura(monkeypatch, caplog):
monkeypatch.setenv("WEB3_INFURA_SCHEME", "https")
API_KEY = "aoeuhtns"

monkeypatch.setenv(environ_name, API_KEY)
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", API_KEY)

importlib.reload(infura)
assert len(caplog.record_tuples) == 0
Expand All @@ -130,13 +114,10 @@ def test_web3_auto_infura(monkeypatch, caplog, environ_name):
assert getattr(w3.provider, "endpoint_uri") == expected_url


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

importlib.reload(infura)
Expand All @@ -148,19 +129,16 @@ def test_web3_auto_infura_websocket_default(monkeypatch, caplog, environ_name):


def test_web3_auto_infura_raises_error_with_nonexistent_scheme(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_API_KEY", "test")
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "test")
monkeypatch.setenv("WEB3_INFURA_SCHEME", "not-a-scheme")

error_msg = "Cannot connect to Infura with scheme 'not-a-scheme'"
with pytest.raises(ValidationError, match=error_msg):
importlib.reload(infura)


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura_websocket_with_secret(monkeypatch, caplog, environ_name):
monkeypatch.setenv(environ_name, "test")
def test_web3_auto_infura_websocket_with_secret(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "test")
monkeypatch.setenv("WEB3_INFURA_API_SECRET", "secret")

importlib.reload(infura)
Expand All @@ -171,12 +149,9 @@ def test_web3_auto_infura_websocket_with_secret(monkeypatch, caplog, environ_nam
assert getattr(w3.provider, "endpoint_uri") == expected_url


@pytest.mark.parametrize(
"environ_name", ["WEB3_INFURA_API_KEY", "WEB3_INFURA_PROJECT_ID"]
)
def test_web3_auto_infura_with_secret(monkeypatch, caplog, environ_name):
def test_web3_auto_infura_with_secret(monkeypatch):
monkeypatch.setenv("WEB3_INFURA_SCHEME", "https")
monkeypatch.setenv(environ_name, "test")
monkeypatch.setenv("WEB3_INFURA_PROJECT_ID", "test")
monkeypatch.setenv("WEB3_INFURA_API_SECRET", "secret")

importlib.reload(infura)
Expand Down
13 changes: 5 additions & 8 deletions web3/auto/infura/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)

from web3.exceptions import (
InfuraKeyNotFound,
InfuraProjectIdNotFound,
)

INFURA_MAINNET_DOMAIN = "mainnet.infura.io"
Expand All @@ -26,13 +26,10 @@
HTTP_SCHEME = "https"


def load_api_key() -> str:
# in web3py v6 remove outdated WEB3_INFURA_API_KEY
key = os.environ.get(
"WEB3_INFURA_PROJECT_ID", os.environ.get("WEB3_INFURA_API_KEY", "")
)
def load_project_id() -> str:
key = os.environ.get("WEB3_INFURA_PROJECT_ID", "")
if key == "":
raise InfuraKeyNotFound(
raise InfuraProjectIdNotFound(
"No Infura Project ID found. Please ensure "
"that the environment variable WEB3_INFURA_PROJECT_ID is set."
)
Expand All @@ -53,7 +50,7 @@ def build_http_headers() -> Optional[Dict[str, Tuple[str, str]]]:

def build_infura_url(domain: str) -> URI:
scheme = os.environ.get("WEB3_INFURA_SCHEME", WEBSOCKET_SCHEME)
key = load_api_key()
key = load_project_id()
secret = load_secret()

if scheme == WEBSOCKET_SCHEME and secret != "":
Expand Down
2 changes: 1 addition & 1 deletion web3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class BlockNotFound(Exception):
pass


class InfuraKeyNotFound(Exception):
class InfuraProjectIdNotFound(Exception):
"""
Raised when there is no Infura Project Id set.
"""
Expand Down