44from typing import get_args
55
66from fastapi import APIRouter , Depends , Query , status
7+ from pydantic import TypeAdapter
78
89from syncmaster .backend .services import UnitOfWork , get_user
9- from syncmaster .db .models import Connection , Transfer , User
10+ from syncmaster .db .models import Connection , ConnectionType , Transfer , User
1011from syncmaster .db .utils import Permission
1112from syncmaster .errors .registration import get_error_responses
1213from syncmaster .exceptions import ActionNotAllowedError
2627 ORACLE_TYPE ,
2728 POSTGRES_TYPE ,
2829 S3_TYPE ,
29- ConnectionType ,
3030)
3131from syncmaster .schemas .v1 .connections .connection import (
3232 ConnectionCopySchema ,
@@ -80,13 +80,16 @@ async def read_connections(
8080 if pagination .items :
8181 credentials = await unit_of_work .credentials .read_bulk ([item .id for item in pagination .items ])
8282 items = [
83- ReadConnectionSchema (
84- id = item .id ,
85- group_id = item .group_id ,
86- name = item .name ,
87- description = item .description ,
88- auth_data = credentials .get (item .id , None ),
89- data = item .data ,
83+ TypeAdapter (ReadConnectionSchema ).validate_python (
84+ {
85+ "id" : item .id ,
86+ "group_id" : item .group_id ,
87+ "name" : item .name ,
88+ "description" : item .description ,
89+ "type" : item .type ,
90+ "data" : item .data ,
91+ "auth_data" : credentials .get (item .id , None ),
92+ },
9093 )
9194 for item in pagination .items
9295 ]
@@ -126,6 +129,7 @@ async def create_connection(
126129 async with unit_of_work :
127130 connection = await unit_of_work .connection .create (
128131 name = connection_data .name ,
132+ type = connection_data .type ,
129133 description = connection_data .description ,
130134 group_id = connection_data .group_id ,
131135 data = connection_data .data .dict (),
@@ -137,13 +141,16 @@ async def create_connection(
137141 )
138142
139143 credentials = await unit_of_work .credentials .read (connection .id )
140- return ReadConnectionSchema (
141- id = connection .id ,
142- group_id = connection .group_id ,
143- name = connection .name ,
144- description = connection .description ,
145- data = connection .data ,
146- auth_data = credentials ,
144+ return TypeAdapter (ReadConnectionSchema ).validate_python (
145+ {
146+ "id" : connection .id ,
147+ "group_id" : connection .group_id ,
148+ "name" : connection .name ,
149+ "description" : connection .description ,
150+ "type" : connection .type ,
151+ "data" : connection .data ,
152+ "auth_data" : credentials ,
153+ },
147154 )
148155
149156
@@ -172,13 +179,16 @@ async def read_connection(
172179 except AuthDataNotFoundError :
173180 credentials = None
174181
175- return ReadConnectionSchema (
176- id = connection .id ,
177- group_id = connection .group_id ,
178- name = connection .name ,
179- description = connection .description ,
180- data = connection .data ,
181- auth_data = credentials ,
182+ return TypeAdapter (ReadConnectionSchema ).validate_python (
183+ {
184+ "id" : connection .id ,
185+ "group_id" : connection .group_id ,
186+ "name" : connection .name ,
187+ "description" : connection .description ,
188+ "type" : connection .type ,
189+ "data" : connection .data ,
190+ "auth_data" : credentials ,
191+ },
182192 )
183193
184194
@@ -202,15 +212,15 @@ async def update_connection(
202212
203213 async with unit_of_work :
204214 data = changes .data .dict (exclude = {"auth_data" }) if changes .data else {}
205- if data .get ("type" , None ) is not None :
206- source_connection : Connection = await unit_of_work .connection .read_by_id (connection_id = connection_id )
207- if data ["type" ] != source_connection .data ["type" ]:
208- linked_transfers : Sequence [Transfer ] = await unit_of_work .transfer .list_by_connection_id (connection_id )
209- if linked_transfers :
210- raise ConnectionTypeUpdateError
215+ source_connection : Connection = await unit_of_work .connection .read_by_id (connection_id = connection_id )
216+ if changes .type != source_connection .type :
217+ linked_transfers : Sequence [Transfer ] = await unit_of_work .transfer .list_by_connection_id (connection_id )
218+ if linked_transfers :
219+ raise ConnectionTypeUpdateError
211220 connection = await unit_of_work .connection .update (
212221 connection_id = connection_id ,
213222 name = changes .name ,
223+ type = changes .type ,
214224 description = changes .description ,
215225 data = data ,
216226 )
@@ -222,13 +232,16 @@ async def update_connection(
222232 )
223233
224234 credentials = await unit_of_work .credentials .read (connection_id )
225- return ReadConnectionSchema (
226- id = connection .id ,
227- group_id = connection .group_id ,
228- name = connection .name ,
229- description = connection .description ,
230- data = connection .data ,
231- auth_data = credentials ,
235+ return TypeAdapter (ReadConnectionSchema ).validate_python (
236+ {
237+ "id" : connection .id ,
238+ "group_id" : connection .group_id ,
239+ "name" : connection .name ,
240+ "description" : connection .description ,
241+ "type" : connection .type ,
242+ "data" : connection .data ,
243+ "auth_data" : credentials ,
244+ },
232245 )
233246
234247
0 commit comments