-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathodata.py
More file actions
1351 lines (1211 loc) · 54.6 KB
/
odata.py
File metadata and controls
1351 lines (1211 loc) · 54.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
from __future__ import annotations
from typing import Any, Dict, Optional, List, Union, Iterable
from enum import Enum
import unicodedata
import time
import re
import json
from datetime import datetime, timezone
import importlib.resources as ir
from ..core.http import HttpClient
from .upload import ODataFileUpload
from ..core.errors import *
from ..core import error_codes as ec
from ..__version__ import __version__ as _SDK_VERSION
_USER_AGENT = f"DataverseSvcPythonClient:{_SDK_VERSION}"
_GUID_RE = re.compile(r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")
class ODataClient(ODataFileUpload):
"""Dataverse Web API client: CRUD, SQL-over-API, and table metadata helpers."""
@staticmethod
def _escape_odata_quotes(value: str) -> str:
"""Escape single quotes for OData queries (by doubling them)."""
return value.replace("'", "''")
def __init__(
self,
auth,
base_url: str,
config=None,
) -> None:
self.auth = auth
self.base_url = (base_url or "").rstrip("/")
if not self.base_url:
raise ValueError("base_url is required.")
self.api = f"{self.base_url}/api/data/v9.2"
self.config = config or __import__("PowerPlatform.Dataverse.core.config", fromlist=["DataverseConfig"]).DataverseConfig.from_env()
self._http = HttpClient(
retries=self.config.http_retries,
backoff=self.config.http_backoff,
max_backoff=getattr(self.config, 'http_max_backoff', None),
timeout=self.config.http_timeout,
jitter=getattr(self.config, 'http_jitter', True),
retry_transient_errors=getattr(self.config, 'http_retry_transient_errors', True),
)
# Cache: logical name -> entity set name (plural) resolved from metadata
self._logical_to_entityset_cache: dict[str, str] = {}
# Cache: logical name -> primary id attribute (e.g. accountid)
self._logical_primaryid_cache: dict[str, str] = {}
# Picklist label cache: (logical_name, attribute_logical) -> {'map': {...}, 'ts': epoch_seconds}
self._picklist_label_cache = {}
self._picklist_cache_ttl_seconds = 3600 # 1 hour TTL
def _headers(self) -> Dict[str, str]:
"""Build standard OData headers with bearer auth."""
scope = f"{self.base_url}/.default"
token = self.auth.acquire_token(scope).access_token
return {
"Authorization": f"Bearer {token}",
"Accept": "application/json",
"Content-Type": "application/json",
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"User-Agent": _USER_AGENT,
}
def _merge_headers(self, headers: Optional[Dict[str, str]] = None) -> Dict[str, str]:
base = self._headers()
if not headers:
return base
merged = base.copy()
merged.update(headers)
return merged
def _raw_request(self, method: str, url: str, **kwargs):
return self._http.request(method, url, **kwargs)
def _request(self, method: str, url: str, *, expected: tuple[int, ...] = (200, 201, 202, 204), **kwargs):
headers_in = kwargs.pop("headers", None)
kwargs["headers"] = self._merge_headers(headers_in)
r = self._raw_request(method, url, **kwargs)
if r.status_code in expected:
return r
headers = getattr(r, "headers", {}) or {}
body_excerpt = (getattr(r, "text", "") or "")[:200]
svc_code = None
msg = f"HTTP {r.status_code}"
try:
data = r.json() if getattr(r, "text", None) else {}
if isinstance(data, dict):
inner = data.get("error")
if isinstance(inner, dict):
svc_code = inner.get("code")
imsg = inner.get("message")
if isinstance(imsg, str) and imsg.strip():
msg = imsg.strip()
else:
imsg2 = data.get("message")
if isinstance(imsg2, str) and imsg2.strip():
msg = imsg2.strip()
except Exception:
pass
sc = r.status_code
subcode = ec.http_subcode(sc)
correlation_id = headers.get("x-ms-correlation-request-id") or headers.get("x-ms-correlation-id")
request_id = headers.get("x-ms-client-request-id") or headers.get("request-id") or headers.get("x-ms-request-id")
traceparent = headers.get("traceparent")
ra = headers.get("Retry-After")
retry_after = None
if ra:
try:
retry_after = int(ra)
except Exception:
retry_after = None
is_transient = ec.is_transient_status(sc)
raise HttpError(
msg,
status_code=sc,
subcode=subcode,
service_error_code=svc_code,
correlation_id=correlation_id,
request_id=request_id,
traceparent=traceparent,
body_excerpt=body_excerpt,
retry_after=retry_after,
is_transient=is_transient,
)
# --- CRUD Internal functions ---
def _create(self, entity_set: str, logical_name: str, record: Dict[str, Any]) -> str:
"""Create a single record and return its GUID.
Parameters
-------
entity_set : str
Resolved entity set (plural) name.
logical_name : str
Singular logical entity name.
record : dict[str, Any]
Attribute payload mapped by logical column names.
Returns
-------
str
Created record GUID.
Notes
-------
Relies on OData-EntityId (canonical) or Location header. No response body parsing is performed.
Raises RuntimeError if neither header contains a GUID.
"""
record = self._convert_labels_to_ints(logical_name, record)
url = f"{self.api}/{entity_set}"
r = self._request("post", url, json=record)
ent_loc = r.headers.get("OData-EntityId") or r.headers.get("OData-EntityID")
if ent_loc:
m = _GUID_RE.search(ent_loc)
if m:
return m.group(0)
loc = r.headers.get("Location")
if loc:
m = _GUID_RE.search(loc)
if m:
return m.group(0)
header_keys = ", ".join(sorted(r.headers.keys()))
raise RuntimeError(
f"Create response missing GUID in OData-EntityId/Location headers (status={getattr(r,'status_code', '?')}). Headers: {header_keys}"
)
def _create_multiple(self, entity_set: str, logical_name: str, records: List[Dict[str, Any]]) -> List[str]:
"""Create multiple records using the collection-bound CreateMultiple action.
Parameters
----------
entity_set : str
Resolved entity set (plural) name.
logical_name : str
Singular logical entity name.
records : list[dict[str, Any]]
Payloads mapped by logical attribute names.
Multi-create logical name resolution
------------------------------------
- If any payload omits ``@odata.type`` the client stamps ``Microsoft.Dynamics.CRM.<logical_name>``.
- If all payloads already include ``@odata.type`` no modification occurs.
Returns
-------
list[str]
List of created IDs.
"""
if not all(isinstance(r, dict) for r in records):
raise TypeError("All items for multi-create must be dicts")
need_logical = any("@odata.type" not in r for r in records)
enriched: List[Dict[str, Any]] = []
for r in records:
r = self._convert_labels_to_ints(logical_name, r)
if "@odata.type" in r or not need_logical:
enriched.append(r)
else:
nr = r.copy()
nr["@odata.type"] = f"Microsoft.Dynamics.CRM.{logical_name}"
enriched.append(nr)
payload = {"Targets": enriched}
# Bound action form: POST {entity_set}/Microsoft.Dynamics.CRM.CreateMultiple
url = f"{self.api}/{entity_set}/Microsoft.Dynamics.CRM.CreateMultiple"
# The action currently returns only Ids; no need to request representation.
r = self._request("post", url, json=payload)
try:
body = r.json() if r.text else {}
except ValueError:
body = {}
if not isinstance(body, dict):
return []
# Expected: { "Ids": [guid, ...] }
ids = body.get("Ids")
if isinstance(ids, list):
return [i for i in ids if isinstance(i, str)]
value = body.get("value")
if isinstance(value, list):
# Extract IDs if possible
out: List[str] = []
for item in value:
if isinstance(item, dict):
# Heuristic: look for a property ending with 'id'
for k, v in item.items():
if isinstance(k, str) and k.lower().endswith("id") and isinstance(v, str) and len(v) >= 32:
out.append(v)
break
return out
return []
# --- Derived helpers for high-level client ergonomics ---
def _primary_id_attr(self, logical_name: str) -> str:
"""Return primary key attribute using metadata; error if unavailable."""
pid = self._logical_primaryid_cache.get(logical_name)
if pid:
return pid
# Resolve metadata (populates _logical_primaryid_cache or raises if logical unknown)
self._entity_set_from_logical(logical_name)
pid2 = self._logical_primaryid_cache.get(logical_name)
if pid2:
return pid2
raise RuntimeError(
f"PrimaryIdAttribute not resolved for logical name '{logical_name}'. Metadata did not include PrimaryIdAttribute."
)
def _update_by_ids(self, logical_name: str, ids: List[str], changes: Union[Dict[str, Any], List[Dict[str, Any]]]) -> None:
"""Update many records by GUID list using UpdateMultiple under the hood.
Parameters
----------
logical_name : str
Logical name (singular).
ids : list[str]
GUIDs of target records.
changes : dict | list[dict]
Broadcast patch (dict) applied to all IDs, or list of per-record patches (1:1 with ids).
"""
if not isinstance(ids, list):
raise TypeError("ids must be list[str]")
if not ids:
return None
pk_attr = self._primary_id_attr(logical_name)
entity_set = self._entity_set_from_logical(logical_name)
if isinstance(changes, dict):
batch = [{pk_attr: rid, **changes} for rid in ids]
self._update_multiple(entity_set, logical_name, batch)
return None
if not isinstance(changes, list):
raise TypeError("changes must be dict or list[dict]")
if len(changes) != len(ids):
raise ValueError("Length of changes list must match length of ids list")
batch: List[Dict[str, Any]] = []
for rid, patch in zip(ids, changes):
if not isinstance(patch, dict):
raise TypeError("Each patch must be a dict")
batch.append({pk_attr: rid, **patch})
self._update_multiple(entity_set, logical_name, batch)
return None
def _delete_multiple(
self,
logical_name: str,
ids: List[str],
) -> Optional[str]:
"""Delete many records by GUID list.
Returns the asynchronous job identifier reported by the BulkDelete action.
"""
targets = [rid for rid in ids if rid]
if not targets:
return None
value_objects = [{"Value": rid, "Type": "System.Guid"} for rid in targets]
pk_attr = self._primary_id_attr(logical_name)
timestamp = datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
job_label = f"Bulk delete {logical_name} records @ {timestamp}"
query = {
"@odata.type": "Microsoft.Dynamics.CRM.QueryExpression",
"EntityName": logical_name,
"ColumnSet": {
"@odata.type": "Microsoft.Dynamics.CRM.ColumnSet",
"AllColumns": False,
"Columns": [],
},
"Criteria": {
"@odata.type": "Microsoft.Dynamics.CRM.FilterExpression",
"FilterOperator": "And",
"Conditions": [
{
"@odata.type": "Microsoft.Dynamics.CRM.ConditionExpression",
"AttributeName": pk_attr,
"Operator": "In",
"Values": value_objects,
}
],
},
}
payload = {
"JobName": job_label,
"SendEmailNotification": False,
"ToRecipients": [],
"CCRecipients": [],
"RecurrencePattern": "",
"StartDateTime": timestamp,
"QuerySet": [query],
}
url = f"{self.api}/BulkDelete"
response = self._request("post", url, json=payload, expected=(200, 202, 204))
job_id = None
try:
body = response.json() if response.text else {}
except ValueError:
body = {}
if isinstance(body, dict):
job_id = body.get("JobId")
return job_id
def _format_key(self, key: str) -> str:
k = key.strip()
if k.startswith("(") and k.endswith(")"):
return k
# Escape single quotes in alternate key values
if "=" in k and "'" in k:
def esc(match):
# match.group(1) is the key, match.group(2) is the value
return f"{match.group(1)}='{self._escape_odata_quotes(match.group(2))}'"
k = re.sub(r"(\w+)=\'([^\']*)\'", esc, k)
return f"({k})"
if len(k) == 36 and "-" in k:
return f"({k})"
return f"({k})"
def _update(self, logical_name: str, key: str, data: Dict[str, Any]) -> None:
"""Update an existing record.
Parameters
----------
logical_name : str
Logical (singular) entity name.
key : str
Record GUID (with or without parentheses) or alternate key.
data : dict
Partial entity payload.
Returns
-------
None
"""
data = self._convert_labels_to_ints(logical_name, data)
entity_set = self._entity_set_from_logical(logical_name)
url = f"{self.api}/{entity_set}{self._format_key(key)}"
r = self._request("patch", url, headers={"If-Match": "*"}, json=data)
def _update_multiple(self, entity_set: str, logical_name: str, records: List[Dict[str, Any]]) -> None:
"""Bulk update existing records via the collection-bound UpdateMultiple action.
Parameters
----------
entity_set : str
Resolved entity set name.
logical_name : str
Logical (singular) name, e.g. "account".
records : list[dict]
Each dict must include the real primary key attribute for the entity (e.g. ``accountid``) and one or more
fields to update. If ``@odata.type`` is omitted in any payload, the logical name is resolved once and
stamped into those payloads as ``Microsoft.Dynamics.CRM.<logical>`` (same behaviour as bulk create).
Behaviour
---------
- POST ``/{entity_set}/Microsoft.Dynamics.CRM.UpdateMultiple`` with body ``{"Targets": [...]}``.
- Expects Dataverse transactional semantics: if any individual update fails the entire request is rolled back.
- Response content is ignored; no stable contract for returned IDs or representations.
Returns
-------
None
No representation is returned (symmetry with single update).
Notes
-----
- Caller must include the correct primary key attribute (e.g. ``accountid``) in every record.
- Both single and multiple updates return None.
"""
if not isinstance(records, list) or not records or not all(isinstance(r, dict) for r in records):
raise TypeError("records must be a non-empty list[dict]")
# Determine whether we need logical name resolution (@odata.type missing in any payload)
need_logical = any("@odata.type" not in r for r in records)
enriched: List[Dict[str, Any]] = []
for r in records:
r = self._convert_labels_to_ints(logical_name, r)
if "@odata.type" in r or not need_logical:
enriched.append(r)
else:
nr = r.copy()
nr["@odata.type"] = f"Microsoft.Dynamics.CRM.{logical_name}"
enriched.append(nr)
payload = {"Targets": enriched}
url = f"{self.api}/{entity_set}/Microsoft.Dynamics.CRM.UpdateMultiple"
r = self._request("post", url, json=payload)
# Intentionally ignore response content: no stable contract for IDs across environments.
return None
def _delete(self, logical_name: str, key: str) -> None:
"""Delete a record by GUID or alternate key."""
entity_set = self._entity_set_from_logical(logical_name)
url = f"{self.api}/{entity_set}{self._format_key(key)}"
self._request("delete", url, headers={"If-Match": "*"})
def _get(self, logical_name: str, key: str, select: Optional[str] = None) -> Dict[str, Any]:
"""Retrieve a single record.
Parameters
----------
logical_name : str
Logical (singular) name.
key : str
Record GUID (with or without parentheses) or alternate key syntax.
select : str | None
Comma separated columns for $select.
"""
params = {}
if select:
params["$select"] = select
entity_set = self._entity_set_from_logical(logical_name)
url = f"{self.api}/{entity_set}{self._format_key(key)}"
r = self._request("get", url, params=params)
return r.json()
def _get_multiple(
self,
logical_name: str,
select: Optional[List[str]] = None,
filter: Optional[str] = None,
orderby: Optional[List[str]] = None,
top: Optional[int] = None,
expand: Optional[List[str]] = None,
page_size: Optional[int] = None,
) -> Iterable[List[Dict[str, Any]]]:
"""Iterate records from an entity set, yielding one page (list of dicts) at a time.
Parameters
----------
logical_name : str
Logical (singular) entity name.
select : list[str] | None
Columns to select; joined with commas into $select.
filter : str | None
OData $filter expression as a string.
orderby : list[str] | None
Order expressions; joined with commas into $orderby.
top : int | None
Max number of records across all pages. Passed as $top on the first request; the server will paginate via nextLink as needed.
expand : list[str] | None
Navigation properties to expand; joined with commas into $expand.
page_size : int | None
Hint for per-page size using Prefer: ``odata.maxpagesize``.
Yields
------
list[dict]
A page of records from the Web API (the "value" array for each page).
"""
extra_headers: Dict[str, str] = {}
if page_size is not None:
ps = int(page_size)
if ps > 0:
extra_headers["Prefer"] = f"odata.maxpagesize={ps}"
def _do_request(url: str, *, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
headers = extra_headers if extra_headers else None
r = self._request("get", url, headers=headers, params=params)
try:
return r.json()
except ValueError:
return {}
entity_set = self._entity_set_from_logical(logical_name)
base_url = f"{self.api}/{entity_set}"
params: Dict[str, Any] = {}
if select:
params["$select"] = ",".join(select)
if filter:
params["$filter"] = filter
if orderby:
params["$orderby"] = ",".join(orderby)
if expand:
params["$expand"] = ",".join(expand)
if top is not None:
params["$top"] = int(top)
data = _do_request(base_url, params=params)
items = data.get("value") if isinstance(data, dict) else None
if isinstance(items, list) and items:
yield [x for x in items if isinstance(x, dict)]
next_link = None
if isinstance(data, dict):
next_link = data.get("@odata.nextLink") or data.get("odata.nextLink")
while next_link:
data = _do_request(next_link)
items = data.get("value") if isinstance(data, dict) else None
if isinstance(items, list) and items:
yield [x for x in items if isinstance(x, dict)]
next_link = data.get("@odata.nextLink") or data.get("odata.nextLink") if isinstance(data, dict) else None
# --------------------------- SQL Custom API -------------------------
def _query_sql(self, sql: str) -> list[dict[str, Any]]:
"""Execute a read-only SQL query using the Dataverse Web API `?sql=` capability.
The platform supports a constrained subset of SQL SELECT statements directly on entity set endpoints:
GET /{entity_set}?sql=<encoded select statement>
This client extracts the logical table name from the query, resolves the corresponding
entity set name (cached) and invokes the Web API using the `sql` query parameter.
Parameters
----------
sql : str
Single SELECT statement within supported subset.
Returns
-------
list[dict]
Result rows (empty list if none).
Raises
------
ValueError
If the SQL is empty or malformed, or if the table logical name cannot be determined.
RuntimeError
If metadata lookup for the logical name fails.
"""
if not isinstance(sql, str):
raise ValidationError("sql must be a string", subcode=ec.VALIDATION_SQL_NOT_STRING)
if not sql.strip():
raise ValidationError("sql must be a non-empty string", subcode=ec.VALIDATION_SQL_EMPTY)
sql = sql.strip()
# Extract logical table name via helper (robust to identifiers ending with 'from')
logical = self._extract_logical_table(sql)
entity_set = self._entity_set_from_logical(logical)
# Issue GET /{entity_set}?sql=<query>
url = f"{self.api}/{entity_set}"
params = {"sql": sql}
r = self._request("get", url, params=params)
try:
body = r.json()
except ValueError:
return []
if isinstance(body, dict):
value = body.get("value")
if isinstance(value, list):
# Ensure dict rows only
return [row for row in value if isinstance(row, dict)]
# Fallbacks: if body itself is a list
if isinstance(body, list):
return [row for row in body if isinstance(row, dict)]
return []
@staticmethod
def _extract_logical_table(sql: str) -> str:
"""Extract the logical table name after the first standalone FROM.
Examples:
SELECT * FROM account
SELECT col1, startfrom FROM new_sampleitem WHERE col1 = 1
"""
if not isinstance(sql, str):
raise ValueError("sql must be a string")
# Mask out single-quoted string literals to avoid matching FROM inside them.
masked = re.sub(r"'([^']|'')*'", "'x'", sql)
pattern = r"\bfrom\b\s+([A-Za-z0-9_]+)" # minimal, single-line regex
m = re.search(pattern, masked, flags=re.IGNORECASE)
if not m:
raise ValueError("Unable to determine table logical name from SQL (expected 'FROM <name>').")
return m.group(1).lower()
# ---------------------- Entity set resolution -----------------------
def _entity_set_from_logical(self, logical: str) -> str:
"""Resolve entity set name (plural) from a logical (singular) name using metadata.
Caches results for subsequent SQL queries.
"""
if not logical:
raise ValueError("logical name required")
cached = self._logical_to_entityset_cache.get(logical)
if cached:
return cached
url = f"{self.api}/EntityDefinitions"
logical_escaped = self._escape_odata_quotes(logical)
params = {
"$select": "LogicalName,EntitySetName,PrimaryIdAttribute",
"$filter": f"LogicalName eq '{logical_escaped}'",
}
r = self._request("get", url, params=params)
try:
body = r.json()
items = body.get("value", []) if isinstance(body, dict) else []
except ValueError:
items = []
if not items:
plural_hint = " (did you pass a plural entity set name instead of the singular logical name?)" if logical.endswith("s") and not logical.endswith("ss") else ""
raise MetadataError(
f"Unable to resolve entity set for logical name '{logical}'. Provide the singular logical name.{plural_hint}",
subcode=ec.METADATA_ENTITYSET_NOT_FOUND,
)
md = items[0]
es = md.get("EntitySetName")
if not es:
raise MetadataError(
f"Metadata response missing EntitySetName for logical '{logical}'.",
subcode=ec.METADATA_ENTITYSET_NAME_MISSING,
)
self._logical_to_entityset_cache[logical] = es
primary_id_attr = md.get("PrimaryIdAttribute")
if isinstance(primary_id_attr, str) and primary_id_attr:
self._logical_primaryid_cache[logical] = primary_id_attr
return es
# ---------------------- Table metadata helpers ----------------------
def _label(self, text: str) -> Dict[str, Any]:
lang = int(self.config.language_code)
return {
"@odata.type": "Microsoft.Dynamics.CRM.Label",
"LocalizedLabels": [
{
"@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
"Label": text,
"LanguageCode": lang,
}
],
}
def _to_pascal(self, name: str) -> str:
parts = re.split(r"[^A-Za-z0-9]+", name)
return "".join(p[:1].upper() + p[1:] for p in parts if p)
def _normalize_entity_schema(self, tablename: str) -> str:
if "_" in tablename:
return tablename
return f"new_{self._to_pascal(tablename)}"
def _get_entity_by_schema(
self,
schema_name: str,
headers: Optional[Dict[str, str]] = None,
) -> Optional[Dict[str, Any]]:
url = f"{self.api}/EntityDefinitions"
# Escape single quotes in schema name
schema_escaped = self._escape_odata_quotes(schema_name)
params = {
"$select": "MetadataId,LogicalName,SchemaName,EntitySetName",
"$filter": f"SchemaName eq '{schema_escaped}'",
}
r = self._request("get", url, params=params, headers=headers)
items = r.json().get("value", [])
return items[0] if items else None
def _create_entity(
self,
schema_name: str,
display_name: str,
attributes: List[Dict[str, Any]],
solution_unique_name: Optional[str] = None,
) -> Dict[str, Any]:
url = f"{self.api}/EntityDefinitions"
payload = {
"@odata.type": "Microsoft.Dynamics.CRM.EntityMetadata",
"SchemaName": schema_name,
"DisplayName": self._label(display_name),
"DisplayCollectionName": self._label(display_name + "s"),
"Description": self._label(f"Custom entity for {display_name}"),
"OwnershipType": "UserOwned",
"HasActivities": False,
"HasNotes": True,
"IsActivity": False,
"Attributes": attributes,
}
params = None
if solution_unique_name:
params = {"SolutionUniqueName": solution_unique_name}
self._request("post", url, json=payload, params=params)
ent = self._get_entity_by_schema(
schema_name,
headers={"Consistency": "Strong"},
)
if not ent or not ent.get("EntitySetName"):
raise RuntimeError(
f"Failed to create or retrieve entity '{schema_name}' (EntitySetName not available)."
)
if not ent.get("MetadataId"):
raise RuntimeError(
f"MetadataId missing after creating entity '{schema_name}'."
)
return ent
def _normalize_attribute_schema(self, entity_schema: str, column_name: str) -> str:
# Use same publisher prefix segment as entity_schema if present; else default to 'new_'.
if not isinstance(column_name, str) or not column_name.strip():
raise ValueError("column_name must be a non-empty string")
publisher = entity_schema.split("_", 1)[0] if "_" in entity_schema else "new"
expected_prefix = f"{publisher}_"
if column_name.lower().startswith(expected_prefix.lower()):
return column_name
return f"{publisher}_{self._to_pascal(column_name)}"
def _get_attribute_metadata(
self,
entity_metadata_id: str,
schema_name: str,
extra_select: Optional[str] = None,
) -> Optional[Dict[str, Any]]:
attr_escaped = self._escape_odata_quotes(schema_name)
url = f"{self.api}/EntityDefinitions({entity_metadata_id})/Attributes"
select_fields = ["MetadataId", "LogicalName", "SchemaName"]
if extra_select:
for piece in extra_select.split(","):
piece = piece.strip()
if not piece or piece in select_fields:
continue
if piece.startswith("@"):
continue
if piece not in select_fields:
select_fields.append(piece)
params = {
"$select": ",".join(select_fields),
"$filter": f"SchemaName eq '{attr_escaped}'",
}
r = self._request("get", url, params=params)
try:
body = r.json() if r.text else {}
except ValueError:
return None
items = body.get("value") if isinstance(body, dict) else None
if isinstance(items, list) and items:
item = items[0]
if isinstance(item, dict):
return item
return None
# ---------------------- Enum / Option Set helpers ------------------
def _build_localizedlabels_payload(self, translations: Dict[int, str]) -> Dict[str, Any]:
"""Build a Dataverse Label object from {<language_code>: <text>} entries.
Ensures at least one localized label. Does not deduplicate language codes; last wins.
"""
locs: List[Dict[str, Any]] = []
for lang, text in translations.items():
if not isinstance(lang, int):
raise ValueError(f"Language code '{lang}' must be int")
if not isinstance(text, str) or not text.strip():
raise ValueError(f"Label for lang {lang} must be non-empty string")
locs.append({
"@odata.type": "Microsoft.Dynamics.CRM.LocalizedLabel",
"Label": text,
"LanguageCode": lang,
})
if not locs:
raise ValueError("At least one translation required")
return {
"@odata.type": "Microsoft.Dynamics.CRM.Label",
"LocalizedLabels": locs,
}
def _enum_optionset_payload(self, schema_name: str, enum_cls: type[Enum], is_primary_name: bool = False) -> Dict[str, Any]:
"""Create local (IsGlobal=False) PicklistAttributeMetadata from an Enum subclass.
Supports translation mapping via optional class attribute `__labels__`:
__labels__ = { 1033: { "Active": "Active", "Inactive": "Inactive" },
1036: { "Active": "Actif", "Inactive": "Inactif" } }
Keys inside per-language dict may be either enum member objects or their names.
If a language lacks a label for a member, member.name is used as fallback.
The client's configured language code is always ensured to exist.
"""
all_member_items = list(enum_cls.__members__.items())
if not all_member_items:
raise ValueError(f"Enum {enum_cls.__name__} has no members")
# Duplicate detection
value_to_first_name: Dict[int, str] = {}
for name, member in all_member_items:
val = getattr(member, "value", None)
# Defer non-int validation to later loop for consistency
if val in value_to_first_name and value_to_first_name[val] != name:
raise ValueError(
f"Duplicate enum value {val} in {enum_cls.__name__} (names: {value_to_first_name[val]}, {name})"
)
value_to_first_name[val] = name
members = list(enum_cls)
# Validate integer values
for m in members:
if not isinstance(m.value, int):
raise ValueError(f"Enum member '{m.name}' has non-int value '{m.value}' (only int values supported)")
raw_labels = getattr(enum_cls, "__labels__", None)
labels_by_lang: Dict[int, Dict[str, str]] = {}
if raw_labels is not None:
if not isinstance(raw_labels, dict):
raise ValueError("__labels__ must be a dict {lang:int -> {member: label}}")
# Build a helper map for value -> member name to resolve raw int keys
value_to_name = {m.value: m.name for m in members}
for lang, mapping in raw_labels.items():
if not isinstance(lang, int):
raise ValueError("Language codes in __labels__ must be ints")
if not isinstance(mapping, dict):
raise ValueError(f"__labels__[{lang}] must be a dict of member names to strings")
labels_by_lang.setdefault(lang, {})
for k, v in mapping.items():
# Accept enum member object, its name, or raw int value (from class body reference)
if isinstance(k, enum_cls):
member_name = k.name
elif isinstance(k, int):
member_name = value_to_name.get(k)
if member_name is None:
raise ValueError(f"__labels__[{lang}] has int key {k} not matching any enum value")
else:
member_name = str(k)
if not isinstance(v, str) or not v.strip():
raise ValueError(f"Label for {member_name} lang {lang} must be non-empty string")
labels_by_lang[lang][member_name] = v
config_lang = int(self.config.language_code)
# Ensure config language appears (fallback to names)
all_langs = set(labels_by_lang.keys()) | {config_lang}
options: List[Dict[str, Any]] = []
for m in sorted(members, key=lambda x: x.value):
per_lang: Dict[int, str] = {}
for lang in all_langs:
label_text = labels_by_lang.get(lang, {}).get(m.name, m.name)
per_lang[lang] = label_text
options.append({
"@odata.type": "Microsoft.Dynamics.CRM.OptionMetadata",
"Value": m.value,
"Label": self._build_localizedlabels_payload(per_lang),
})
attr_label = schema_name.split("_")[-1]
return {
"@odata.type": "Microsoft.Dynamics.CRM.PicklistAttributeMetadata",
"SchemaName": schema_name,
"DisplayName": self._label(attr_label),
"RequiredLevel": {"Value": "None"},
"IsPrimaryName": bool(is_primary_name),
"OptionSet": {
"@odata.type": "Microsoft.Dynamics.CRM.OptionSetMetadata",
"IsGlobal": False,
"Options": options,
},
}
def _normalize_picklist_label(self, label: str) -> str:
"""Normalize a label for case / diacritic insensitive comparison."""
if not isinstance(label, str):
return ""
# Strip accents
norm = unicodedata.normalize("NFD", label)
norm = "".join(c for c in norm if unicodedata.category(c) != "Mn")
# Collapse whitespace, lowercase
norm = re.sub(r"\s+", " ", norm).strip().lower()
return norm
def _request_metadata_with_retry(self, method: str, url: str, **kwargs) -> Any:
"""
Make HTTP request with metadata-specific retry logic.
Retries 404 errors for metadata operations since attribute metadata
may not be immediately available after creation or schema changes.
:param method: HTTP method (e.g., 'get', 'post').
:type method: str
:param url: Target URL for the metadata request.
:type url: str
:param kwargs: Additional arguments passed to the underlying request method.
:return: HTTP response object.
:rtype: Any
:raises HttpError: If the request fails after all retry attempts.
"""
last_error = None
for attempt in range(3): # 3 attempts for metadata operations
try:
return self._request(method, url, **kwargs)
except HttpError as err:
last_error = err
if getattr(err, "status_code", None) == 404 and attempt < 2:
# Exponential backoff: 0.4s, 0.8s for metadata propagation delays
delay = 0.4 * (2 ** attempt)
time.sleep(delay)
continue
raise
def _optionset_map(self, logical_name: str, attr_logical: str) -> Optional[Dict[str, int]]:
"""Build or return cached mapping of normalized label -> value for a picklist attribute.
Returns empty dict if attribute is not a picklist or has no options. Returns None only
for invalid inputs or unexpected metadata parse failures.
Notes
-----
- This method calls the Web API twice per attribute so it could have perf impact when there are lots of columns on the entity.
"""
if not logical_name or not attr_logical:
return None
cache_key = (logical_name, attr_logical.lower())
now = time.time()
entry = self._picklist_label_cache.get(cache_key)
if isinstance(entry, dict) and 'map' in entry and (now - entry.get('ts', 0)) < self._picklist_cache_ttl_seconds:
return entry['map']
attr_esc = self._escape_odata_quotes(attr_logical)
logical_esc = self._escape_odata_quotes(logical_name)
# Step 1: lightweight fetch (no expand) to determine attribute type
url_type = (
f"{self.api}/EntityDefinitions(LogicalName='{logical_esc}')/Attributes"
f"?$filter=LogicalName eq '{attr_esc}'&$select=LogicalName,AttributeType"
)
# Retry up to 3 times on 404 (new or not-yet-published attribute metadata). If still 404, raise.
try:
r_type = self._request_metadata_with_retry("get", url_type)
except HttpError as err:
if getattr(err, "status_code", None) == 404:
raise RuntimeError(
f"Picklist attribute metadata not found after retries: entity='{logical_name}' attribute='{attr_logical}' (404)"
) from err
raise
body_type = r_type.json()
items = body_type.get("value", []) if isinstance(body_type, dict) else []
if not items:
return None
attr_md = items[0]
if attr_md.get("AttributeType") not in ("Picklist", "PickList"):
self._picklist_label_cache[cache_key] = {'map': {}, 'ts': now}
return {}
# Step 2: fetch with expand only now that we know it's a picklist
# Need to cast to the derived PicklistAttributeMetadata type; OptionSet is not a nav on base AttributeMetadata.
cast_url = (
f"{self.api}/EntityDefinitions(LogicalName='{logical_esc}')/Attributes(LogicalName='{attr_esc}')/"
"Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$expand=OptionSet($select=Options)"
)
# Step 2 fetch with retries: expanded OptionSet (cast form first)
try:
r_opts = self._request_metadata_with_retry("get", cast_url)
except HttpError as err:
if getattr(err, "status_code", None) == 404:
raise RuntimeError(
f"Picklist OptionSet metadata not found after retries: entity='{logical_name}' attribute='{attr_logical}' (404)"
) from err
raise
attr_full = {}
try: