diff --git a/crypto_com_developer_platform_client/__init__.py b/crypto_com_developer_platform_client/__init__.py index 79fcd5c..2cbdb11 100644 --- a/crypto_com_developer_platform_client/__init__.py +++ b/crypto_com_developer_platform_client/__init__.py @@ -1,6 +1,7 @@ from .block import Block from .client import Client from .contract import Contract +from .exchange import Exchange from .interfaces.chain_interfaces import CronosEvm, CronosZkEvm from .token import Token from .transaction import Transaction @@ -15,4 +16,5 @@ "Token", "CronosEvm", "CronosZkEvm", + "Exchange", ] diff --git a/crypto_com_developer_platform_client/client.py b/crypto_com_developer_platform_client/client.py index c9f09fa..a6af647 100644 --- a/crypto_com_developer_platform_client/client.py +++ b/crypto_com_developer_platform_client/client.py @@ -27,12 +27,14 @@ def init(cls, api_key: str, chain_id: str, provider: str = "") -> None: from .token import Token from .transaction import Transaction from .wallet import Wallet - + from .exchange import Exchange + Contract.init(cls()) Wallet.init(cls()) Block.init(cls()) Transaction.init(cls()) Token.init(cls()) + Exchange.init(cls()) @classmethod def get_api_key(cls) -> str: diff --git a/crypto_com_developer_platform_client/exchange.py b/crypto_com_developer_platform_client/exchange.py new file mode 100644 index 0000000..1fb878e --- /dev/null +++ b/crypto_com_developer_platform_client/exchange.py @@ -0,0 +1,43 @@ +from .client import Client +from .integrations.api_interfaces import ApiResponse +from .integrations.exchange_api import get_all_tickers, get_ticker_by_instrument + + +class Exchange: + """ + Exchange class for managing Crypto.com Exchange-related operations like retrieving ticker information (Chain agnostic). + """ + + _client: Client + + @classmethod + def init(cls, client: Client) -> None: + """ + Initialize the Exchange class with a Client instance. + + :param client: An instance of the Client class. + """ + cls._client = client + + @classmethod + def get_all_tickers(cls) -> ApiResponse: + """ + Get all tickers from the Crypto.com Exchange (Chain agnostic). + + :return: A list of all available tickers and their information. + """ + return get_all_tickers() + + @classmethod + def get_ticker_by_instrument(cls, instrument_name: str) -> ApiResponse: + """ + Get ticker information for a specific instrument from the Crypto.com Exchange (Chain agnostic). + + :param instrument_name: The name of the instrument to get ticker information for. + :return: Ticker information for the specified instrument. + :raises ValueError: If instrument_name is None or empty. + """ + if not instrument_name: + raise ValueError("Instrument name is required") + + return get_ticker_by_instrument(instrument_name) diff --git a/crypto_com_developer_platform_client/integrations/exchange_api.py b/crypto_com_developer_platform_client/integrations/exchange_api.py new file mode 100644 index 0000000..7fc28a3 --- /dev/null +++ b/crypto_com_developer_platform_client/integrations/exchange_api.py @@ -0,0 +1,50 @@ +import requests + +from .api_interfaces import ApiResponse + + +def get_all_tickers() -> ApiResponse: + """ + Get all tickers from the Crypto.com Exchange (Chain agnostic). + + :return: A list of all available tickers and their information. + :raises Exception: If the ticker retrieval fails or the server responds with an error. + """ + url = f"""https://developer-platform-api.crypto.com/v1/cdc-developer-platform/exchange/tickers""" + + response = requests.get(url, headers={'Content-Type': 'application/json'}) + + 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(f"Failed to fetch all tickers: {server_error_message}") + + return response.json() + + +def get_ticker_by_instrument(instrument_name: str) -> ApiResponse: + """ + Get ticker information for a specific instrument from the Crypto.com Exchange (Chain agnostic). + + :param instrument_name: The name of the instrument to get ticker information for. + :return: Ticker information for the specified instrument. + :raises Exception: If the ticker retrieval fails, does not exist or the server responds with an error. + """ + url = f"""https://developer-platform-api.crypto.com/v1/cdc-developer-platform/exchange/tickers/{ + instrument_name}""" + + response = requests.get(url, headers={'Content-Type': 'application/json'}) + + 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(f"""Failed to fetch ticker for instrument { + instrument_name}: {server_error_message}""") + + return response.json() diff --git a/pyproject.toml b/pyproject.toml index 088dc1f..f221f01 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "crypto-com-developer-platform-client" -version = "1.0.5" +version = "1.0.6" description = "A python client to interact with @cryptocom/developer-platform-service" authors = ["Ric Arcifa "] license = "MIT"