Skip to content

Commit 4a205b2

Browse files
committed
Split create() into create() and create_multiple()
1 parent db7ff4a commit 4a205b2

4 files changed

Lines changed: 34 additions & 55 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ for r in rows:
119119

120120
## Bulk create (CreateMultiple)
121121

122-
Pass a list of payloads to `create(entity_set, payloads)` to invoke the collection-bound `Microsoft.Dynamics.CRM.CreateMultiple` action. The method returns a `list[str]` of created record IDs.
122+
Call `create_multiple(entity_set, payloads)` to invoke the collection-bound `Microsoft.Dynamics.CRM.CreateMultiple` action. The method returns a `list[str]` of created record IDs.
123123

124124
```python
125125
# Bulk create accounts (returns list of GUIDs)
@@ -128,7 +128,7 @@ payloads = [
128128
{"name": "Fabrikam"},
129129
{"name": "Northwind"},
130130
]
131-
ids = client.create("accounts", payloads)
131+
ids = client.create_multiple("accounts", payloads)
132132
assert isinstance(ids, list) and all(isinstance(x, str) for x in ids)
133133
print({"created_ids": ids})
134134
```
@@ -268,7 +268,7 @@ client.delete_table("SampleItem") # delete the table
268268

269269
Notes:
270270
- `create/update` return the full record using `Prefer: return=representation`.
271-
- Passing a list of payloads to `create` triggers bulk create and returns `list[str]` of IDs.
271+
- Use `create_multiple` for bulk create; single `create` only accepts a dict payload.
272272
- Use `get_multiple` for paging through result sets; prefer `select` to limit columns.
273273
- For CRUD methods that take a record id, pass the GUID string (36-char hyphenated). Parentheses around the GUID are accepted but not required.
274274
* SQL queries are executed directly against entity set endpoints using the `?sql=` parameter. Supported subset only (single SELECT, optional WHERE/TOP/ORDER BY, alias). Unsupported constructs will be rejected by the service.

examples/quickstart.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ def print_line_summaries(label: str, summaries: list[dict]) -> None:
188188
if rid1:
189189
record_ids.append(rid1)
190190

191-
# Multi create (list) now returns list[str] of IDs
192-
log_call(f"client.create('{entity_set}', multi_payloads)")
193-
multi_ids = backoff_retry(lambda: client.create(entity_set, multi_payloads))
191+
# Multi create via create_multiple now returns list[str] of IDs
192+
log_call(f"client.create_multiple('{entity_set}', multi_payloads)")
193+
multi_ids = backoff_retry(lambda: client.create_multiple(entity_set, multi_payloads))
194194
if isinstance(multi_ids, list):
195195
for mid in multi_ids:
196196
if isinstance(mid, str):

src/dataverse_sdk/client.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,19 @@ def _get_odata(self) -> ODataClient:
6363
return self._odata
6464

6565
# CRUD
66-
def create(self, entity: str, record_data: Union[Dict[str, Any], List[Dict[str, Any]]]) -> Union[Dict[str, Any], List[str]]:
67-
"""Create one or more records.
66+
def create(self, entity: str, record_data: Dict[str, Any]) -> Dict[str, Any]:
67+
"""Create a single record.
6868
69-
Behaviour:
70-
- Single: returns the created record (dict) using Prefer: return=representation.
71-
- Multiple: uses bound CreateMultiple action and returns list[str] of created record IDs.
69+
Returns the created record representation (if provided by the service).
70+
"""
71+
return self._get_odata().create(entity, record_data)
7272

73-
Parameters
74-
----------
75-
entity : str
76-
Entity set name (plural logical name), e.g., ``"accounts"``.
77-
record_data : dict | list[dict]
78-
Single record payload or list of records for multi-create.
73+
def create_multiple(self, entity: str, records: List[Dict[str, Any]]) -> List[str]:
74+
"""Create multiple records via the bound CreateMultiple action.
7975
80-
Returns
81-
-------
82-
dict | list[str]
83-
Dict for single create, list of GUID strings for multi-create.
84-
85-
Raises
86-
------
87-
requests.exceptions.HTTPError
88-
If the request fails.
89-
TypeError
90-
If ``record_data`` is not a dict or list of dict.
76+
Returns list of created record GUID strings.
9177
"""
92-
return self._get_odata().create(entity, record_data)
78+
return self._get_odata().create_multiple(entity, records)
9379

9480
def update(self, entity: str, record_id: str, record_data: dict) -> dict:
9581
"""Update a record and return its full representation.

src/dataverse_sdk/odata.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,30 @@ def _request(self, method: str, url: str, **kwargs):
4848
return self._http.request(method, url, **kwargs)
4949

5050
# ----------------------------- CRUD ---------------------------------
51-
def create(self, entity_set: str, data: Union[Dict[str, Any], List[Dict[str, Any]]]) -> Union[Dict[str, Any], List[str]]:
52-
"""Create one or many records.
51+
def create(self, entity_set: str, record: Dict[str, Any]) -> Dict[str, Any]:
52+
"""Create a single record and return its representation.
5353
54-
Parameters
55-
----------
56-
entity_set : str
57-
Entity set (plural logical name), e.g. "accounts".
58-
data : dict | list[dict]
59-
Single entity payload or list of payloads for batch create.
54+
POST /{entity_set} with Prefer: return=representation.
55+
"""
56+
if not isinstance(record, dict):
57+
raise TypeError("record must be a dict for single create")
58+
return self._create_single(entity_set, record)
6059

61-
Behaviour
62-
---------
63-
- Single (dict): POST /{entity_set} with Prefer: return=representation. Returns created record (dict).
64-
- Multiple (list[dict]): POST /{entity_set}/Microsoft.Dynamics.CRM.CreateMultiple. Returns list[str] of created GUIDs.
60+
def create_multiple(self, entity_set: str, records: List[Dict[str, Any]]) -> List[str]:
61+
"""Create multiple records via the bound CreateMultiple action.
6562
66-
Multi-create logical name resolution
67-
------------------------------------
68-
- If any payload omits ``@odata.type`` the client performs a metadata lookup (once per entity set, cached)
69-
to resolve the logical name and stamps ``Microsoft.Dynamics.CRM.<logical>`` into missing payloads.
70-
- If all payloads already include ``@odata.type`` no lookup or modification occurs.
63+
POST /{entity_set}/Microsoft.Dynamics.CRM.CreateMultiple
7164
72-
Returns
73-
-------
74-
dict | list[str]
75-
Created entity (single) or list of created IDs (multi).
65+
Returns list[str] of created GUIDs.
66+
67+
Multi-create logical name resolution:
68+
- If any payload omits @odata.type the client performs metadata lookup (cached per entity set)
69+
to resolve the logical name and injects Microsoft.Dynamics.CRM.<logical>.
70+
- If all payloads include @odata.type no lookup occurs.
7671
"""
77-
if isinstance(data, dict):
78-
return self._create_single(entity_set, data)
79-
if isinstance(data, list):
80-
return self._create_multiple(entity_set, data)
81-
raise TypeError("data must be dict or list[dict]")
72+
if not isinstance(records, list) or not all(isinstance(r, dict) for r in records):
73+
raise TypeError("records must be list[dict] for create_multiple")
74+
return self._create_multiple(entity_set, records)
8275

8376
# --- Internal helpers ---
8477
def _create_single(self, entity_set: str, record: Dict[str, Any]) -> Dict[str, Any]:

0 commit comments

Comments
 (0)