Skip to content

Commit

Permalink
Merge pull request #2 from Ankr-network/advanced-api-goes-freemium
Browse files Browse the repository at this point in the history
Make api_key required param
  • Loading branch information
romanankr authored May 27, 2023
2 parents d5e2fb1 + 0d59aad commit 16c42b7
Show file tree
Hide file tree
Showing 11 changed files with 528 additions and 470 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
isort .
test:
needs: linting
env:
ANKR_API_KEY: ${{ secrets.ANKR_API_KEY }}
strategy:
fail-fast: true
matrix:
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ pip install ankr-sdk

#### 2. Initialize the SDK

_Note: to use Advanced API for free starting from May 29th, 2023 you have to register on the platform._

Get your individual endpoint here https://www.ankr.com/rpc/advanced-api and provide it to the `AnkrWeb3` class.

```python3
from ankr import AnkrWeb3

ankr_w3 = AnkrWeb3()

# Or, if you have an Ankr Protocol premium plan
ankr_w3 = AnkrWeb3("YOUR-TOKEN")
```

Expand Down Expand Up @@ -218,7 +219,6 @@ tx = ankr_w3.query.get_transaction(

### About API keys

For now, Ankr is offering _free_ access to these APIs with no request limits i.e. you don't need an API key at this
time.
Ankr is offering _free_ access to Advanced API, however you have to register on Ankr platform to access it.

Later on, these APIs will become a part of Ankr Protocol's [Premium Plan](https://www.ankr.com/protocol/plan/).
Get your individual API Key here https://www.ankr.com/rpc/advanced-api.
4 changes: 2 additions & 2 deletions ankr/advanced_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
class AnkrMultichainAPI:
def __init__(
self,
api_key: Optional[str] = None,
api_key: str,
endpoint_uri: Optional[str] = None,
) -> None:
self.provider = MultichainHTTPProvider(api_key or "", endpoint_uri)
self.provider = MultichainHTTPProvider(api_key, endpoint_uri)


class AnkrQueryAPI(AnkrMultichainAPI):
Expand Down
5 changes: 2 additions & 3 deletions ankr/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
class MultichainHTTPProvider(HTTPProvider):
def __init__(
self,
api_key: str = "",
api_key: str,
endpoint_uri: Optional[Union[URI, str]] = None,
request_kwargs: Optional[Any] = None,
session: Optional[Any] = None,
) -> None:
if endpoint_uri is None:
endpoint_uri = "https://rpc.ankr.com/multichain/"
endpoint_uri = endpoint_uri or "https://rpc.ankr.com/multichain/"
super().__init__(endpoint_uri + api_key, request_kwargs, session)

def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
Expand Down
2 changes: 1 addition & 1 deletion ankr/web3.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class AnkrWeb3(Web3, metaclass=_Web3NamedMeta):

def __init__(
self,
api_key: Optional[str] = None,
api_key: str,
request_kwargs: Optional[Any] = None,
middlewares: Optional[Sequence[Any]] = None,
modules: Optional[Dict[str, Union[Type[Module], Sequence[Any]]]] = None,
Expand Down
887 changes: 474 additions & 413 deletions poetry.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ankr-sdk"
version = "0.3.0"
version = "0.4.0"
description = "Compact Python library for interacting with Ankr's Advanced APIs."
authors = ["Roman Fasakhov <romanfasakhov@ankr.com>"]
license = "MIT License"
Expand All @@ -16,14 +16,14 @@ packages = [{ include = "ankr" }]

[tool.poetry.dependencies]
python = "^3.8.1"
web3 = "^6.0.0"
pydantic = "^1.10.7"
web3 = "^6.4.0"
pydantic = "^1.10.8"
pyhumps = "^3.8.0"
typing-extensions = "^4.5.0"
typing-extensions = "^4.6.2"

[tool.poetry.dev-dependencies]
pytest = "^7.2.2"
mypy = "^1.1.1"
pytest = "^7.3.1"
mypy = "^1.3.0"
flake8 = "^6.0.0"
isort = "^5.12.0"

Expand Down
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

import pytest

from ankr import AnkrAdvancedAPI


@pytest.fixture
def api_key() -> str:
key = os.environ.get("ANKR_API_KEY")
assert key
return key


@pytest.fixture
def client(api_key: str) -> AnkrAdvancedAPI:
return AnkrAdvancedAPI(api_key)
47 changes: 15 additions & 32 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@


def test_client_api_key() -> None:
client = AnkrAdvancedAPI()
client_with_key = AnkrAdvancedAPI("my-test-api-key")

assert client.provider.endpoint_uri == "https://rpc.ankr.com/multichain/"
assert (
client_with_key.provider.endpoint_uri
AnkrAdvancedAPI("my-test-api-key").provider.endpoint_uri
== "https://rpc.ankr.com/multichain/my-test-api-key"
)


@pytest.mark.webtest
def test_get_logs() -> None:
client = AnkrAdvancedAPI()
def test_get_logs(client: AnkrAdvancedAPI) -> None:
logs = list(
client.get_logs(
blockchain=Blockchain.ETH,
Expand All @@ -39,12 +34,11 @@ def test_get_logs() -> None:
assert len(logs) == 18
assert logs[0].address == "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
assert logs[0].event
assert logs[0].event.name == "Deposit"
assert logs[0].event.name == "Transfer"


@pytest.mark.webtest
def test_get_blocks() -> None:
client = AnkrAdvancedAPI()
def test_get_blocks(client: AnkrAdvancedAPI) -> None:
blocks = client.get_blocks(
blockchain=Blockchain.ETH,
from_block=14500001,
Expand All @@ -63,8 +57,7 @@ def test_get_blocks() -> None:


@pytest.mark.webtest
def test_get_nfts() -> None:
client = AnkrAdvancedAPI()
def test_get_nfts(client: AnkrAdvancedAPI) -> None:
nfts = list(
client.get_nfts(
blockchain=Blockchain.ETH,
Expand All @@ -83,8 +76,7 @@ def test_get_nfts() -> None:


@pytest.mark.webtest
def test_get_nft_metadata() -> None:
client = AnkrAdvancedAPI()
def test_get_nft_metadata(client: AnkrAdvancedAPI) -> None:
reply = client.get_nft_metadata(
blockchain="eth",
contract_address="0x4100670ee2f8aef6c47a4ed13c7f246e621228ec",
Expand All @@ -99,8 +91,7 @@ def test_get_nft_metadata() -> None:


@pytest.mark.webtest
def test_get_nft_holders() -> None:
client = AnkrAdvancedAPI()
def test_get_nft_holders(client: AnkrAdvancedAPI) -> None:
holders = list(
client.get_nft_holders(
blockchain="eth",
Expand All @@ -114,8 +105,7 @@ def test_get_nft_holders() -> None:


@pytest.mark.webtest
def test_get_transactions() -> None:
client = AnkrAdvancedAPI()
def test_get_transactions(client: AnkrAdvancedAPI) -> None:
tx = client.get_transaction(
transaction_hash="0x82c13aaac6f0b6471afb94a3a64ae89d45baa3608ad397621dbb0d847f51196f",
include_logs=True,
Expand All @@ -135,8 +125,7 @@ def test_get_transactions() -> None:


@pytest.mark.webtest
def test_get_token_holders() -> None:
client = AnkrAdvancedAPI()
def test_get_token_holders(client: AnkrAdvancedAPI) -> None:
holders = list(
client.get_token_holders(
blockchain="bsc",
Expand All @@ -152,8 +141,7 @@ def test_get_token_holders() -> None:


@pytest.mark.webtest
def test_get_token_holders_pagination() -> None:
client = AnkrAdvancedAPI()
def test_get_token_holders_pagination(client: AnkrAdvancedAPI) -> None:
holders = list(
client.get_token_holders(
blockchain="bsc",
Expand All @@ -169,8 +157,7 @@ def test_get_token_holders_pagination() -> None:


@pytest.mark.webtest
def test_get_token_holders_count_history() -> None:
client = AnkrAdvancedAPI()
def test_get_token_holders_count_history(client: AnkrAdvancedAPI) -> None:
daily_holders_counts = list(
client.get_token_holders_count_history(
blockchain="bsc",
Expand All @@ -187,8 +174,7 @@ def test_get_token_holders_count_history() -> None:


@pytest.mark.webtest
def test_get_token_holders_count() -> None:
client = AnkrAdvancedAPI()
def test_get_token_holders_count(client: AnkrAdvancedAPI) -> None:
holders_count = client.get_token_holders_count(
blockchain="bsc",
contract_address="0xf307910A4c7bbc79691fD374889b36d8531B08e3",
Expand All @@ -200,8 +186,7 @@ def test_get_token_holders_count() -> None:


@pytest.mark.webtest
def test_get_account_balance() -> None:
client = AnkrAdvancedAPI()
def test_get_account_balance(client: AnkrAdvancedAPI) -> None:
assets = list(
client.get_account_balance(
wallet_address="0x77A859A53D4de24bBC0CC80dD93Fbe391Df45527",
Expand All @@ -214,8 +199,7 @@ def test_get_account_balance() -> None:


@pytest.mark.webtest
def test_get_token_price() -> None:
client = AnkrAdvancedAPI()
def test_get_token_price(client: AnkrAdvancedAPI) -> None:
price = client.get_token_price(
contract_address="0x8290333cef9e6d528dd5618fb97a76f268f3edd4",
blockchain="eth",
Expand All @@ -226,8 +210,7 @@ def test_get_token_price() -> None:


@pytest.mark.webtest
def test_get_token_price__no_price() -> None:
client = AnkrAdvancedAPI()
def test_get_token_price__no_price(client: AnkrAdvancedAPI) -> None:
price = client.get_token_price(
contract_address="0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
blockchain="eth",
Expand Down
6 changes: 1 addition & 5 deletions tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@


def test_provider_api_key() -> None:
provider = MultichainHTTPProvider()
provider_with_key = MultichainHTTPProvider("my-test-api-key")

assert provider.endpoint_uri == "https://rpc.ankr.com/multichain/"
assert (
provider_with_key.endpoint_uri
MultichainHTTPProvider("my-test-api-key").endpoint_uri
== "https://rpc.ankr.com/multichain/my-test-api-key"
)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_web3.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"syscoin",
],
)
def test_ankr_web3(blockchain: str) -> None:
w3 = AnkrWeb3()
def test_ankr_web3(blockchain: str, api_key: str) -> None:
w3 = AnkrWeb3(api_key)

block = getattr(w3, blockchain).get_block("latest")

Expand Down

0 comments on commit 16c42b7

Please sign in to comment.