1
+ import typing
1
2
from typing import Any , Dict , List , Optional , Union
2
3
3
4
from ..models .record import Record
@@ -17,9 +18,7 @@ def set(
17
18
transaction : Optional [Transaction ] = None ,
18
19
) -> Dict [str , str ]:
19
20
"""Update a record by ID."""
20
- headers = Transaction ._build_transaction_header (
21
- transaction .id if transaction else None
22
- )
21
+ headers = Transaction ._build_transaction_header (transaction )
23
22
return self .client ._make_request (
24
23
"PUT" , f"/api/v1/records/{ record_id } " , data , headers
25
24
)
@@ -31,9 +30,8 @@ def update(
31
30
transaction : Optional [Transaction ] = None ,
32
31
) -> Dict [str , str ]:
33
32
"""Update a record by ID."""
34
- headers = Transaction ._build_transaction_header (
35
- transaction .id if transaction else None
36
- )
33
+ headers = Transaction ._build_transaction_header (transaction )
34
+
37
35
return self .client ._make_request (
38
36
"PATCH" , f"/api/v1/records/{ record_id } " , data , headers
39
37
)
@@ -57,9 +55,7 @@ def create(
57
55
Record object
58
56
:param
59
57
"""
60
- headers = Transaction ._build_transaction_header (
61
- transaction .id if transaction else None
62
- )
58
+ headers = Transaction ._build_transaction_header (transaction )
63
59
64
60
payload = {
65
61
"label" : label ,
@@ -89,9 +85,7 @@ def create_many(
89
85
Returns:
90
86
List of Record objects
91
87
"""
92
- headers = Transaction ._build_transaction_header (
93
- transaction .id if transaction else None
94
- )
88
+ headers = Transaction ._build_transaction_header (transaction )
95
89
96
90
payload = {
97
91
"label" : label ,
@@ -106,39 +100,51 @@ def create_many(
106
100
def attach (
107
101
self ,
108
102
source : Union [str , Dict [str , Any ]],
109
- target : Union [str , List [str ], Dict [str , Any ], List [Dict [str , Any ]]],
103
+ target : Union [
104
+ str ,
105
+ List [str ],
106
+ Dict [str , Any ],
107
+ List [Dict [str , Any ]],
108
+ "Record" ,
109
+ List ["Record" ],
110
+ ],
110
111
options : Optional [RelationshipOptions ] = None ,
111
112
transaction : Optional [Transaction ] = None ,
112
113
) -> Dict [str , str ]:
113
114
"""Attach records to a source record."""
114
- headers = Transaction ._build_transaction_header (
115
- transaction .id if transaction else None
116
- )
115
+ headers = Transaction ._build_transaction_header (transaction )
116
+
117
117
source_id = self ._extract_target_ids (source )[0 ]
118
118
target_ids = self ._extract_target_ids (target )
119
119
payload = {"targetIds" : target_ids }
120
120
if options :
121
- payload .update (options )
121
+ payload .update (typing . cast ( typing . Dict [ str , typing . Any ], options ) )
122
122
return self .client ._make_request (
123
123
"POST" , f"/api/v1/records/{ source_id } /relations" , payload , headers
124
124
)
125
125
126
126
def detach (
127
127
self ,
128
128
source : Union [str , Dict [str , Any ]],
129
- target : Union [str , List [str ], Dict [str , Any ], List [Dict [str , Any ]]],
129
+ target : Union [
130
+ str ,
131
+ List [str ],
132
+ Dict [str , Any ],
133
+ List [Dict [str , Any ]],
134
+ "Record" ,
135
+ List ["Record" ],
136
+ ],
130
137
options : Optional [RelationshipDetachOptions ] = None ,
131
138
transaction : Optional [Transaction ] = None ,
132
139
) -> Dict [str , str ]:
133
140
"""Detach records from a source record."""
134
- headers = Transaction ._build_transaction_header (
135
- transaction .id if transaction else None
136
- )
141
+ headers = Transaction ._build_transaction_header (transaction )
142
+
137
143
source_id = self ._extract_target_ids (source )[0 ]
138
144
target_ids = self ._extract_target_ids (target )
139
145
payload = {"targetIds" : target_ids }
140
146
if options :
141
- payload .update (options )
147
+ payload .update (typing . cast ( typing . Dict [ str , typing . Any ], options ) )
142
148
return self .client ._make_request (
143
149
"PUT" , f"/api/v1/records/{ source_id } /relations" , payload , headers
144
150
)
@@ -147,11 +153,13 @@ def delete(
147
153
self , query : SearchQuery , transaction : Optional [Transaction ] = None
148
154
) -> Dict [str , str ]:
149
155
"""Delete records matching the query."""
150
- headers = Transaction ._build_transaction_header (
151
- transaction .id if transaction else None
152
- )
156
+ headers = Transaction ._build_transaction_header (transaction )
157
+
153
158
return self .client ._make_request (
154
- "PUT" , "/api/v1/records/delete" , query , headers
159
+ "PUT" ,
160
+ "/api/v1/records/delete" ,
161
+ typing .cast (typing .Dict [str , typing .Any ], query or {}),
162
+ headers ,
155
163
)
156
164
157
165
def delete_by_id (
@@ -160,9 +168,8 @@ def delete_by_id(
160
168
transaction : Optional [Transaction ] = None ,
161
169
) -> Dict [str , str ]:
162
170
"""Delete records by ID(s)."""
163
- headers = Transaction ._build_transaction_header (
164
- transaction .id if transaction else None
165
- )
171
+ headers = Transaction ._build_transaction_header (transaction )
172
+
166
173
if isinstance (id_or_ids , list ):
167
174
return self .client ._make_request (
168
175
"PUT" ,
@@ -183,16 +190,18 @@ def find(
183
190
"""Find records matching the query."""
184
191
185
192
try :
186
- headers = Transaction ._build_transaction_header (
187
- transaction .id if transaction else None
188
- )
193
+ headers = Transaction ._build_transaction_header (transaction )
194
+
189
195
path = (
190
196
f"/api/v1/records/{ record_id } /search"
191
197
if record_id
192
198
else "/api/v1/records/search"
193
199
)
194
200
response = self .client ._make_request (
195
- "POST" , path , data = query or {}, headers = headers
201
+ "POST" ,
202
+ path ,
203
+ data = typing .cast (typing .Dict [str , typing .Any ], query or {}),
204
+ headers = headers ,
196
205
)
197
206
return [Record (self .client , record ) for record in response .get ("data" )]
198
207
except Exception :
@@ -206,9 +215,7 @@ def import_csv(
206
215
transaction : Optional [Transaction ] = None ,
207
216
) -> List [Dict [str , Any ]]:
208
217
"""Import data from CSV."""
209
- headers = Transaction ._build_transaction_header (
210
- transaction .id if transaction else None
211
- )
218
+ headers = Transaction ._build_transaction_header (transaction )
212
219
213
220
payload = {
214
221
"label" : label ,
@@ -222,15 +229,20 @@ def import_csv(
222
229
223
230
@staticmethod
224
231
def _extract_target_ids (
225
- target : Union [str , List [str ], Dict [str , Any ], List [Dict [str , Any ]]]
232
+ target : Union [
233
+ str ,
234
+ List [str ],
235
+ Dict [str , Any ],
236
+ List [Dict [str , Any ]],
237
+ "Record" ,
238
+ List ["Record" ],
239
+ ]
226
240
) -> List [str ]:
227
241
"""Extract target IDs from various input types."""
228
242
if isinstance (target , str ):
229
243
return [target ]
230
244
elif isinstance (target , list ):
231
- return [
232
- t ["__id" ] if isinstance (t , dict ) and "__id" in t else t for t in target
233
- ]
245
+ return [t .get ("__id" , "" ) if isinstance (t , dict ) else "" for t in target ]
234
246
elif isinstance (target , Record ) and "__id" in target .data :
235
247
return [target .data ["__id" ]]
236
248
elif isinstance (target , dict ) and "__id" in target :
0 commit comments