1111import renku_data_services .base_models as base_models
1212from renku_data_services .base_api .auth import authenticate , only_admins , only_authenticated
1313from renku_data_services .base_api .blueprint import BlueprintFactoryResponse , CustomBlueprint
14+ from renku_data_services .base_models .validation import validated_json
1415from renku_data_services .connected_services import apispec
1516from renku_data_services .connected_services .apispec_base import AuthorizeParams , CallbackParams
1617from renku_data_services .connected_services .db import ConnectedServicesRepository
@@ -29,9 +30,7 @@ def get_all(self) -> BlueprintFactoryResponse:
2930 @authenticate (self .authenticator )
3031 async def _get_all (_ : Request , user : base_models .APIUser ) -> JSONResponse :
3132 clients = await self .connected_services_repo .get_oauth2_clients (user = user )
32- return json (
33- [apispec .Provider .model_validate (c ).model_dump (exclude_none = True , mode = "json" ) for c in clients ]
34- )
33+ return validated_json (apispec .ProviderList , clients )
3534
3635 return "/oauth2/providers" , ["GET" ], _get_all
3736
@@ -41,7 +40,7 @@ def get_one(self) -> BlueprintFactoryResponse:
4140 @authenticate (self .authenticator )
4241 async def _get_one (_ : Request , user : base_models .APIUser , provider_id : str ) -> JSONResponse :
4342 client = await self .connected_services_repo .get_oauth2_client (provider_id = provider_id , user = user )
44- return json (apispec .Provider . model_validate ( client ). model_dump ( exclude_none = True , mode = "json" ) )
43+ return validated_json (apispec .Provider , client )
4544
4645 return "/oauth2/providers/<provider_id>" , ["GET" ], _get_one
4746
@@ -53,7 +52,7 @@ def post(self) -> BlueprintFactoryResponse:
5352 @validate (json = apispec .ProviderPost )
5453 async def _post (_ : Request , user : base_models .APIUser , body : apispec .ProviderPost ) -> JSONResponse :
5554 client = await self .connected_services_repo .insert_oauth2_client (user = user , new_client = body )
56- return json (apispec .Provider . model_validate ( client ). model_dump ( exclude_none = True , mode = "json" ) , 201 )
55+ return validated_json (apispec .Provider , client , 201 )
5756
5857 return "/oauth2/providers" , ["POST" ], _post
5958
@@ -70,7 +69,7 @@ async def _patch(
7069 client = await self .connected_services_repo .update_oauth2_client (
7170 user = user , provider_id = provider_id , ** body_dict
7271 )
73- return json (apispec .Provider . model_validate ( client ). model_dump ( exclude_none = True , mode = "json" ) )
72+ return validated_json (apispec .Provider , client )
7473
7574 return "/oauth2/providers/<provider_id>" , ["PATCH" ], _patch
7675
@@ -142,9 +141,7 @@ def get_all(self) -> BlueprintFactoryResponse:
142141 @authenticate (self .authenticator )
143142 async def _get_all (_ : Request , user : base_models .APIUser ) -> JSONResponse :
144143 connections = await self .connected_services_repo .get_oauth2_connections (user = user )
145- return json (
146- [apispec .Connection .model_validate (c ).model_dump (exclude_none = True , mode = "json" ) for c in connections ]
147- )
144+ return validated_json (apispec .ConnectionList , connections )
148145
149146 return "/oauth2/connections" , ["GET" ], _get_all
150147
@@ -156,7 +153,7 @@ async def _get_one(_: Request, user: base_models.APIUser, connection_id: str) ->
156153 connection = await self .connected_services_repo .get_oauth2_connection (
157154 connection_id = connection_id , user = user
158155 )
159- return json (apispec .Connection . model_validate ( connection ). model_dump ( exclude_none = True , mode = "json" ) )
156+ return validated_json (apispec .Connection , connection )
160157
161158 return "/oauth2/connections/<connection_id>" , ["GET" ], _get_one
162159
@@ -168,7 +165,7 @@ async def _get_account(_: Request, user: base_models.APIUser, connection_id: str
168165 account = await self .connected_services_repo .get_oauth2_connected_account (
169166 connection_id = connection_id , user = user
170167 )
171- return json (apispec .ConnectedAccount . model_validate ( account ). model_dump ( exclude_none = True , mode = "json" ) )
168+ return validated_json (apispec .ConnectedAccount , account )
172169
173170 return "/oauth2/connections/<connection_id>/account" , ["GET" ], _get_account
174171
0 commit comments