aiverify-moonshot 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. aiverify_moonshot-0.4.0.dist-info/METADATA +249 -0
  2. aiverify_moonshot-0.4.0.dist-info/RECORD +163 -0
  3. aiverify_moonshot-0.4.0.dist-info/WHEEL +4 -0
  4. aiverify_moonshot-0.4.0.dist-info/licenses/AUTHORS.md +5 -0
  5. aiverify_moonshot-0.4.0.dist-info/licenses/LICENSE.md +201 -0
  6. aiverify_moonshot-0.4.0.dist-info/licenses/NOTICES.md +3340 -0
  7. moonshot/__init__.py +0 -0
  8. moonshot/__main__.py +198 -0
  9. moonshot/api.py +155 -0
  10. moonshot/integrations/__init__.py +0 -0
  11. moonshot/integrations/cli/__init__.py +0 -0
  12. moonshot/integrations/cli/__main__.py +25 -0
  13. moonshot/integrations/cli/active_session_cfg.py +1 -0
  14. moonshot/integrations/cli/benchmark/__init__.py +0 -0
  15. moonshot/integrations/cli/benchmark/benchmark.py +186 -0
  16. moonshot/integrations/cli/benchmark/cookbook.py +545 -0
  17. moonshot/integrations/cli/benchmark/datasets.py +164 -0
  18. moonshot/integrations/cli/benchmark/metrics.py +141 -0
  19. moonshot/integrations/cli/benchmark/recipe.py +598 -0
  20. moonshot/integrations/cli/benchmark/result.py +216 -0
  21. moonshot/integrations/cli/benchmark/run.py +140 -0
  22. moonshot/integrations/cli/benchmark/runner.py +174 -0
  23. moonshot/integrations/cli/cli.py +64 -0
  24. moonshot/integrations/cli/common/__init__.py +0 -0
  25. moonshot/integrations/cli/common/common.py +72 -0
  26. moonshot/integrations/cli/common/connectors.py +325 -0
  27. moonshot/integrations/cli/common/display_helper.py +42 -0
  28. moonshot/integrations/cli/common/prompt_template.py +94 -0
  29. moonshot/integrations/cli/initialisation/__init__.py +0 -0
  30. moonshot/integrations/cli/initialisation/initialisation.py +14 -0
  31. moonshot/integrations/cli/redteam/__init__.py +0 -0
  32. moonshot/integrations/cli/redteam/attack_module.py +70 -0
  33. moonshot/integrations/cli/redteam/context_strategy.py +147 -0
  34. moonshot/integrations/cli/redteam/prompt_template.py +67 -0
  35. moonshot/integrations/cli/redteam/redteam.py +90 -0
  36. moonshot/integrations/cli/redteam/session.py +467 -0
  37. moonshot/integrations/web_api/.env.dev +7 -0
  38. moonshot/integrations/web_api/__init__.py +0 -0
  39. moonshot/integrations/web_api/__main__.py +56 -0
  40. moonshot/integrations/web_api/app.py +125 -0
  41. moonshot/integrations/web_api/container.py +146 -0
  42. moonshot/integrations/web_api/log/.gitkeep +0 -0
  43. moonshot/integrations/web_api/logging_conf.py +114 -0
  44. moonshot/integrations/web_api/routes/__init__.py +0 -0
  45. moonshot/integrations/web_api/routes/attack_modules.py +66 -0
  46. moonshot/integrations/web_api/routes/benchmark.py +116 -0
  47. moonshot/integrations/web_api/routes/benchmark_result.py +175 -0
  48. moonshot/integrations/web_api/routes/context_strategy.py +129 -0
  49. moonshot/integrations/web_api/routes/cookbook.py +225 -0
  50. moonshot/integrations/web_api/routes/dataset.py +120 -0
  51. moonshot/integrations/web_api/routes/endpoint.py +282 -0
  52. moonshot/integrations/web_api/routes/metric.py +78 -0
  53. moonshot/integrations/web_api/routes/prompt_template.py +128 -0
  54. moonshot/integrations/web_api/routes/recipe.py +219 -0
  55. moonshot/integrations/web_api/routes/redteam.py +609 -0
  56. moonshot/integrations/web_api/routes/runner.py +239 -0
  57. moonshot/integrations/web_api/schemas/__init__.py +0 -0
  58. moonshot/integrations/web_api/schemas/benchmark_runner_dto.py +13 -0
  59. moonshot/integrations/web_api/schemas/cookbook_create_dto.py +19 -0
  60. moonshot/integrations/web_api/schemas/cookbook_response_model.py +9 -0
  61. moonshot/integrations/web_api/schemas/dataset_response_dto.py +9 -0
  62. moonshot/integrations/web_api/schemas/endpoint_create_dto.py +21 -0
  63. moonshot/integrations/web_api/schemas/endpoint_response_model.py +11 -0
  64. moonshot/integrations/web_api/schemas/prompt_response_model.py +14 -0
  65. moonshot/integrations/web_api/schemas/prompt_template_response_model.py +10 -0
  66. moonshot/integrations/web_api/schemas/recipe_create_dto.py +32 -0
  67. moonshot/integrations/web_api/schemas/recipe_response_model.py +7 -0
  68. moonshot/integrations/web_api/schemas/session_create_dto.py +16 -0
  69. moonshot/integrations/web_api/schemas/session_prompt_dto.py +7 -0
  70. moonshot/integrations/web_api/schemas/session_response_model.py +38 -0
  71. moonshot/integrations/web_api/services/__init__.py +0 -0
  72. moonshot/integrations/web_api/services/attack_module_service.py +34 -0
  73. moonshot/integrations/web_api/services/auto_red_team_test_manager.py +86 -0
  74. moonshot/integrations/web_api/services/auto_red_team_test_state.py +57 -0
  75. moonshot/integrations/web_api/services/base_service.py +8 -0
  76. moonshot/integrations/web_api/services/benchmark_result_service.py +25 -0
  77. moonshot/integrations/web_api/services/benchmark_test_manager.py +106 -0
  78. moonshot/integrations/web_api/services/benchmark_test_state.py +56 -0
  79. moonshot/integrations/web_api/services/benchmarking_service.py +31 -0
  80. moonshot/integrations/web_api/services/context_strategy_service.py +22 -0
  81. moonshot/integrations/web_api/services/cookbook_service.py +194 -0
  82. moonshot/integrations/web_api/services/dataset_service.py +20 -0
  83. moonshot/integrations/web_api/services/endpoint_service.py +65 -0
  84. moonshot/integrations/web_api/services/metric_service.py +14 -0
  85. moonshot/integrations/web_api/services/prompt_template_service.py +39 -0
  86. moonshot/integrations/web_api/services/recipe_service.py +155 -0
  87. moonshot/integrations/web_api/services/runner_service.py +147 -0
  88. moonshot/integrations/web_api/services/session_service.py +350 -0
  89. moonshot/integrations/web_api/services/utils/exceptions_handler.py +41 -0
  90. moonshot/integrations/web_api/services/utils/results_formatter.py +47 -0
  91. moonshot/integrations/web_api/status_updater/interface/benchmark_progress_callback.py +14 -0
  92. moonshot/integrations/web_api/status_updater/interface/redteam_progress_callback.py +14 -0
  93. moonshot/integrations/web_api/status_updater/moonshot_ui_webhook.py +72 -0
  94. moonshot/integrations/web_api/types/types.py +99 -0
  95. moonshot/src/__init__.py +0 -0
  96. moonshot/src/api/__init__.py +0 -0
  97. moonshot/src/api/api_connector.py +58 -0
  98. moonshot/src/api/api_connector_endpoint.py +162 -0
  99. moonshot/src/api/api_context_strategy.py +57 -0
  100. moonshot/src/api/api_cookbook.py +160 -0
  101. moonshot/src/api/api_dataset.py +46 -0
  102. moonshot/src/api/api_environment_variables.py +17 -0
  103. moonshot/src/api/api_metrics.py +51 -0
  104. moonshot/src/api/api_prompt_template.py +43 -0
  105. moonshot/src/api/api_recipe.py +182 -0
  106. moonshot/src/api/api_red_teaming.py +59 -0
  107. moonshot/src/api/api_result.py +84 -0
  108. moonshot/src/api/api_run.py +74 -0
  109. moonshot/src/api/api_runner.py +132 -0
  110. moonshot/src/api/api_session.py +290 -0
  111. moonshot/src/configs/__init__.py +0 -0
  112. moonshot/src/configs/env_variables.py +187 -0
  113. moonshot/src/connectors/__init__.py +0 -0
  114. moonshot/src/connectors/connector.py +327 -0
  115. moonshot/src/connectors/connector_prompt_arguments.py +17 -0
  116. moonshot/src/connectors_endpoints/__init__.py +0 -0
  117. moonshot/src/connectors_endpoints/connector_endpoint.py +211 -0
  118. moonshot/src/connectors_endpoints/connector_endpoint_arguments.py +54 -0
  119. moonshot/src/cookbooks/__init__.py +0 -0
  120. moonshot/src/cookbooks/cookbook.py +225 -0
  121. moonshot/src/cookbooks/cookbook_arguments.py +34 -0
  122. moonshot/src/datasets/__init__.py +0 -0
  123. moonshot/src/datasets/dataset.py +255 -0
  124. moonshot/src/datasets/dataset_arguments.py +50 -0
  125. moonshot/src/metrics/__init__.py +0 -0
  126. moonshot/src/metrics/metric.py +192 -0
  127. moonshot/src/metrics/metric_interface.py +95 -0
  128. moonshot/src/prompt_templates/__init__.py +0 -0
  129. moonshot/src/prompt_templates/prompt_template.py +103 -0
  130. moonshot/src/recipes/__init__.py +0 -0
  131. moonshot/src/recipes/recipe.py +340 -0
  132. moonshot/src/recipes/recipe_arguments.py +111 -0
  133. moonshot/src/redteaming/__init__.py +0 -0
  134. moonshot/src/redteaming/attack/__init__.py +0 -0
  135. moonshot/src/redteaming/attack/attack_module.py +618 -0
  136. moonshot/src/redteaming/attack/attack_module_arguments.py +44 -0
  137. moonshot/src/redteaming/attack/context_strategy.py +131 -0
  138. moonshot/src/redteaming/context_strategy/__init__.py +0 -0
  139. moonshot/src/redteaming/context_strategy/context_strategy_interface.py +46 -0
  140. moonshot/src/redteaming/session/__init__.py +0 -0
  141. moonshot/src/redteaming/session/chat.py +209 -0
  142. moonshot/src/redteaming/session/red_teaming_progress.py +128 -0
  143. moonshot/src/redteaming/session/red_teaming_type.py +6 -0
  144. moonshot/src/redteaming/session/session.py +775 -0
  145. moonshot/src/results/__init__.py +0 -0
  146. moonshot/src/results/result.py +119 -0
  147. moonshot/src/results/result_arguments.py +44 -0
  148. moonshot/src/runners/__init__.py +0 -0
  149. moonshot/src/runners/runner.py +476 -0
  150. moonshot/src/runners/runner_arguments.py +46 -0
  151. moonshot/src/runners/runner_type.py +6 -0
  152. moonshot/src/runs/__init__.py +0 -0
  153. moonshot/src/runs/run.py +344 -0
  154. moonshot/src/runs/run_arguments.py +162 -0
  155. moonshot/src/runs/run_progress.py +145 -0
  156. moonshot/src/runs/run_status.py +10 -0
  157. moonshot/src/storage/__init__.py +0 -0
  158. moonshot/src/storage/db_interface.py +128 -0
  159. moonshot/src/storage/io_interface.py +31 -0
  160. moonshot/src/storage/storage.py +525 -0
  161. moonshot/src/utils/__init__.py +0 -0
  162. moonshot/src/utils/import_modules.py +96 -0
  163. moonshot/src/utils/timeit.py +25 -0
