1212from renku_data_services .base_api .auth import authenticate , only_admins , only_authenticated
1313from renku_data_services .base_api .blueprint import BlueprintFactoryResponse , CustomBlueprint
1414from renku_data_services .base_api .misc import validate_query
15+ from renku_data_services .base_models .validation import validated_json
1516from renku_data_services .connected_services import apispec
1617from renku_data_services .connected_services .apispec_base import AuthorizeParams , CallbackParams
1718from renku_data_services .connected_services .db import ConnectedServicesRepository
@@ -30,9 +31,7 @@ def get_all(self) -> BlueprintFactoryResponse:
3031 @authenticate (self .authenticator )
3132 async def _get_all (_ : Request , user : base_models .APIUser ) -> JSONResponse :
3233 clients = await self .connected_services_repo .get_oauth2_clients (user = user )
33- return json (
34- [apispec .Provider .model_validate (c ).model_dump (exclude_none = True , mode = "json" ) for c in clients ]
35- )
34+ return validated_json (apispec .ProviderList , clients )
3635
3736 return "/oauth2/providers" , ["GET" ], _get_all
3837
@@ -42,7 +41,7 @@ def get_one(self) -> BlueprintFactoryResponse:
4241 @authenticate (self .authenticator )
4342 async def _get_one (_ : Request , user : base_models .APIUser , provider_id : str ) -> JSONResponse :
4443 client = await self .connected_services_repo .get_oauth2_client (provider_id = provider_id , user = user )
45- return json (apispec .Provider . model_validate ( client ). model_dump ( exclude_none = True , mode = "json" ) )
44+ return validated_json (apispec .Provider , client )
4645
4746 return "/oauth2/providers/<provider_id>" , ["GET" ], _get_one
4847
@@ -54,7 +53,7 @@ def post(self) -> BlueprintFactoryResponse:
5453 @validate (json = apispec .ProviderPost )
5554 async def _post (_ : Request , user : base_models .APIUser , body : apispec .ProviderPost ) -> JSONResponse :
5655 client = await self .connected_services_repo .insert_oauth2_client (user = user , new_client = body )
57- return json (apispec .Provider . model_validate ( client ). model_dump ( exclude_none = True , mode = "json" ) , 201 )
56+ return validated_json (apispec .Provider , client , 201 )
5857
5958 return "/oauth2/providers" , ["POST" ], _post
6059
@@ -71,7 +70,7 @@ async def _patch(
7170 client = await self .connected_services_repo .update_oauth2_client (
7271 user = user , provider_id = provider_id , ** body_dict
7372 )
74- return json (apispec .Provider . model_validate ( client ). model_dump ( exclude_none = True , mode = "json" ) )
73+ return validated_json (apispec .Provider , client )
7574
7675 return "/oauth2/providers/<provider_id>" , ["PATCH" ], _patch
7776
@@ -143,9 +142,7 @@ def get_all(self) -> BlueprintFactoryResponse:
143142 @authenticate (self .authenticator )
144143 async def _get_all (_ : Request , user : base_models .APIUser ) -> JSONResponse :
145144 connections = await self .connected_services_repo .get_oauth2_connections (user = user )
146- return json (
147- [apispec .Connection .model_validate (c ).model_dump (exclude_none = True , mode = "json" ) for c in connections ]
148- )
145+ return validated_json (apispec .ConnectionList , connections )
149146
150147 return "/oauth2/connections" , ["GET" ], _get_all
151148
@@ -157,7 +154,7 @@ async def _get_one(_: Request, user: base_models.APIUser, connection_id: str) ->
157154 connection = await self .connected_services_repo .get_oauth2_connection (
158155 connection_id = connection_id , user = user
159156 )
160- return json (apispec .Connection . model_validate ( connection ). model_dump ( exclude_none = True , mode = "json" ) )
157+ return validated_json (apispec .Connection , connection )
161158
162159 return "/oauth2/connections/<connection_id>" , ["GET" ], _get_one
163160
@@ -169,7 +166,7 @@ async def _get_account(_: Request, user: base_models.APIUser, connection_id: str
169166 account = await self .connected_services_repo .get_oauth2_connected_account (
170167 connection_id = connection_id , user = user
171168 )
172- return json (apispec .ConnectedAccount . model_validate ( account ). model_dump ( exclude_none = True , mode = "json" ) )
169+ return validated_json (apispec .ConnectedAccount , account )
173170
174171 return "/oauth2/connections/<connection_id>/account" , ["GET" ], _get_account
175172
0 commit comments