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

53 statements  

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

1from mindsdb.integrations.handlers.dockerhub_handler.dockerhub_tables import ( 

2 DockerHubRepoImagesSummaryTable, 

3 DockerHubRepoImagesTable, 

4 DockerHubRepoTagTable, 

5 DockerHubRepoTagsTable, 

6 DockerHubOrgSettingsTable 

7) 

8from mindsdb.integrations.handlers.dockerhub_handler.dockerhub import DockerHubClient 

9from mindsdb.integrations.libs.api_handler import APIHandler 

10from mindsdb.integrations.libs.response import ( 

11 HandlerStatusResponse as StatusResponse, 

12) 

13 

14from mindsdb.utilities import log 

15from mindsdb_sql_parser import parse_sql 

16 

17logger = log.getLogger(__name__) 

18 

19 

20class DockerHubHandler(APIHandler): 

21 """The DockerHub handler implementation""" 

22 

23 def __init__(self, name: str, **kwargs): 

24 """Initialize the DockerHub handler. 

25 

26 Parameters 

27 ---------- 

28 name : str 

29 name of a handler instance 

30 """ 

31 super().__init__(name) 

32 

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

34 self.connection_data = connection_data 

35 self.kwargs = kwargs 

36 self.docker_client = DockerHubClient() 

37 self.is_connected = False 

38 

39 repo_images_stats_data = DockerHubRepoImagesSummaryTable(self) 

40 self._register_table("repo_images_summary", repo_images_stats_data) 

41 

42 repo_images_data = DockerHubRepoImagesTable(self) 

43 self._register_table("repo_images", repo_images_data) 

44 

45 repo_tag_details_data = DockerHubRepoTagTable(self) 

46 self._register_table("repo_tag_details", repo_tag_details_data) 

47 

48 repo_tags_data = DockerHubRepoTagsTable(self) 

49 self._register_table("repo_tags", repo_tags_data) 

50 

51 org_settings = DockerHubOrgSettingsTable(self) 

52 self._register_table("org_settings", org_settings) 

53 

54 def connect(self) -> StatusResponse: 

55 """Set up the connection required by the handler. 

56 

57 Returns 

58 ------- 

59 StatusResponse 

60 connection object 

61 """ 

62 resp = StatusResponse(False) 

63 status = self.docker_client.login(self.connection_data.get("username"), self.connection_data.get("password")) 

64 if status["code"] != 200: 

65 resp.success = False 

66 resp.error_message = status["error"] 

67 return resp 

68 self.is_connected = True 

69 return resp 

70 

71 def check_connection(self) -> StatusResponse: 

72 """Check connection to the handler. 

73 

74 Returns 

75 ------- 

76 StatusResponse 

77 Status confirmation 

78 """ 

79 response = StatusResponse(False) 

80 

81 try: 

82 status = self.docker_client.login(self.connection_data.get("username"), self.connection_data.get("password")) 

83 if status["code"] == 200: 

84 current_user = self.connection_data.get("username") 

85 logger.info(f"Authenticated as user {current_user}") 

86 response.success = True 

87 else: 

88 response.success = False 

89 logger.info("Error connecting to dockerhub. " + status["error"]) 

90 response.error_message = status["error"] 

91 except Exception as e: 

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

93 response.error_message = e 

94 

95 self.is_connected = response.success 

96 return response 

97 

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

99 """Receive and process a raw query. 

100 

101 Parameters 

102 ---------- 

103 query : str 

104 query in a native format 

105 

106 Returns 

107 ------- 

108 StatusResponse 

109 Request status 

110 """ 

111 ast = parse_sql(query) 

112 return self.query(ast)