Coverage for mindsdb / integrations / handlers / email_handler / email_handler.py: 0%

45 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1from mindsdb.utilities import log 

2 

3from mindsdb.integrations.libs.api_handler import APIHandler 

4 

5from mindsdb.integrations.libs.response import ( 

6 HandlerStatusResponse as StatusResponse, 

7) 

8from mindsdb_sql_parser import parse_sql 

9 

10from mindsdb.integrations.handlers.email_handler.email_tables import EmailsTable 

11from mindsdb.integrations.handlers.email_handler.email_client import EmailClient 

12from mindsdb.integrations.handlers.email_handler.settings import EmailConnectionDetails 

13 

14 

15logger = log.getLogger(__name__) 

16 

17 

18class EmailHandler(APIHandler): 

19 """ 

20 A class for handling connections and interactions with Email (send and search). 

21 

22 Parameters 

23 ---------- 

24 name : str 

25 The name of the handler 

26 connection_data : EmailConnectionDetails 

27 The connection details for the email server 

28 

29 see `EmailConnectionDetails` for more details and examples 

30 

31 """ 

32 

33 def __init__(self, name=None, **kwargs): 

34 super().__init__(name) 

35 

36 connection_data = kwargs.get("connection_data", {}) 

37 self.connection_data = EmailConnectionDetails(**connection_data) 

38 self.kwargs = kwargs 

39 

40 self.connection = None 

41 self.is_connected = False 

42 

43 emails = EmailsTable(self) 

44 self._register_table('emails', emails) 

45 

46 def connect(self): 

47 """Authenticate with the email servers using credentials.""" 

48 

49 if self.is_connected is True: 

50 return self.connection 

51 

52 try: 

53 self.connection = EmailClient(self.connection_data) 

54 except Exception as e: 

55 logger.error(f'Error connecting to email api: {e}!') 

56 raise e 

57 

58 self.is_connected = True 

59 return self.connection 

60 

61 def disconnect(self): 

62 """ Close any existing connections 

63 

64 Should switch self.is_connected. 

65 """ 

66 self.is_connected = False 

67 

68 return self.connection.logout() 

69 

70 def check_connection(self) -> StatusResponse: 

71 

72 response = StatusResponse(False) 

73 

74 try: 

75 self.connect() 

76 response.success = True 

77 

78 except Exception as e: 

79 response.error_message = f'Error connecting to Email: {e}. ' 

80 logger.error(response.error_message) 

81 

82 if response.success is False and self.is_connected is True: 

83 self.is_connected = False 

84 

85 return response 

86 

87 def native_query(self, query: str) -> StatusResponse: 

88 """Receive and process a raw query. 

89 Parameters 

90 ---------- 

91 query : str 

92 query in a native format 

93 Returns 

94 ------- 

95 StatusResponse 

96 Request status 

97 """ 

98 ast = parse_sql(query) 

99 return self.query(ast)