diff --git a/web3/auto/infura.py b/web3/auto/infura.py deleted file mode 100644 index 1e85988d46..0000000000 --- a/web3/auto/infura.py +++ /dev/null @@ -1,24 +0,0 @@ -import logging -import os - -from web3 import ( - HTTPProvider, - Web3, -) - -INFURA_MAINNET_BASE_URL = 'https://mainnet.infura.io' - - -def load_infura_url(): - key = os.environ.get('INFURA_API_KEY', '') - if key == '': - logging.getLogger('web3.auto.infura').warning( - "No Infura API Key found. Add environment variable INFURA_API_KEY to ensure continued " - "API access. New keys are available at https://infura.io/signup" - ) - return INFURA_MAINNET_BASE_URL - else: - return "%s/%s" % (INFURA_MAINNET_BASE_URL, key) - - -w3 = Web3(HTTPProvider(load_infura_url())) diff --git a/web3/auto/infura/__init__.py b/web3/auto/infura/__init__.py new file mode 100644 index 0000000000..aa29978adb --- /dev/null +++ b/web3/auto/infura/__init__.py @@ -0,0 +1,13 @@ +from web3 import Web3 +from web3.providers.auto import ( + load_provider_from_uri, +) + +from .endpoints import ( + INFURA_MAINNET_DOMAIN, + build_infura_url, +) + +_infura_url = build_infura_url(INFURA_MAINNET_DOMAIN) + +w3 = Web3(load_provider_from_uri(_infura_url)) diff --git a/web3/auto/infura/endpoints.py b/web3/auto/infura/endpoints.py new file mode 100644 index 0000000000..7840cfa6d5 --- /dev/null +++ b/web3/auto/infura/endpoints.py @@ -0,0 +1,39 @@ +import logging +import os + +from eth_utils import ( + ValidationError, +) + +INFURA_MAINNET_DOMAIN = 'mainnet.infura.io' +INFURA_ROPSTEN_DOMAIN = 'ropsten.infura.io' + +WEBSOCKET_SCHEME = 'wss' +HTTP_SCHEME = 'https' + + +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', '') + ) + 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" + ) + 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) + 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) diff --git a/web3/auto/infura/ropsten.py b/web3/auto/infura/ropsten.py new file mode 100644 index 0000000000..cde15e757b --- /dev/null +++ b/web3/auto/infura/ropsten.py @@ -0,0 +1,13 @@ +from web3 import Web3 +from web3.providers.auto import ( + load_provider_from_uri, +) + +from .endpoints import ( + INFURA_ROPSTEN_DOMAIN, + build_infura_url, +) + +_infura_url = build_infura_url(INFURA_ROPSTEN_DOMAIN) + +w3 = Web3(load_provider_from_uri(_infura_url)) diff --git a/web3/providers/auto.py b/web3/providers/auto.py index 6bb56a26a5..23079d21bc 100644 --- a/web3/providers/auto.py +++ b/web3/providers/auto.py @@ -21,6 +21,10 @@ def load_provider_from_environment(): if not uri_string: return None + return load_provider_from_uri(uri_string) + + +def load_provider_from_uri(uri_string): uri = urlparse(uri_string) if uri.scheme == 'file': return IPCProvider(uri.path)