Coverage for mindsdb / integrations / handlers / oilpriceapi_handler / oilpriceapi_handler.py: 0%
46 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.oilpriceapi_handler.oilpriceapi_tables import (
2 OilPriceLatestTable,
3 OilPricePastDayPriceTable
4)
5from mindsdb.integrations.handlers.oilpriceapi_handler.oilpriceapi import OilPriceAPIClient
6from mindsdb.integrations.libs.api_handler import APIHandler
7from mindsdb.integrations.libs.response import (
8 HandlerStatusResponse as StatusResponse,
9)
11from mindsdb.utilities import log
12from mindsdb_sql_parser import parse_sql
15logger = log.getLogger(__name__)
18class OilPriceAPIHandler(APIHandler):
19 """The OilPriceAPI handler implementation"""
21 def __init__(self, name: str, **kwargs):
22 """Initialize the OilPriceAPI handler.
24 Parameters
25 ----------
26 name : str
27 name of a handler instance
28 """
29 super().__init__(name)
31 connection_data = kwargs.get("connection_data", {})
32 self.connection_data = connection_data
33 self.kwargs = kwargs
34 self.client = OilPriceAPIClient(self.connection_data["api_key"])
35 self.is_connected = False
37 latest_price_data = OilPriceLatestTable(self)
38 self._register_table("latest_price", latest_price_data)
40 past_day_price_data = OilPricePastDayPriceTable(self)
41 self._register_table("past_day_price", past_day_price_data)
43 def connect(self) -> StatusResponse:
44 """Set up the connection required by the handler.
46 Returns
47 -------
48 StatusResponse
49 connection object
50 """
51 resp = StatusResponse(False)
52 status = self.client.get_latest_price()
53 if status["code"] != 200:
54 resp.success = False
55 resp.error_message = status["error"]
56 return resp
57 self.is_connected = True
58 return resp
60 def check_connection(self) -> StatusResponse:
61 """Check connection to the handler.
63 Returns
64 -------
65 StatusResponse
66 Status confirmation
67 """
68 response = StatusResponse(False)
70 try:
71 status = self.client.get_latest_price()
72 if status["code"] == 200:
73 logger.info("Authentication successful")
74 response.success = True
75 else:
76 response.success = False
77 logger.info("Error connecting to OilPriceAPI. " + status["error"])
78 response.error_message = status["error"]
79 except Exception as e:
80 logger.error(f"Error connecting to OilPriceAPI: {e}!")
81 response.error_message = e
83 self.is_connected = response.success
84 return response
86 def native_query(self, query: str) -> StatusResponse:
87 """Receive and process a raw query.
89 Parameters
90 ----------
91 query : str
92 query in a native format
94 Returns
95 -------
96 StatusResponse
97 Request status
98 """
99 ast = parse_sql(query)
100 return self.query(ast)