Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions blockapi/test/v2/api/debank/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ def protocol_parser():

@pytest.fixture
def debank_api(protocol_cache):
return DebankApi(True, protocol_cache)
return DebankApi('dummy-key', True, protocol_cache)


@pytest.fixture
def debank_api_all_off(protocol_cache):
return DebankApi(False, protocol_cache)
return DebankApi('dummy-key', False, protocol_cache)


@pytest.fixture
Expand Down
82 changes: 65 additions & 17 deletions blockapi/test/v2/api/debank/test_debank.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ def test_build_balance_request_url(debank_api):
)
assert (
url
== 'https://openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=True'
== 'https://pro-openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=True'
)


def test_build_balance_request_url_with_custom_url():
api = DebankApi('dummy-key', False, base_url='https://proxy/')

url = api._build_request_url(
'get_balance',
address='0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca',
is_all=True,
)
assert (
url
== 'https://proxy/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=True'
)


Expand All @@ -21,13 +35,13 @@ def test_build_balance_request_url_with_is_all_off(debank_api_all_off):
)
assert (
url
== 'https://openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=False'
== 'https://pro-openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=False'
)


def test_build_protocols_request_url(debank_api):
url = debank_api._build_request_url('get_protocols')
assert url == 'https://openapi.debank.com/v1/protocol/list'
assert url == 'https://pro-openapi.debank.com/v1/protocol/list'


def test_build_portfolio_request_url(debank_api):
Expand All @@ -36,7 +50,7 @@ def test_build_portfolio_request_url(debank_api):
)
assert (
url
== 'https://openapi.debank.com/v1/user/complex_protocol_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca'
== 'https://pro-openapi.debank.com/v1/user/complex_protocol_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca'
)


Expand All @@ -45,7 +59,7 @@ def test_error_response_returns_empty_balances(
):
protocol_cache.update({})
requests_mock.get(
"https://openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=true",
"https://pro-openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=true",
text=error_response_raw,
)
parsed_items = debank_api.get_balance("0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca")
Expand All @@ -63,13 +77,42 @@ def test_error_response_logs_error(
]

requests_mock.get(
"https://openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=true",
"https://pro-openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=true",
text=error_response_raw,
)
_ = debank_api.get_balance("0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca")
assert expected_log == caplog.messages


def test_get_balance_has_api_key_header(debank_api, protocol_cache, requests_mock):
req = requests_mock.get(
'https://pro-openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=True',
text='{}',
)
protocol_cache.update({})
debank_api.get_balance("0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca")
assert req.last_request.headers.get('AccessKey') == 'dummy-key'


def test_get_portfolio_has_api_key_header(debank_api, protocol_cache, requests_mock):
req = requests_mock.get(
'https://pro-openapi.debank.com/v1/user/complex_protocol_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca',
text='{}',
)
protocol_cache.update({})
debank_api.get_portfolio("0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca")
assert req.last_request.headers.get('AccessKey') == 'dummy-key'


def test_get_protocol(debank_api, requests_mock):
req = requests_mock.get(
'https://pro-openapi.debank.com/v1/protocol/list',
text='{}',
)
debank_api.get_protocols()
assert req.last_request.headers.get('AccessKey') == 'dummy-key'


def test_repr_doesnt_fail(debank_api):
assert repr(debank_api) == "DebankApi"

