diff --git a/examples/rest_api/market/mark_price.py b/examples/rest_api/market/mark_price.py index 7ef3265..bc75ecc 100644 --- a/examples/rest_api/market/mark_price.py +++ b/examples/rest_api/market/mark_price.py @@ -1,8 +1,59 @@ +# spot_price_fetcher.py + import logging -from aster.rest_api import Client +import sys +from aster.rest_api import Client as AsterClient # Renaming Client to avoid confusion from aster.lib.utils import config_logging +from aster.lib.errors import APIError, ConnectionError + +# Define constants for clarity and easy modification +SYMBOL = "BTCUSDT" +LOG_LEVEL = logging.DEBUG + + +def fetch_and_log_mark_price(client: AsterClient, symbol: str) -> None: + """ + Fetches the mark price for a given trading symbol using the Aster client + and logs the result or any encountered errors. + + Args: + client: An initialized instance of the Aster REST API Client. + symbol: The trading pair symbol (e.g., "BTCUSDT"). + """ + try: + # Attempt to retrieve the mark price for the specified symbol. + mark_price_data = client.mark_price(symbol) + logging.info("Successfully retrieved mark price for %s: %s", symbol, mark_price_data) + + except (APIError, ConnectionError) as e: + # Catch specific exceptions related to API interaction or network issues. + logging.error("Failed to fetch mark price for %s. Error: %s", symbol, e) + except Exception as e: + # Catch any other unexpected exceptions. + logging.critical("An unexpected error occurred during price fetching: %s", e) + + +if __name__ == "__main__": + # 1. Configure logging system. + # We configure logging inside the main execution block. + try: + config_logging(logging, LOG_LEVEL) + logging.debug("Logging initialized at %s level.", logging.getLevelName(LOG_LEVEL)) + except Exception as e: + print(f"FATAL: Could not configure logging. Error: {e}", file=sys.stderr) + sys.exit(1) + + + # 2. Initialize the client. + # Note: Ensure that the API keys (if required) are configured correctly + # via environment variables or a configuration file that 'Client()' expects. + try: + client = AsterClient() + logging.info("Aster REST API Client initialized.") + except Exception as e: + logging.critical("Failed to initialize Aster client. Check configuration. Error: %s", e) + sys.exit(1) -config_logging(logging, logging.DEBUG) -client = Client() -logging.info(client.mark_price("BTCUSDT")) \ No newline at end of file + # 3. Execute the core logic. + fetch_and_log_mark_price(client, SYMBOL)