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
910from syncmaster .db .models import Connection , Transfer , User
@@ -80,13 +81,16 @@ async def read_connections(
8081 if pagination .items :
8182 credentials = await unit_of_work .credentials .read_bulk ([item .id for item in pagination .items ])
8283 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 ,
84+ TypeAdapter (ReadConnectionSchema ).validate_python (
85+ {
86+ "id" : item .id ,
87+ "group_id" : item .group_id ,
88+ "name" : item .name ,
89+ "description" : item .description ,
90+ "type" : item .type ,
91+ "data" : item .data ,
92+ "auth_data" : credentials .get (item .id , None ),
93+ },
9094 )
9195 for item in pagination .items
9296 ]
@@ -126,6 +130,7 @@ async def create_connection(
126130 async with unit_of_work :
127131 connection = await unit_of_work .connection .create (
128132 name = connection_data .name ,
133+ type = connection_data .type ,
129134 description = connection_data .description ,
130135 group_id = connection_data .group_id ,
131136 data = connection_data .data .dict (),
@@ -137,13 +142,16 @@ async def create_connection(
137142 )
138143
139144 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 ,
145+ return TypeAdapter (ReadConnectionSchema ).validate_python (
146+ {
147+ "id" : connection .id ,
148+ "group_id" : connection .group_id ,
149+ "name" : connection .name ,
150+ "description" : connection .description ,
151+ "type" : connection .type ,
152+ "data" : connection .data ,
153+ "auth_data" : credentials ,
154+ },
147155 )
148156
149157
@@ -172,13 +180,16 @@ async def read_connection(
172180 except AuthDataNotFoundError :
173181 credentials = None
174182
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 ,
183+ return TypeAdapter (ReadConnectionSchema ).validate_python (
184+ {
185+ "id" : connection .id ,
186+ "group_id" : connection .group_id ,
187+ "name" : connection .name ,
188+ "description" : connection .description ,
189+ "type" : connection .type ,
190+ "data" : connection .data ,
191+ "auth_data" : credentials ,
192+ },
182193 )
183194
184195
@@ -202,15 +213,15 @@ async def update_connection(
202213
203214 async with unit_of_work :
204215 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
216+ source_connection : Connection = await unit_of_work .connection .read_by_id (connection_id = connection_id )
217+ if changes .type != source_connection .type :
218+ linked_transfers : Sequence [Transfer ] = await unit_of_work .transfer .list_by_connection_id (connection_id )
219+ if linked_transfers :
220+ raise ConnectionTypeUpdateError
211221 connection = await unit_of_work .connection .update (
212222 connection_id = connection_id ,
213223 name = changes .name ,
224+ type = changes .type ,
214225 description = changes .description ,
215226 data = data ,
216227 )
@@ -222,13 +233,16 @@ async def update_connection(
222233 )
223234
224235 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 ,
236+ return TypeAdapter (ReadConnectionSchema ).validate_python (
237+ {
238+ "id" : connection .id ,
239+ "group_id" : connection .group_id ,
240+ "name" : connection .name ,
241+ "description" : connection .description ,
242+ "type" : connection .type ,
243+ "data" : connection .data ,
244+ "auth_data" : credentials ,
245+ },
232246 )
233247
234248
0 commit comments