import sys import os import asyncio from typing import Any, Dict, List, Optional # Add backend to path to import TushareProvider sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "backend")) from app.data_providers.tushare import TushareProvider class TushareLegacyClient: """ An adapter to mimic the old TushareClient for legacy scripts, but uses the new TushareProvider under the hood. """ def __init__(self, token: str): if not token: raise ValueError("Token must be provided.") self.provider = TushareProvider(token=token) async def query( self, api_name: str, params: Optional[Dict[str, Any]] = None, fields: Optional[str] = None, # Note: fields are not used in the new provider's _query ) -> List[Dict[str, Any]]: """ Mimics the .query() method by calling the provider's internal _query method. """ # The new _query method is protected, but we call it here for the script's sake. return await self.provider._query(api_name=api_name, params=params, fields=fields) async def aclose(self): """Mimic aclose to allow 'async with' syntax.""" if hasattr(self.provider, '_client') and self.provider._client: await self.provider._client.aclose() async def __aenter__(self): return self async def __aexit__(self, exc_type, exc, tb): await self.aclose()