Skip to content

Commit

Permalink
chore: reconnect refactored to disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviksaikat committed Aug 18, 2024
1 parent 6b0455f commit f02d539
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 8 deletions.
9 changes: 2 additions & 7 deletions ape_infura/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def load_api_keys(self):

def __get_random_api_key(self) -> str:
"""
Get a random api key a private method. As self.api_keys are unhashable so have to typecast into list
to make it hashable
Get a random api key a private method.
"""
return random.choice(list(self.api_keys))

Expand Down Expand Up @@ -104,16 +103,12 @@ def connect(self):
self._web3.eth.set_gas_price_strategy(rpc_gas_price_strategy)

def disconnect(self):
self._web3 = None

def reconnect(self):
"""
Disconnect the connected API.
Refresh the API keys from environment variable.
Make the self.network_uris empty otherwise the old network_uri will be returned.
Connect again.
"""
self.disconnect()
self._web3 = None
self.load_api_keys()
self.network_uris = {}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_uri_with_random_api_key(provider, mocker):
provider.load_api_keys()
uris = set()
for _ in range(100): # Generate multiple URIs
provider.reconnect() # connect to a new URI
provider.disconnect() # connect to a new URI
uri = provider.uri
uris.add(uri)
assert uri.startswith("https")
Expand Down

0 comments on commit f02d539

Please sign in to comment.