Coverage for mindsdb / integrations / handlers / confluence_handler / confluence_handler.py: 91%

41 statements  

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

1from typing import Any, Dict 

2 

3from mindsdb.integrations.handlers.confluence_handler.confluence_api_client import ConfluenceAPIClient 

4from mindsdb.integrations.handlers.confluence_handler.confluence_tables import ( 

5 ConfluenceBlogPostsTable, 

6 ConfluenceDatabasesTable, 

7 ConfluencePagesTable, 

8 ConfluenceSpacesTable, 

9 ConfluenceTasksTable, 

10 ConfluenceWhiteboardsTable, 

11) 

12from mindsdb.integrations.libs.api_handler import APIHandler 

13from mindsdb.integrations.libs.response import ( 

14 HandlerStatusResponse as StatusResponse, 

15) 

16from mindsdb.utilities import log 

17 

18 

19logger = log.getLogger(__name__) 

20 

21 

22class ConfluenceHandler(APIHandler): 

23 """ 

24 This handler handles the connection and execution of SQL statements on Confluence. 

25 """ 

26 

27 name = "confluence" 

28 

29 def __init__(self, name: str, connection_data: Dict, **kwargs: Any) -> None: 

30 """ 

31 Initializes the handler. 

32 

33 Args: 

34 name (str): The name of the handler instance. 

35 connection_data (Dict): The connection data required to connect to the Confluence API. 

36 kwargs: Arbitrary keyword arguments. 

37 """ 

38 super().__init__(name) 

39 self.connection_data = connection_data 

40 self.kwargs = kwargs 

41 

42 self.connection = None 

43 self.is_connected = False 

44 self.cache_thread_safe = True 

45 

46 self._register_table("spaces", ConfluenceSpacesTable(self)) 

47 self._register_table("pages", ConfluencePagesTable(self)) 

48 self._register_table("blogposts", ConfluenceBlogPostsTable(self)) 

49 self._register_table("whiteboards", ConfluenceWhiteboardsTable(self)) 

50 self._register_table("databases", ConfluenceDatabasesTable(self)) 

51 self._register_table("tasks", ConfluenceTasksTable(self)) 

52 

53 def connect(self) -> ConfluenceAPIClient: 

54 """ 

55 Establishes a connection to the Confluence API. 

56 

57 Raises: 

58 ValueError: If the required connection parameters are not provided. 

59 

60 Returns: 

61 atlassian.confluence.Confluence: A connection object to the Confluence API. 

62 """ 

63 if self.is_connected is True: 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true

64 return self.connection 

65 

66 if not all( 66 ↛ 70line 66 didn't jump to line 70 because the condition on line 66 was never true

67 key in self.connection_data and self.connection_data.get(key) 

68 for key in ["api_base", "username", "password"] 

69 ): 

70 raise ValueError( 

71 "Required parameters (api_base, username, password) must be provided and should not be empty." 

72 ) 

73 

74 self.connection = ConfluenceAPIClient( 

75 url=self.connection_data.get("api_base"), 

76 username=self.connection_data.get("username"), 

77 password=self.connection_data.get("password"), 

78 ) 

79 

80 self.is_connected = True 

81 return self.connection 

82 

83 def check_connection(self) -> StatusResponse: 

84 """ 

85 Checks the status of the connection to the Confluence API. 

86 

87 Returns: 

88 StatusResponse: An object containing the success status and an error message if an error occurs. 

89 """ 

90 response = StatusResponse(False) 

91 

92 try: 

93 connection = self.connect() 

94 connection.get_spaces(limit=1) 

95 response.success = True 

96 except Exception as e: 

97 logger.error(f"Error connecting to Confluence API: {e}!") 

98 response.error_message = e 

99 

100 self.is_connected = response.success 

101 

102 return response