Expand All @@ -78,7 +121,8 @@ def test_debank_parse_protocols(
debank_api, yflink_protocol_response_raw, yflink_cache_data, requests_mock
):
requests_mock.get(
"https://openapi.debank.com/v1/protocol/list", text=yflink_protocol_response_raw
"https://pro-openapi.debank.com/v1/protocol/list",
text=yflink_protocol_response_raw,
)
parsed_items = debank_api.get_protocols()
assert parsed_items == yflink_cache_data
Expand All @@ -91,10 +135,11 @@ def test_get_balance_fetches_protocols(
requests_mock,
):
requests_mock.get(
"https://openapi.debank.com/v1/protocol/list", text=yflink_protocol_response_raw
"https://pro-openapi.debank.com/v1/protocol/list",
text=yflink_protocol_response_raw,
)
requests_mock.get(
"https://openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=true",
"https://pro-openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=true",
text=coin_with_protocol_response_raw,
)
debank_api._protocol_cache.invalidate()
Expand All @@ -109,10 +154,11 @@ def test_get_balance_uses_correct_url(
requests_mock,
):
requests_mock.get(
"https://openapi.debank.com/v1/protocol/list", text=yflink_protocol_response_raw
"https://pro-openapi.debank.com/v1/protocol/list",
text=yflink_protocol_response_raw,
)
requests_mock.get(
"https://openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=true",
"https://pro-openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=true",
text=coin_with_protocol_response_raw,
)
parsed_items = debank_api.get_balance("0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca")
Expand All @@ -126,10 +172,11 @@ def test_get_balance_with_all_unset_uses_correct_url(
requests_mock,
):
requests_mock.get(
"https://openapi.debank.com/v1/protocol/list", text=yflink_protocol_response_raw
"https://pro-openapi.debank.com/v1/protocol/list",
text=yflink_protocol_response_raw,
)
requests_mock.get(
"https://openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=false",
"https://pro-openapi.debank.com/v1/user/token_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca&is_all=false",
text=coin_with_protocol_response_raw,
)
parsed_items = debank_api_all_off.get_balance(
Expand All @@ -142,10 +189,11 @@ def test_get_portfolio_fetches_protocols(
debank_api, yflink_protocol_response_raw, portfolio_response_raw, requests_mock
):
requests_mock.get(
"https://openapi.debank.com/v1/protocol/list", text=yflink_protocol_response_raw
"https://pro-openapi.debank.com/v1/protocol/list",
text=yflink_protocol_response_raw,
)
requests_mock.get(
"https://openapi.debank.com/v1/user/complex_protocol_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca",
"https://pro-openapi.debank.com/v1/user/complex_protocol_list?id=0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca",
text=portfolio_response_raw,
)
debank_api._protocol_cache.invalidate()
Expand All @@ -156,8 +204,8 @@ def test_get_portfolio_fetches_protocols(


def test_protocol_cache_is_shared_by_instances():
one = DebankApi(True)
two = DebankApi(True)
one = DebankApi('dummy-key', True)
two = DebankApi('dummy-key', True)

assert one._protocol_cache is two._protocol_cache

Expand Down
20 changes: 14 additions & 6 deletions blockapi/v2/api/debank.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@ def _get_reward_asset_type(asset_type):

class DebankApi(BlockchainApi, IBalance, IPortfolio):
"""
DeBank OpenApi: https://openapi.debank.com/docs
DeBank OpenApi: https://open.debank.com/
"""

API_BASE_URL = 'https://openapi.debank.com'
API_BASE_URL = 'https://pro-openapi.debank.com'
API_BASE_RATE_LIMIT = 0.05 # 20 req / s

api_options = ApiOptions(
Expand All @@ -419,11 +419,17 @@ class DebankApi(BlockchainApi, IBalance, IPortfolio):
default_protocol_cache = DebankProtocolCache()

def __init__(
self, is_all: bool, protocol_cache: Optional[DebankProtocolCache] = None
self,
api_key: str,
is_all: bool,
protocol_cache: Optional[DebankProtocolCache] = None,
base_url: Optional[str] = None,
):
super().__init__()

self.api_options.base_url = base_url or self.API_BASE_URL
self._is_all = bool(is_all)
self._headers = {'AccessKey': api_key}
self._protocol_cache = protocol_cache or self.default_protocol_cache
self._balance_parser = DebankBalanceParser(self._protocol_cache)
self._protocol_parser = DebankProtocolParser()
Expand All @@ -433,22 +439,24 @@ def __init__(

def get_balance(self, address: str) -> List[BalanceItem]:
self._maybe_update_protocols()
response = self.get('get_balance', address=address, is_all=self._is_all)
response = self.get(
'get_balance', headers=self._headers, address=address, is_all=self._is_all
)
if self._has_error(response):
return []

return self._balance_parser.parse(response)

def get_protocols(self) -> Dict[str, Protocol]:
response = self.get('get_protocols')
response = self.get('get_protocols', headers=self._headers)
if self._has_error(response):
return {}

return self._protocol_parser.parse(response)

def get_portfolio(self, address: str) -> List[Pool]:
self._maybe_update_protocols()
response = self.get('get_portfolio', address=address)
response = self.get('get_portfolio', headers=self._headers, address=address)
if self._has_error(response):
return []

Expand Down