44from bson import ObjectId
55from bson .errors import InvalidId
66from fastapi import HTTPException , status
7- from pymongo import ReturnDocument
87from pymongo .errors import PyMongoError
98
10- from app .utils .collection_name import (
11- API_CLIENT_COLLECTIONS ,
12- API_CLIENT_ENVIRONMENTS ,
13- API_CLIENT_HISTORY ,
14- )
159from app .api .routes .api_client .schema import (
10+ HISTORY_MAX_ITEMS ,
1611 ApiClientCollectionCreate ,
1712 ApiClientCollectionOut ,
1813 ApiClientCollectionUpdate ,
2116 ApiClientEnvironmentUpdate ,
2217 ApiClientHistoryCreate ,
2318 ApiClientHistoryOut ,
24- HISTORY_MAX_ITEMS ,
2519)
26- from app .core .cache import cached , bump_version
20+ from app .core .cache import bump_version , cached
2721from app .database import db_manager
22+ from app .utils .collection_name import (
23+ API_CLIENT_COLLECTIONS ,
24+ API_CLIENT_ENVIRONMENTS ,
25+ API_CLIENT_HISTORY ,
26+ )
27+ from app .utils .crud import safe_delete_one , safe_insert , safe_update_one
2828
2929HISTORY_TRIM_BATCH_SIZE = 500
3030
@@ -70,13 +70,7 @@ async def list_collections(*, uid: str) -> list[ApiClientCollectionOut]:
7070
7171async def create_collection (uid : str , body : ApiClientCollectionCreate ) -> ApiClientCollectionOut :
7272 doc : dict [str , Any ] = {"created_by" : uid , "name" : body .name , "items" : []}
73- try :
74- result = await db_manager .insert_one (API_CLIENT_COLLECTIONS , doc )
75- except PyMongoError as exc :
76- raise HTTPException (
77- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "Failed to create collection."
78- ) from exc
79- doc ["_id" ] = result .inserted_id
73+ await safe_insert (API_CLIENT_COLLECTIONS , doc , name = "Collection" )
8074 await bump_version (ns = "api_client" , uid = uid )
8175 return _collection_to_out (doc )
8276
@@ -89,28 +83,16 @@ async def patch_collection(uid: str, collection_id: str, body: ApiClientCollecti
8983 if not doc :
9084 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "Collection not found." )
9185 return _collection_to_out (doc )
92- try :
93- doc = await db_manager .find_one_and_update (
94- API_CLIENT_COLLECTIONS ,
95- {"_id" : oid , "created_by" : uid },
96- {"$set" : patch },
97- return_document = ReturnDocument .AFTER ,
98- )
99- except PyMongoError as exc :
100- raise HTTPException (
101- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "Failed to update collection."
102- ) from exc
103- if not doc :
104- raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "Collection not found." )
86+ doc = await safe_update_one (
87+ API_CLIENT_COLLECTIONS , {"_id" : oid , "created_by" : uid }, patch , name = "Collection"
88+ )
10589 await bump_version (ns = "api_client" , uid = uid )
10690 return _collection_to_out (doc )
10791
10892
10993async def delete_collection (uid : str , collection_id : str ) -> None :
11094 oid = _parse_oid (collection_id , kind = "collection" )
111- result = await db_manager .delete_one (API_CLIENT_COLLECTIONS , {"_id" : oid , "created_by" : uid })
112- if result .deleted_count == 0 :
113- raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "Collection not found." )
95+ await safe_delete_one (API_CLIENT_COLLECTIONS , {"_id" : oid , "created_by" : uid }, name = "Collection" )
11496 await bump_version (ns = "api_client" , uid = uid )
11597
11698
@@ -127,13 +109,7 @@ async def list_environments(*, uid: str) -> list[ApiClientEnvironmentOut]:
127109
128110async def create_environment (uid : str , body : ApiClientEnvironmentCreate ) -> ApiClientEnvironmentOut :
129111 doc : dict [str , Any ] = {"created_by" : uid , "name" : body .name , "variables" : []}
130- try :
131- result = await db_manager .insert_one (API_CLIENT_ENVIRONMENTS , doc )
132- except PyMongoError as exc :
133- raise HTTPException (
134- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "Failed to create environment."
135- ) from exc
136- doc ["_id" ] = result .inserted_id
112+ await safe_insert (API_CLIENT_ENVIRONMENTS , doc , name = "Environment" )
137113 await bump_version (ns = "api_client" , uid = uid )
138114 return _env_to_out (doc )
139115
@@ -146,28 +122,16 @@ async def patch_environment(uid: str, environment_id: str, body: ApiClientEnviro
146122 if not doc :
147123 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "Environment not found." )
148124 return _env_to_out (doc )
149- try :
150- doc = await db_manager .find_one_and_update (
151- API_CLIENT_ENVIRONMENTS ,
152- {"_id" : oid , "created_by" : uid },
153- {"$set" : patch },
154- return_document = ReturnDocument .AFTER ,
155- )
156- except PyMongoError as exc :
157- raise HTTPException (
158- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "Failed to update environment."
159- ) from exc
160- if not doc :
161- raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "Environment not found." )
125+ doc = await safe_update_one (
126+ API_CLIENT_ENVIRONMENTS , {"_id" : oid , "created_by" : uid }, patch , name = "Environment"
127+ )
162128 await bump_version (ns = "api_client" , uid = uid )
163129 return _env_to_out (doc )
164130
165131
166132async def delete_environment (uid : str , environment_id : str ) -> None :
167133 oid = _parse_oid (environment_id , kind = "environment" )
168- result = await db_manager .delete_one (API_CLIENT_ENVIRONMENTS , {"_id" : oid , "created_by" : uid })
169- if result .deleted_count == 0 :
170- raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "Environment not found." )
134+ await safe_delete_one (API_CLIENT_ENVIRONMENTS , {"_id" : oid , "created_by" : uid }, name = "Environment" )
171135 await bump_version (ns = "api_client" , uid = uid )
172136
173137
@@ -228,22 +192,14 @@ async def create_history(uid: str, body: ApiClientHistoryCreate) -> ApiClientHis
228192 "timestamp" : ts ,
229193 "status" : body .status ,
230194 }
231- try :
232- result = await db_manager .insert_one (API_CLIENT_HISTORY , doc )
233- except PyMongoError as exc :
234- raise HTTPException (
235- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "Failed to save history entry."
236- ) from exc
237- doc ["_id" ] = result .inserted_id
195+ await safe_insert (API_CLIENT_HISTORY , doc , name = "History entry" )
238196 await bump_version (ns = "api_client" , uid = uid )
239197 return _history_doc_to_out (doc )
240198
241199
242200async def delete_history_entry (uid : str , entry_id : str ) -> None :
243201 oid = _parse_oid (entry_id , kind = "history" )
244- result = await db_manager .delete_one (API_CLIENT_HISTORY , {"_id" : oid , "created_by" : uid })
245- if result .deleted_count == 0 :
246- raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "History entry not found." )
202+ await safe_delete_one (API_CLIENT_HISTORY , {"_id" : oid , "created_by" : uid }, name = "History entry" )
247203 await bump_version (ns = "api_client" , uid = uid )
248204
249205
0 commit comments