Skip to content

Commit

Permalink
feat(ProviderAPI): add uri str fields for easier use [APE-1317] (ApeW…
Browse files Browse the repository at this point in the history
…orX#1623)

* feat: add `.http_url` and `.ws_url`

* test: add tests to show default implementations work
  • Loading branch information
fubuloubu authored Aug 24, 2023
1 parent bc52af9 commit def9103
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ def disconnect(self):
Disconnect from a provider, such as tear-down a process or quit an HTTP session.
"""

@property
def http_uri(self) -> Optional[str]:
"""
Return the raw HTTP/HTTPS URI to connect to this provider, if supported.
"""
return None

@property
def ws_uri(self) -> Optional[str]:
"""
Return the raw WS/WSS URI to connect to this provider, if supported.
"""
return None

@abstractmethod
def update_settings(self, new_settings: dict):
"""
Expand Down Expand Up @@ -726,6 +740,32 @@ def web3(self) -> Web3:

return self._web3

@property
def http_uri(self) -> Optional[str]:
if (
hasattr(self.web3.provider, "endpoint_uri")
and isinstance(self.web3.provider.endpoint_uri, str)
and self.web3.provider.endpoint_uri.startswith("http")
):
return self.web3.provider.endpoint_uri

elif hasattr(self, "uri"):
# NOTE: Some providers define this
return self.uri

return None

@property
def ws_uri(self) -> Optional[str]:
if (
hasattr(self.web3.provider, "endpoint_uri")
and isinstance(self.web3.provider.endpoint_uri, str)
and self.web3.provider.endpoint_uri.startswith("ws")
):
return self.web3.provider.endpoint_uri

return None

@property
def client_version(self) -> str:
if not self._web3:
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/test_geth.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def geth_vyper_receipt(geth_vyper_contract, owner):

@geth_process_test
def test_uri(geth_provider):
assert geth_provider.http_uri == GETH_URI
assert not geth_provider.ws_uri
assert geth_provider.uri == GETH_URI


Expand Down
5 changes: 5 additions & 0 deletions tests/functional/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
from ape.utils import DEFAULT_TEST_CHAIN_ID


def test_uri(eth_tester_provider):
assert not eth_tester_provider.http_uri
assert not eth_tester_provider.ws_uri


@pytest.mark.parametrize("block_id", (0, "0", "0x0", HexStr("0x0")))
def test_get_block(eth_tester_provider, block_id, vyper_contract_instance, owner):
block = eth_tester_provider.get_block(block_id)
Expand Down

0 comments on commit def9103

Please sign in to comment.