Coverage for mindsdb / integrations / handlers / strava_handler / strava_handler.py: 0%
45 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
1from mindsdb.integrations.handlers.strava_handler.strava_tables import StravaAllClubsTable
2from mindsdb.integrations.handlers.strava_handler.strava_tables import StravaClubActivitesTable
3from mindsdb.integrations.libs.api_handler import APIHandler
4from mindsdb.integrations.libs.response import (
5 HandlerStatusResponse as StatusResponse,
6)
7from mindsdb.utilities import log
8from mindsdb_sql_parser import parse_sql
10from collections import OrderedDict
11from mindsdb.integrations.libs.const import HANDLER_CONNECTION_ARG_TYPE as ARG_TYPE
13from stravalib.client import Client
15logger = log.getLogger(__name__)
18class StravaHandler(APIHandler):
19 """Strava handler implementation"""
21 def __init__(self, name=None, **kwargs):
22 """Initialize the Strava handler.
23 Parameters
24 ----------
25 name : str
26 name of a handler instance
27 """
28 super().__init__(name)
30 connection_data = kwargs.get("connection_data", {})
32 self.parser = parse_sql
33 self.connection_data = connection_data
34 self.kwargs = kwargs
35 self.connection = None
36 self.is_connected = False
38 strava_all_clubs_data = StravaAllClubsTable(self)
39 self._register_table("all_clubs", strava_all_clubs_data)
41 strava_club_activites_data = StravaClubActivitesTable(self)
42 self._register_table("club_activities", strava_club_activites_data)
44 def connect(self) -> StatusResponse:
45 """Set up the connection required by the handler.
46 Returns
47 -------
48 StatusResponse
49 connection object
50 """
51 if self.is_connected is True:
52 return self.connection
54 client = Client()
55 client.access_token = self.connection_data['strava_access_token']
56 self.connection = client
58 return self.connection
60 def check_connection(self) -> StatusResponse:
61 """Check connection to the handler.
62 Returns
63 -------
64 StatusResponse
65 Status confirmation
66 """
67 response = StatusResponse(False)
69 try:
70 self.connect()
71 response.success = True
72 except Exception as e:
73 logger.error(f"Error connecting to Strava API: {e}!")
74 response.error_message = e
76 self.is_connected = response.success
78 return response
80 def native_query(self, query: str) -> StatusResponse:
81 """Receive and process a raw query.
82 Parameters
83 ----------
84 query : str
85 query in a native format
86 Returns
87 -------
88 StatusResponse
89 Request status
90 """
91 ast = parse_sql(query)
92 return self.query(ast)
95connection_args = OrderedDict(
96 strava_client_id={
97 'type': ARG_TYPE.STR,
98 'description': 'Client id for accessing Strava Application API'
99 },
100 strava_access_token={
101 'type': ARG_TYPE.STR,
102 'description': 'Access Token for accessing Strava Application API'
103 }
104)
106connection_args_example = OrderedDict(
107 strava_client_id='<your-strava-client_id>',
108 strava_access_token='<your-strava-api-token>'
109)