Coverage for mindsdb / api / http / namespaces / tab.py: 33%

87 statements  

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

1import json 

2from http import HTTPStatus 

3 

4from flask import request 

5from flask_restx import Resource 

6 

7from mindsdb.metrics.metrics import api_endpoint_metrics 

8from mindsdb.api.http.namespaces.configs.tabs import ns_conf 

9from mindsdb.utilities import log 

10from mindsdb.api.http.utils import http_error 

11from mindsdb.utilities.exception import EntityNotExistsError 

12from mindsdb.interfaces.tabs.tabs_controller import tabs_controller, get_storage, TABS_FILENAME 

13 

14 

15logger = log.getLogger(__name__) 

16 

17 

18def _is_request_valid() -> bool: 

19 """check if request body contains all (and only) required fields 

20 

21 Returns: 

22 bool: True if all required data in the request 

23 """ 

24 try: 

25 data = request.json 

26 except Exception: 

27 return False 

28 if ( 

29 isinstance(data, dict) is False 

30 or len(data.keys()) == 0 

31 or len(set(data.keys()) - {"index", "name", "content"}) != 0 

32 ): 

33 return False 

34 return True 

35 

36 

37@ns_conf.route("/") 

38class Tabs(Resource): 

39 @ns_conf.doc("get_tabs") 

40 @api_endpoint_metrics("GET", "/tabs") 

41 def get(self): 

42 mode = request.args.get("mode") 

43 

44 if mode == "new": 

45 return tabs_controller.get_all(), 200 

46 else: 

47 # deprecated 

48 storage = get_storage() 

49 tabs = None 

50 try: 

51 raw_data = storage.file_get(TABS_FILENAME) 

52 tabs = json.loads(raw_data) 

53 except Exception: 

54 logger.warning("unable to get tabs data - %s", exc_info=True) 

55 return {}, 200 

56 return tabs, 200 

57 

58 @ns_conf.doc("save_tab") 

59 @api_endpoint_metrics("POST", "/tabs") 

60 def post(self): 

61 mode = request.args.get("mode") 

62 

63 if mode == "new": 

64 if _is_request_valid() is False: 

65 return http_error(400, "Error", "Invalid parameters") 

66 data = request.json 

67 tab_meta = tabs_controller.add(**data) 

68 tabs_meta = tabs_controller._get_tabs_meta() 

69 return {"tab_meta": tab_meta, "tabs_meta": tabs_meta}, 200 

70 else: 

71 # deprecated 

72 storage = get_storage() 

73 try: 

74 tabs = request.json 

75 b_types = json.dumps(tabs).encode("utf-8") 

76 storage.file_set(TABS_FILENAME, b_types) 

77 except Exception: 

78 logger.exception("Unable to store tabs data:") 

79 return http_error( 

80 HTTPStatus.INTERNAL_SERVER_ERROR, "Can't save tabs", "something went wrong during tabs saving" 

81 ) 

82 

83 return "", 200 

84 

85 

86@ns_conf.route("/<tab_id>") 

87@ns_conf.param("tab_id", "id of tab") 

88class Tab(Resource): 

89 @ns_conf.doc("get_tab") 

90 @api_endpoint_metrics("GET", "/tabs/tab") 

91 def get(self, tab_id: int): 

92 try: 

93 tab_data = tabs_controller.get(int(tab_id)) 

94 except EntityNotExistsError: 

95 return http_error(404, "Error", "The tab does not exist") 

96 

97 return tab_data, 200 

98 

99 @ns_conf.doc("put_tab") 

100 @api_endpoint_metrics("PUT", "/tabs/tab") 

101 def put(self, tab_id: int): 

102 if _is_request_valid() is False: 

103 return http_error(400, "Error", "Invalid parameters") 

104 data = request.json 

105 try: 

106 tab_meta = tabs_controller.modify(int(tab_id), **data) 

107 except EntityNotExistsError: 

108 return http_error(404, "Error", "The tab does not exist") 

109 

110 tabs_meta = tabs_controller._get_tabs_meta() 

111 

112 return {"tab_meta": tab_meta, "tabs_meta": tabs_meta}, 200 

113 

114 @ns_conf.doc("delete_tab") 

115 @api_endpoint_metrics("DELETE", "/tabs/tab") 

116 def delete(self, tab_id: int): 

117 try: 

118 tabs_controller.delete(int(tab_id)) 

119 except EntityNotExistsError: 

120 return http_error(404, "Error", "The tab does not exist") 

121 return "", 200