Skip to content

Commit d8df99b

Browse files
Merge pull request #5 from microsoft/revert-4-user/jeffand/prefix
Revert "Simplify batch create scenarios"
2 parents 84ae218 + 97c24e6 commit d8df99b

File tree

4 files changed

+73
-300
lines changed

4 files changed

+73
-300
lines changed

examples/quickstart.py

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@ def backoff_retry(op, *, delays=(0, 2, 5, 10, 20), retry_http_statuses=(400, 403
5858
table_info = None
5959
created_this_run = False
6060

61-
# Check for existing table using list_tables
62-
log_call("client.list_tables()")
63-
tables = client.list_tables()
64-
existing_table = next((t for t in tables if t.get("SchemaName") == "new_SampleItem"), None)
65-
if existing_table:
66-
table_info = client.get_table_info("new_SampleItem")
61+
# First check for existing table
62+
log_call("client.get_table_info('SampleItem')")
63+
existing = client.get_table_info("SampleItem")
64+
if existing:
65+
table_info = existing
6766
created_this_run = False
6867
print({
6968
"table": table_info.get("entity_schema"),
@@ -72,12 +71,13 @@ def backoff_retry(op, *, delays=(0, 2, 5, 10, 20), retry_http_statuses=(400, 403
7271
"logical": table_info.get("entity_logical_name"),
7372
"metadata_id": table_info.get("metadata_id"),
7473
})
74+
7575
else:
7676
# Create it since it doesn't exist
7777
try:
7878
log_call("client.create_table('SampleItem', schema={code,count,amount,when,active})")
7979
table_info = client.create_table(
80-
"new_SampleItem",
80+
"SampleItem",
8181
{
8282
"code": "string",
8383
"count": "int",
@@ -139,8 +139,12 @@ def print_line_summaries(label: str, summaries: list[dict]) -> None:
139139

140140
# 2) Create a record in the new table
141141
print("Create records (OData):")
142-
143-
# Prepare payloads
142+
# Show planned creates before executing
143+
for _ in range(3):
144+
plan_call(f"client.create('{entity_set}', payload)")
145+
pause("Execute Create")
146+
record_ids: list[str] = []
147+
created_recs: list[dict] = []
144148
create_payloads = [
145149
{
146150
f"{attr_prefix}_name": "Sample A",
@@ -168,41 +172,14 @@ def print_line_summaries(label: str, summaries: list[dict]) -> None:
168172
},
169173
]
170174

171-
# Show planned creates before executing
172-
plan_call(f"client.create('{entity_set}', single_payload)")
173-
plan_call(f"client.create('{entity_set}', [payload, ...])")
174-
pause("Execute Create")
175-
record_ids: list[str] = []
176-
created_recs: list[dict] = []
177-
178175
try:
179-
# Create the first record
180-
single_payload = create_payloads[0]
181-
log_call(f"client.create('{entity_set}', single_payload)")
182-
rec = backoff_retry(lambda: client.create(entity_set, single_payload))
183-
created_recs.append(rec)
184-
rid = rec.get(id_key)
185-
if rid:
186-
record_ids.append(rid)
187-
188-
# Create the remaining records in a single batch call
189-
batch_payloads = create_payloads[1:]
190-
if batch_payloads:
191-
log_call(f"client.create('{entity_set}', batch_payloads)")
192-
batch_recs = backoff_retry(lambda: client.create(entity_set, batch_payloads))
193-
# If the batch call returns a list, extend; else, append
194-
if isinstance(batch_recs, list):
195-
created_recs.extend(batch_recs)
196-
for rec in batch_recs:
197-
rid = rec.get(id_key)
198-
if rid:
199-
record_ids.append(rid)
200-
else:
201-
created_recs.append(batch_recs)
202-
rid = batch_recs.get(id_key)
203-
if rid:
204-
record_ids.append(rid)
205-
176+
for payload in create_payloads:
177+
log_call(f"client.create('{entity_set}', payload)")
178+
rec = backoff_retry(lambda p=payload: client.create(entity_set, p))
179+
created_recs.append(rec)
180+
rid = rec.get(id_key)
181+
if rid:
182+
record_ids.append(rid)
206183
print({"entity": logical, "created_ids": record_ids})
207184
# Summarize the created records from the returned payloads
208185
summaries = []
@@ -344,11 +321,11 @@ def _retry_if(ex: Exception) -> bool:
344321
print("Cleanup (Metadata):")
345322
try:
346323
# Delete if present, regardless of whether it was created in this run
347-
log_call("client.get_table_info('new_SampleItem')")
348-
info = client.get_table_info("new_SampleItem")
324+
log_call("client.get_table_info('SampleItem')")
325+
info = client.get_table_info("SampleItem")
349326
if info:
350-
log_call("client.delete_table('new_SampleItem')")
351-
client.delete_table("new_SampleItem")
327+
log_call("client.delete_table('SampleItem')")
328+
client.delete_table("SampleItem")
352329
print({"table_deleted": True})
353330
else:
354331
print({"table_deleted": False, "reason": "not found"})

src/dataverse_sdk/client.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, Dict, Optional, List, Union
3+
from typing import Any, Dict, Optional
44

55
from azure.core.credentials import TokenCredential
66

@@ -63,29 +63,25 @@ 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]], Any]) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
67-
"""Create one or more records and return their full representation(s).
66+
def create(self, entity: str, record_data: dict) -> dict:
67+
"""Create a record and return its full representation.
6868
6969
Parameters
7070
----------
7171
entity : str
7272
Entity set name (plural logical name), e.g., ``"accounts"``.
73-
record_data : dict, list of dict, or pandas.DataFrame
74-
Single record (dict), list of records, or pandas DataFrame with field-value pairs.
73+
record_data : dict
74+
Field-value pairs to set on the new record.
7575
7676
Returns
7777
-------
78-
dict or list of dict
79-
For single record: the created record as returned by the Web API.
80-
For multiple records: list of created records.
81-
Records are created using batch requests with a batch size of 25 for optimal performance.
78+
dict
79+
The created record as returned by the Web API (``Prefer: return=representation``).
8280
8381
Raises
8482
------
8583
requests.exceptions.HTTPError
8684
If the request fails (via ``raise_for_status`` in the underlying client).
87-
TypeError
88-
If record_data is not a supported type (dict, list, or pandas DataFrame).
8985
"""
9086
return self._get_odata().create(entity, record_data)
9187

@@ -200,15 +196,6 @@ def delete_table(self, tablename: str) -> None:
200196
"""
201197
self._get_odata().delete_table(tablename)
202198

203-
def list_tables(self) -> list[str]:
204-
"""List all custom tables in the Dataverse environment.
205-
206-
Returns
207-
-------
208-
list[str]
209-
A list of table names.
210-
"""
211-
return self._get_odata().list_tables()
212199

213200
__all__ = ["DataverseClient"]
214201

0 commit comments

Comments
 (0)