Skip to content

Commit a62431e

Browse files
committed
Cleanups
1 parent 913b43e commit a62431e

File tree

1 file changed

+27
-73
lines changed

1 file changed

+27
-73
lines changed

rushdb/client/client.py

Lines changed: 27 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,12 @@
1-
"""RushDB Client
2-
3-
A modern graph database client implementation.
4-
"""
1+
"""RushDB Client"""
52

63
import json
74
import urllib.request
85
import urllib.parse
96
import urllib.error
10-
from typing import Any, Dict, List, Optional, Union, TypeVar, TypedDict, Literal, Protocol, cast
7+
from typing import Any, Dict, List, Optional, Union, TypedDict, Literal
118
from datetime import datetime
129

13-
# Basic type aliases
14-
Schema = TypeVar('Schema', bound=Dict[str, Any])
15-
FlatObject = Dict[str, Any]
16-
MaybeArray = Union[List[Any], Any]
17-
18-
# Record types
19-
class DBRecordInternalProps(TypedDict):
20-
"""Internal properties of a database record."""
21-
__id: str
22-
__label: str
23-
__proptypes: Optional[Dict[str, str]]
24-
25-
class DBRecord(TypedDict, total=False):
26-
"""Database record with internal and user-defined properties."""
27-
__id: str
28-
__label: str
29-
__proptypes: Optional[Dict[str, str]]
30-
31-
class DBRecordDraft:
32-
"""Draft for creating a new record."""
33-
def __init__(self, label: str, payload: Union[Dict[str, Any], List[Dict[str, Any]]], options: Optional[Dict[str, bool]] = None):
34-
self.label = label
35-
self.payload = payload
36-
self.options = options or {
37-
"returnResult": True,
38-
"suggestTypes": True
39-
}
40-
41-
def to_json(self) -> Dict[str, Any]:
42-
"""Convert draft to JSON format."""
43-
return {
44-
"label": self.label,
45-
"options": self.options,
46-
"payload": self.payload
47-
}
48-
49-
class DBRecordsBatchDraft:
50-
"""Draft for creating multiple records in batch."""
51-
def __init__(self, label: str, payload: Union[Dict[str, Any], List[Dict[str, Any]]], options: Optional[Dict[str, bool]] = None):
52-
self.label = label
53-
self.payload = payload
54-
self.options = options or {
55-
"returnResult": True,
56-
"suggestTypes": True
57-
}
58-
59-
def to_json(self) -> Dict[str, Any]:
60-
"""Convert batch draft to JSON format."""
61-
return {
62-
"label": self.label,
63-
"options": self.options,
64-
"payload": self.payload
65-
}
66-
6710
# Relation types
6811
RelationDirection = Literal['in', 'out']
6912

@@ -265,25 +208,41 @@ def create(self, label: str, data: Dict[str, Any], options: Optional[Dict[str, b
265208
:param
266209
"""
267210
headers = self._build_transaction_header(transaction.id if transaction else None)
268-
draft = DBRecordDraft(label, data, options)
269-
response = self.client._make_request('POST', '/api/v1/records', draft.to_json(), headers)
211+
212+
payload = {
213+
"label": label,
214+
"payload": data,
215+
"options": options or {
216+
"returnResult": True,
217+
"suggestTypes": True
218+
}
219+
}
220+
response = self.client._make_request('POST', '/api/v1/records', payload, headers)
270221
return Record(self.client, response)
271222

272-
def create_many(self, label: str, data: List[Dict[str, Any]], options: Optional[Dict[str, bool]] = None, transaction: Optional[Transaction] = None) -> List[Record]:
223+
def create_many(self, label: str, data: Union[Dict[str, Any], List[Dict[str, Any]]], options: Optional[Dict[str, bool]] = None, transaction: Optional[Transaction] = None) -> List[Record]:
273224
"""Create multiple records.
274225
275226
Args:
276227
label: Label for all records
277-
data: List of record data
228+
data: List or Dict of record data
278229
options: Optional parsing and response options (returnResult, suggestTypes)
279230
transaction: Optional transaction object
280231
281232
Returns:
282233
List of Record objects
283234
"""
284235
headers = self._build_transaction_header(transaction.id if transaction else None)
285-
draft = DBRecordsBatchDraft(label, data, options)
286-
response = self.client._make_request('POST', '/api/v1/records/import/json', draft.to_json(), headers)
236+
237+
payload = {
238+
"label": label,
239+
"payload": data,
240+
"options": options or {
241+
"returnResult": True,
242+
"suggestTypes": True
243+
}
244+
}
245+
response = self.client._make_request('POST', '/api/v1/records/import/json', payload, headers)
287246

288247
print('r:', response)
289248

@@ -358,8 +317,6 @@ def find_unique(self, query: Dict[str, Any], transaction: Optional[Transaction]
358317
def import_csv(self, label: str, csv_data: Union[str, bytes], options: Optional[Dict[str, bool]] = None, transaction: Optional[Transaction] = None) -> List[Dict[str, Any]]:
359318
"""Import data from CSV."""
360319
headers = self._build_transaction_header(transaction.id if transaction else None)
361-
# if isinstance(csv_data, str):
362-
# csv_data = csv_data.encode('utf-8')
363320

364321
payload = {
365322
"label": label,
@@ -370,12 +327,7 @@ def import_csv(self, label: str, csv_data: Union[str, bytes], options: Optional[
370327
}
371328
}
372329

373-
return self.client._make_request(
374-
'POST',
375-
'/api/v1/records/import/csv',
376-
payload,
377-
headers,
378-
)
330+
return self.client._make_request('POST','/api/v1/records/import/csv', payload, headers)
379331

380332
@staticmethod
381333
def _build_transaction_header(transaction_id: Optional[str] = None) -> Optional[Dict[str, str]]:
@@ -391,6 +343,8 @@ def _extract_target_ids(target: Union[str, List[str], Dict[str, Any], List[Dict[
391343
return [t['__id'] if isinstance(t, dict) and '__id' in t else t for t in target]
392344
elif isinstance(target, Record) and '__id' in target.data:
393345
return [target.data['__id']]
346+
elif isinstance(target, dict) and '__id' in target:
347+
return [target['__id']]
394348
raise ValueError("Invalid target format")
395349

396350
class PropertyAPI:

0 commit comments

Comments
 (0)