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
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,24 @@ coverage.xml
*~
*~
.*.swp

# Test coverage
htmlcov/
.coverage
.coverage.*
coverage.xml
coverage.svg
.pytest_cache/

# Test artifacts
.tox/
.cache
nosetests.xml
*.cover
.hypothesis/

# Virtual environments
.venv
venv/
ENV/
env/
4 changes: 2 additions & 2 deletions crypto_com_developer_platform_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from .block import Block
from .client import Client
from .contract import Contract
from .event import Event
from .network import Network
from .cronosid import CronosId
from .defi import Defi
from .event import Event
from .exchange import Exchange
from .interfaces.chain_interfaces import CronosEvm, CronosZkEvm
from .interfaces.defi_interfaces import DefiProtocol
from .network import Network
from .token import Token
from .transaction import Transaction
from .wallet import Wallet
Expand Down
8 changes: 4 additions & 4 deletions crypto_com_developer_platform_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def init(cls, api_key: str, provider: str = "") -> None:

from .block import Block
from .contract import Contract
from .network import Network
from .event import Event
from .cronosid import CronosId
from .defi import Defi
from .event import Event
from .exchange import Exchange
from .network import Network
from .token import Token
from .transaction import Transaction
from .wallet import Wallet
Expand All @@ -48,7 +48,7 @@ def get_api_key(cls) -> str:
:return: The API key.
:raises ValueError: If the API key is not set.
"""
if cls._api_key is None:
if not hasattr(cls, "_api_key") or cls._api_key is None:
raise ValueError("API key is not set. Please set the API key.")

return cls._api_key
Expand All @@ -60,7 +60,7 @@ def get_provider(cls) -> str:

:return: The provider.
"""
if cls._provider is None:
if not hasattr(cls, "_provider") or cls._provider is None:
raise ValueError("Provider is not set. Please set the provider.")

return cls._provider
10 changes: 8 additions & 2 deletions crypto_com_developer_platform_client/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def get_all_tickers(cls) -> ApiResponse:

:return: A list of all available tickers and their information.
"""
return get_all_tickers()
if cls._client is None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed if if we check the condition directly in the client file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before calling get_api_instance(), it check validity of _client

Copy link
Collaborator Author

@leejw51crypto leejw51crypto Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_all_tickers need apikey, so this change ensures, apikey is given correctly

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right this is needed after our latest change and is consistent with the other methods.

raise ValueError("Exchange class not initialized with a Client instance")

return get_all_tickers(cls._client.get_api_key())

@classmethod
def get_ticker_by_instrument(cls, instrument_name: str) -> ApiResponse:
Expand All @@ -37,7 +40,10 @@ def get_ticker_by_instrument(cls, instrument_name: str) -> ApiResponse:
:return: Ticker information for the specified instrument.
:raises ValueError: If instrument_name is None or empty.
"""
if cls._client is None:
raise ValueError("Exchange class not initialized with a Client instance")

if not instrument_name:
raise ValueError("Instrument name is required")

return get_ticker_by_instrument(instrument_name)
return get_ticker_by_instrument(cls._client.get_api_key(), instrument_name)
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


class Status(str, Enum):
SUCCESS = 'Success'
FAILED = 'Failed'
SUCCESS = "Success"
FAILED = "Failed"


class ApiResponse(TypedDict):
Expand Down
20 changes: 20 additions & 0 deletions crypto_com_developer_platform_client/integrations/api_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import requests


def handle_api_error(response: requests.Response) -> None:
"""
Handles API error responses by extracting error messages and raising exceptions.

:param response: The HTTP response object from requests
:raises Exception: Always raises an exception with the appropriate error message
"""
try:
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
except ValueError:
# Handle non-JSON error responses
server_error_message = f"HTTP error! status: {response.status_code}"

raise Exception(server_error_message)
13 changes: 3 additions & 10 deletions crypto_com_developer_platform_client/integrations/block_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ..constants import API_URL
from .api_interfaces import ApiResponse
from .api_utils import handle_api_error


def get_current_block(api_key: str) -> ApiResponse:
Expand All @@ -25,11 +26,7 @@ def get_current_block(api_key: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()

Expand All @@ -55,10 +52,6 @@ def get_block_by_tag(api_key: str, block_tag: str, tx_detail: str) -> ApiRespons
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"""HTTP error! status: {response.status_code}"""
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import requests

from ..constants import API_URL
from .api_interfaces import ApiResponse
from .api_utils import handle_api_error


def get_contract_code(api_key: str, contract_address: str) -> ApiResponse:
Expand All @@ -25,11 +27,7 @@ def get_contract_code(api_key: str, contract_address: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()

Expand Down Expand Up @@ -60,10 +58,6 @@ def get_contract_abi(
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"""HTTP error! status: {response.status_code}"""
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import requests

from ..constants import API_URL
from .api_interfaces import ApiResponse
from .api_utils import handle_api_error


def resolve_cronos_id(api_key: str, name: str) -> ApiResponse:
Expand All @@ -22,11 +24,7 @@ def resolve_cronos_id(api_key: str, name: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()

Expand All @@ -50,10 +48,6 @@ def lookup_cronos_id(api_key: str, address: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()
19 changes: 4 additions & 15 deletions crypto_com_developer_platform_client/integrations/defi_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ..constants import API_URL
from .api_interfaces import ApiResponse
from .api_utils import handle_api_error


def get_whitelisted_tokens(project: str, api_key: str) -> ApiResponse:
Expand All @@ -22,11 +23,7 @@ def get_whitelisted_tokens(project: str, api_key: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()

Expand All @@ -49,11 +46,7 @@ def get_all_farms(project: str, api_key: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()

Expand All @@ -77,10 +70,6 @@ def get_farm_by_symbol(project: str, symbol: str, api_key: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import requests

from ..constants import API_URL
from .api_interfaces import ApiResponse
from .api_utils import handle_api_error


def get_logs(api_key: str, address: str) -> ApiResponse:
Expand All @@ -22,10 +24,6 @@ def get_logs(api_key: str, address: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from ..constants import API_URL
from .api_interfaces import ApiResponse
from .api_utils import handle_api_error


def get_all_tickers(api_key: str) -> ApiResponse:
Expand All @@ -21,11 +22,7 @@ def get_all_tickers(api_key: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()

Expand All @@ -48,10 +45,6 @@ def get_ticker_by_instrument(api_key: str, instrument_name: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import requests

from ..constants import API_URL
from .api_interfaces import ApiResponse
from .api_utils import handle_api_error


def get_network_info(api_key: str) -> ApiResponse:
Expand All @@ -21,11 +23,7 @@ def get_network_info(api_key: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()

Expand All @@ -48,11 +46,7 @@ def get_chain_id(api_key: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()

Expand All @@ -74,10 +68,6 @@ def get_client_version(api_key: str) -> ApiResponse:
)

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get("error") or f"HTTP error! status: {response.status_code}"
)
raise Exception(server_error_message)
handle_api_error(response)

return response.json()
Loading