@@ -0,0 +1,129 @@
1
+ from dependency_injector.wiring import Provide, inject
2
+ from fastapi import APIRouter, Depends, HTTPException
3
+
4
+ from ..container import Container
5
+ from ..services.context_strategy_service import ContextStrategyService
6
+ from ..services.utils.exceptions_handler import ServiceException
7
+
8
+ router = APIRouter(tags=["Context Strategy"])
9
+
10
+ @router.get("/api/v1/context-strategies")
11
+ @inject
12
+ def get_all_context_strategies(
13
+ context_strategy_service: ContextStrategyService = Depends(
14
+ Provide[Container.context_strategy_service]
15
+ ),
16
+ ) -> list[dict]:
17
+ """
18
+ Retrieve all context strategies from the database.
19
+
20
+ Args:
21
+ context_strategy_service (ContextStrategyService): The service responsible for retrieving context strategies.
22
+
23
+ Returns:
24
+ list[dict]: A list of context strategies with details.
25
+
26
+ Raises:
27
+ HTTPException: An error with status code 404 if no context strategies are found.
28
+ An error with status code 400 if there is a validation error.
29
+ An error with status code 500 for any other server-side error.
30
+ """
31
+ try:
32
+ return context_strategy_service.get_ctx_strategy()
33
+ except ServiceException as e:
34
+ if e.error_code == "FileNotFound":
35
+ raise HTTPException(
36
+ status_code=404,
37
+ detail=f"Failed to retrieve context strategies: {e.msg}",
38
+ )
39
+ elif e.error_code == "ValidationError":
40
+ raise HTTPException(
41
+ status_code=400,
42
+ detail=f"Failed to retrieve context strategies: {e.msg}",
43
+ )
44
+ else:
45
+ raise HTTPException(
46
+ status_code=500,
47
+ detail=f"Failed to retrieve context strategies: {e.msg}",
48
+ )
49
+
50
+ @router.get("/api/v1/context-strategies/name")
51
+ @inject
52
+ def get_all_context_strategies_name(
53
+ context_strategy_service: ContextStrategyService = Depends(
54
+ Provide[Container.context_strategy_service]
55
+ ),
56
+ ) -> list[str]:
57
+ """
58
+ Retrieve all context strategies from the database.
59
+
60
+ Args:
61
+ context_strategy_service (ContextStrategyService): The service responsible for retrieving context strategies.
62
+
63
+ Returns:
64
+ list[str]: A list of context strategies.
65
+
66
+ Raises:
67
+ HTTPException: An error with status code 404 if no context strategies are found.
68
+ An error with status code 400 if there is a validation error.
69
+ An error with status code 500 for any other server-side error.
70
+ """
71
+ try:
72
+ return context_strategy_service.get_ctx_strategy_name()
73
+ except ServiceException as e:
74
+ if e.error_code == "FileNotFound":
75
+ raise HTTPException(
76
+ status_code=404,
77
+ detail=f"Failed to retrieve context strategies: {e.msg}",
78
+ )
79
+ elif e.error_code == "ValidationError":
80
+ raise HTTPException(
81
+ status_code=400,
82
+ detail=f"Failed to retrieve context strategies: {e.msg}",
83
+ )
84
+ else:
85
+ raise HTTPException(
86
+ status_code=500,
87
+ detail=f"Failed to retrieve context strategies: {e.msg}",
88
+ )
89
+
90
+
91
+ @router.delete("/api/v1/context-strategies/{ctx_strategy_name}")
92
+ @inject
93
+ def delete_context_strategy(
94
+ ctx_strategy_name: str,
95
+ context_strategy_service: ContextStrategyService = Depends(
96
+ Provide[Container.context_strategy_service]
97
+ ),
98
+ ) -> dict[str, bool]:
99
+ """
100
+ Delete a context strategy from the database by its name.
101
+
102
+ Args:
103
+ ctx_strategy_name (str): The name of the context strategy to delete.
104
+ context_strategy_service (ContextStrategyService): The service responsible for deleting the context strategy.
105
+
106
+ Returns:
107
+ dict[str, bool]: A dictionary with a key 'success' indicating the result of the deletion operation.
108
+
109
+ Raises:
110
+ HTTPException: An error with status code 404 if the context strategy is not found.
111
+ An error with status code 400 if there is a validation error.
112
+ An error with status code 500 for any other server-side error.
113
+ """
114
+ try:
115
+ context_strategy_service.delete_ctx_strategy(ctx_strategy_name)
116
+ return {"success": True}
117
+ except ServiceException as e:
118
+ if e.error_code == "FileNotFound":
119
+ raise HTTPException(
120
+ status_code=404, detail=f"Failed to delete context strategy: {e.msg}"
121
+ )
122
+ elif e.error_code == "ValidationError":
123
+ raise HTTPException(
124
+ status_code=400, detail=f"Failed to delete context strategy: {e.msg}"
125
+ )
126
+ else:
127
+ raise HTTPException(
128
+ status_code=500, detail=f"Failed to delete context strategy: {e.msg}"
129
+ )
@@ -0,0 +1,225 @@
1
+ from typing import Optional
2
+
3
+ from dependency_injector.wiring import Provide, inject
4
+ from fastapi import APIRouter, Depends, HTTPException, Query
5
+
6
+ from ..container import Container
7
+ from ..schemas.cookbook_create_dto import CookbookCreateDTO, CookbookUpdateDTO
8
+ from ..schemas.cookbook_response_model import CookbookResponseModel
9
+ from ..services.cookbook_service import CookbookService
10
+ from ..services.utils.exceptions_handler import ServiceException
11
+
12
+ router = APIRouter(tags=["Cookbook"])
13
+
14
+
15
+ @router.post("/api/v1/cookbooks")
16
+ @inject
17
+ def create_cookbook(
18
+ cookbook_data: CookbookCreateDTO,
19
+ cookbook_service: CookbookService = Depends(Provide[Container.cookbook_service]),
20
+ ) -> dict[str, str]:
21
+ """
22
+ Endpoint to create a new cookbook entry in the database.
23
+
24
+ Parameters:
25
+ cookbook_data (CookbookCreateDTO): The DTO containing the details for the new cookbook.
26
+ cookbook_service (CookbookService): The service layer responsible for the creation logic.
27
+
28
+ Returns:
29
+ dict[str, str]: A dictionary with a message key indicating successful creation.
30
+
31
+ Raises:
32
+ HTTPException: 404 if the cookbook cannot be found.
33
+ 400 if there is a validation error with the provided data.
34
+ 500 for any other internal server error.
35
+ """
36
+ try:
37
+ cookbook_service.create_cookbook(cookbook_data)
38
+ return {"message": "Cookbook created successfully"}
39
+ except ServiceException as e:
40
+ if e.error_code == "FileNotFound":
41
+ raise HTTPException(
42
+ status_code=404, detail=f"Failed to create cookbook: {e.msg}"
43
+ )
44
+ elif e.error_code == "ValidationError":
45
+ raise HTTPException(
46
+ status_code=400, detail=f"Failed to create cookbook: {e.msg}"
47
+ )
48
+ else:
49
+ raise HTTPException(
50
+ status_code=500, detail=f"Failed to create cookbook: {e.msg}"
51
+ )
52
+
53
+
54
+ @router.get("/api/v1/cookbooks")
55
+ @inject
56
+ def get_all_cookbooks(
57
+ ids: Optional[str] = Query(None, description="Get recipes to query"),
58
+ tags: Optional[str] = Query(None, description="Filter cookbooks by tags"),
59
+ categories: Optional[str] = Query(
60
+ None, description="Filter cookbooks by categories"
61
+ ),
62
+ categories_excluded: Optional[str] = Query(
63
+ None, description="Filter out (exlude) cookbooks by categories"
64
+ ),
65
+ count: bool = Query(False, description="Whether to include the count of recipes"),
66
+ cookbook_service: CookbookService = Depends(Provide[Container.cookbook_service]),
67
+ ) -> list[CookbookResponseModel]:
68
+ """
69
+ Endpoint to retrieve a list of all cookbooks, with optional filtering.
70
+
71
+ Parameters:
72
+ ids (Optional[str]): A string to filter cookbooks by ids.
73
+ tags (Optional[str]): A string to filter cookbooks by tags.
74
+ categories (Optional[str]): A string to filter cookbooks by categories.
75
+ categories_excluded (Optional[str]): A string to filter out (exclude) cookbooks by categories.
76
+ count (bool): A flag to decide if the count of recipes should be included.
77
+ cookbook_service (CookbookService): The service layer responsible for retrieval logic.
78
+
79
+ Returns:
80
+ list[CookbookResponseModel]: A list of CookbookResponseModel instances that match the filters.
81
+
82
+ Raises:
83
+ HTTPException: 404 if no cookbooks could be found.
84
+ 400 if there is a validation error with the filters.
85
+ 500 for any other internal server error.
86
+ """
87
+ try:
88
+ cookbooks = cookbook_service.get_all_cookbooks(
89
+ tags=tags,
90
+ categories=categories,
91
+ count=count,
92
+ ids=ids,
93
+ categories_excluded=categories_excluded,
94
+ )
95
+ return cookbooks
96
+ except ServiceException as e:
97
+ if e.error_code == "FileNotFound":
98
+ raise HTTPException(
99
+ status_code=404, detail=f"Failed to retrieve cookbooks: {e.msg}"
100
+ )
101
+ elif e.error_code == "ValidationError":
102
+ raise HTTPException(
103
+ status_code=400, detail=f"Failed to retrieve cookbooks: {e.msg}"
104
+ )
105
+ else:
106
+ raise HTTPException(
107
+ status_code=500, detail=f"Failed to retrieve cookbooks: {e.msg}"
108
+ )
109
+
110
+
111
+ @router.get("/api/v1/cookbooks/name")
112
+ @inject
113
+ def get_all_cookbooks_name(
114
+ cookbook_service: CookbookService = Depends(Provide[Container.cookbook_service]),
115
+ ) -> list[str]:
116
+ """
117
+ Endpoint to retrieve the names of all cookbooks in the database.
118
+
119
+ Parameters:
120
+ cookbook_service (CookbookService): The service layer responsible for retrieving the names.
121
+
122
+ Returns:
123
+ list[str]: A list of strings representing the names of all cookbooks.
124
+
125
+ Raises:
126
+ HTTPException: 404 if no cookbook names could be found.
127
+ 400 if there is a validation error.
128
+ 500 for any other internal server error.
129
+ """
130
+ try:
131
+ cookbooks = cookbook_service.get_all_cookbooks_names()
132
+ return cookbooks
133
+ except ServiceException as e:
134
+ if e.error_code == "FileNotFound":
135
+ raise HTTPException(
136
+ status_code=404, detail=f"Failed to retrieve cookbooks: {e.msg}"
137
+ )
138
+ elif e.error_code == "ValidationError":
139
+ raise HTTPException(
140
+ status_code=400, detail=f"Failed to retrieve cookbooks: {e.msg}"
141
+ )
142
+ else:
143
+ raise HTTPException(
144
+ status_code=500, detail=f"Failed to retrieve cookbooks: {e.msg}"
145
+ )
146
+
147
+
148
+ @router.put("/api/v1/cookbooks/{cookbook_id}")
149
+ @inject
150
+ def update_cookbook(
151
+ cookbook_id: str,
152
+ cookbook_data: CookbookUpdateDTO,
153
+ cookbook_service: CookbookService = Depends(Provide[Container.cookbook_service]),
154
+ ) -> dict[str, str]:
155
+ """
156
+ Endpoint to update the details of an existing cookbook in the database.
157
+
158
+ Parameters:
159
+ cookbook_id (str): The unique identifier of the cookbook to be updated.
160
+ cookbook_data (CookbookCreateDTO): The DTO containing the updated details for the cookbook.
161
+ cookbook_service (CookbookService): The service layer responsible for the update logic.
162
+
163
+ Returns:
164
+ dict[str, str]: A dictionary with a message key indicating successful update.
165
+
166
+ Raises:
167
+ HTTPException: 404 if the cookbook with the given ID cannot be found.
168
+ 400 if there is a validation error with the provided data.
169
+ 500 for any other internal server error.
170
+ """
171
+ try:
172
+ cookbook_service.update_cookbook(cookbook_data, cookbook_id)
173
+ return {"message": "Cookbook updated successfully"}
174
+ except ServiceException as e:
175
+ if e.error_code == "FileNotFound":
176
+ raise HTTPException(
177
+ status_code=404, detail=f"Failed to update cookbook: {e.msg}"
178
+ )
179
+ elif e.error_code == "ValidationError":
180
+ raise HTTPException(
181
+ status_code=400, detail=f"Failed to update cookbook: {e.msg}"
182
+ )
183
+ else:
184
+ raise HTTPException(
185
+ status_code=500, detail=f"Failed to update cookbook: {e.msg}"
186
+ )
187
+
188
+
189
+ @router.delete("/api/v1/cookbooks/{cb_id}")
190
+ @inject
191
+ def delete_cookbook(
192
+ cb_id: str,
193
+ cookbook_service: CookbookService = Depends(Provide[Container.cookbook_service]),
194
+ ) -> dict[str, str]:
195
+ """
196
+ Endpoint to delete a cookbook entry from the database using its ID.
197
+
198
+ Parameters:
199
+ cb_id (str): The unique identifier of the cookbook to be deleted.
200
+ cookbook_service (CookbookService): The service layer responsible for the deletion logic.
201
+
202
+ Returns:
203
+ dict[str, str]: A dictionary with a message key indicating successful deletion.
204
+
205
+ Raises:
206
+ HTTPException: 404 if the cookbook with the given ID cannot be found.
207
+ 400 if there is a validation error.
208
+ 500 for any other internal server error.
209
+ """
210
+ try:
211
+ cookbook_service.delete_cookbook(cb_id)
212
+ return {"message": "Cookbook deleted successfully"}
213
+ except ServiceException as e:
214
+ if e.error_code == "FileNotFound":
215
+ raise HTTPException(
216
+ status_code=404, detail=f"Failed to delete cookbook: {e.msg}"
217
+ )
218
+ elif e.error_code == "ValidationError":
219
+ raise HTTPException(
220
+ status_code=400, detail=f"Failed to delete cookbook: {e.msg}"
221
+ )
222
+ else:
223
+ raise HTTPException(
224
+ status_code=500, detail=f"Failed to delete cookbook: {e.msg}"
225
+ )
@@ -0,0 +1,120 @@
1
+ from dependency_injector.wiring import Provide, inject
2
+ from fastapi import APIRouter, Depends, HTTPException
3
+
4
+ from ..container import Container
5
+ from ..schemas.dataset_response_dto import DatasetResponseDTO
6
+ from ..services.dataset_service import DatasetService
7
+ from ..services.utils.exceptions_handler import ServiceException
8
+
9
+ router = APIRouter(tags=["Datasets"])
10
+
11
+
12
+ @router.get("/api/v1/datasets")
13
+ @inject
14
+ def get_all_datasets(
15
+ dataset_service: DatasetService = Depends(Provide[Container.dataset_service]),
16
+ ) -> list[DatasetResponseDTO]:
17
+ """
18
+ Retrieve all datasets from the database.
19
+
20
+ Args:
21
+ dataset_service (DatasetService): The service responsible for retrieving datasets.
22
+
23
+ Returns:
24
+ list: A list of all datasets.
25
+
26
+ Raises:
27
+ HTTPException: An error with status code 404 if no datasets are found.
28
+ An error with status code 400 if there is a validation error.
29
+ An error with status code 500 for any other server-side error.
30
+ """
31
+ try:
32
+ return dataset_service.get_all_datasets()
33
+ except ServiceException as e:
34
+ if e.error_code == "FileNotFound":
35
+ raise HTTPException(
36
+ status_code=404, detail=f"Failed to retrieve datasets: {e.msg}"
37
+ )
38
+ elif e.error_code == "ValidationError":
39
+ raise HTTPException(
40
+ status_code=400, detail=f"Failed to retrieve datasets: {e.msg}"
41
+ )
42
+ else:
43
+ raise HTTPException(
44
+ status_code=500, detail=f"Failed to retrieve datasets: {e.msg}"
45
+ )
46
+
47
+
48
+ @router.get("/api/v1/datasets/name")
49
+ @inject
50
+ def get_all_datasets_name(
51
+ dataset_service: DatasetService = Depends(Provide[Container.dataset_service]),
52
+ ) -> list[str]:
53
+ """
54
+ Retrieve the names of all datasets from the database.
55
+
56
+ Args:
57
+ dataset_service (DatasetService): The service responsible for retrieving dataset names.
58
+
59
+ Returns:
60
+ list[str]: A list of dataset names.
61
+
62
+ Raises:
63
+ HTTPException: An error with status code 404 if no dataset names are found.
64
+ An error with status code 400 if there is a validation error.
65
+ An error with status code 500 for any other server-side error.
66
+ """
67
+ try:
68
+ return dataset_service.get_all_datasets_name()
69
+ except ServiceException as e:
70
+ if e.error_code == "FileNotFound":
71
+ raise HTTPException(
72
+ status_code=404, detail=f"Failed to retrieve dataset names: {e.msg}"
73
+ )
74
+ elif e.error_code == "ValidationError":
75
+ raise HTTPException(
76
+ status_code=400, detail=f"Failed to retrieve dataset names: {e.msg}"
77
+ )
78
+ else:
79
+ raise HTTPException(
80
+ status_code=500, detail=f"Failed to retrieve dataset names: {e.msg}"
81
+ )
82
+
83
+
84
+ @router.delete("/api/v1/datasets/{dataset_id}")
85
+ @inject
86
+ def delete_dataset(
87
+ dataset_id: str,
88
+ dataset_service: DatasetService = Depends(Provide[Container.dataset_service]),
89
+ ) -> dict[str, str]:
90
+ """
91
+ Delete a dataset from the database by its ID.
92
+
93
+ Args:
94
+ dataset_id (str): The unique identifier of the dataset to delete.
95
+ dataset_service (DatasetService): The service responsible for deleting the dataset.
96
+
97
+ Returns:
98
+ dict[str, str]: A message indicating the successful deletion of the dataset.
99
+
100
+ Raises:
101
+ HTTPException: An error with status code 404 if the dataset is not found.
102
+ An error with status code 400 if there is a validation error.
103
+ An error with status code 500 for any other server-side error.
104
+ """
105
+ try:
106
+ dataset_service.delete_dataset(dataset_id)
107
+ return {"message": "Dataset deleted successfully"}
108
+ except ServiceException as e:
109
+ if e.error_code == "FileNotFound":
110
+ raise HTTPException(
111
+ status_code=404, detail=f"Failed to delete dataset: {e.msg}"
112
+ )
113
+ elif e.error_code == "ValidationError":
114
+ raise HTTPException(
115
+ status_code=400, detail=f"Failed to delete dataset: {e.msg}"
116
+ )
117
+ else:
118
+ raise HTTPException(
119
+ status_code=500, detail=f"Failed to delete dataset: {e.msg}"
120
+ )