Skip to content

Commit

Permalink
feat: get_new_uri method added to get new URI with backwards compatib…
Browse files Browse the repository at this point in the history
…ility
  • Loading branch information
Aviksaikat committed Aug 7, 2024
1 parent 1dbfde2 commit be08aca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ape_infura/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ def uri(self) -> str:
self.network_uris[(ecosystem_name, network_name)] = network_uri
return network_uri

def get_new_uri(self) -> str:
"""
To generate a new URI with a new API key. Added to keep backwards compatibity
"""
key = self.get_random_api_key()
ecosystem_name = self.network.ecosystem.name
network_name = self.network.name

prefix = f"{ecosystem_name}-" if ecosystem_name != "ethereum" else ""
network_uri = f"https://{prefix}{network_name}.infura.io/v3/{key}"
self.network_uris[(ecosystem_name, network_name)] = network_uri
return network_uri

@property
def http_uri(self) -> str:
# NOTE: Overriding `Web3Provider.http_uri` implementation
Expand Down
12 changes: 12 additions & 0 deletions tests/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def test_random_api_key_selection(provider, mocker):
assert len(selected_keys) > 1 # Ensure we're getting different keys


def test_uri_with_random_api_key(provider, mocker):
# mocker.patch.dict(os.environ, {"WEB3_INFURA_PROJECT_ID": "key1, key2, key3, key4, key5, key6"})
provider.load_api_keys()
uris = set()
for _ in range(100): # Generate multiple URIs
uri = provider.get_new_uri() # Use get_new_uri method to get a URI
uris.add(uri)
assert uri.startswith("https")
assert "/v3" in uri
assert len(uris) > 1 # Ensure we're getting different URIs with different


def test_missing_project_key_error_raised(provider, mocker):
mocker.patch.dict(os.environ, {}, clear=True)
with pytest.raises(MissingProjectKeyError):
Expand Down

0 comments on commit be08aca

Please sign in to comment.