Authentication: OAuth 2.0 Status: Production Ready Data Types: Sleep, Recovery, Cycles, Workouts, Body MeasurementsWhoop 提供面向训练恢复与负荷(strain)的指标,深受运动员与健身爱好者欢迎。
Category
Metrics
Sleep
Total in bed, awake time, light/slow-wave/REM sleep, sleep efficiency
async def callback(self, code: str, state: str) -> Dict[str, Any]: # Exchange code for tokens # Save credentials to database # Trigger initial data pull return {"provider_slug": self.info.slug, "stage": "completed"}
5
数据同步
周期性或按需触发时,provider 会从 vendor API 拉取数据,保存 raw 数据,进行格式化,并推送到 platform。
复制
async def _pull_and_push_for_user(self, credentials: Dict) -> bool: # Pull from vendor API # Save raw data # Format to standard structure # Push to platform pass
class ThetaYourProvider(BaseThetaProvider): @classmethod def create_provider(cls, config: Dict) -> Optional['ThetaYourProvider']: """Factory method for provider instantiation""" @property def info(self) -> ProviderInfo: """Provider metadata""" async def link(self, request: Any) -> Dict[str, Any]: """Initiate OAuth flow""" async def callback(self, *args, **kwargs) -> Dict[str, Any]: """Handle OAuth callback""" async def unlink(self, user_id: str) -> Dict[str, Any]: """Unlink user connection""" async def format_data(self, raw_data: Dict) -> StandardPulseData: """Format raw data to standard format""" async def pull_from_vendor_api(self, *args, **kwargs) -> List[Dict]: """Pull data from vendor API""" async def save_raw_data_to_db(self, raw_data: Dict) -> List[Dict]: """Save raw data to database""" async def is_data_already_processed(self, raw_data: Dict) -> bool: """Check if data already processed""" async def _pull_and_push_for_user(self, credentials: Dict) -> bool: """Pull and push data for user"""
每个 provider 都需要一个数据库表:
复制
CREATE TABLE IF NOT EXISTS theta_ai.health_data_<provider> ( id SERIAL PRIMARY KEY, create_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, update_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, is_del BOOLEAN DEFAULT FALSE, msg_id VARCHAR(255) UNIQUE NOT NULL, raw_data JSONB NOT NULL, theta_user_id VARCHAR(255) NOT NULL, external_user_id VARCHAR(255));CREATE INDEX idx_health_data_<provider>_theta_user_id ON theta_ai.health_data_<provider>(theta_user_id);CREATE INDEX idx_health_data_<provider>_msg_id ON theta_ai.health_data_<provider>(msg_id);