anaplan-sdk 0.4.5__py3-none-any.whl → 0.5.0a2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- anaplan_sdk/_async_clients/_alm.py +248 -44
- anaplan_sdk/_async_clients/_audit.py +13 -13
- anaplan_sdk/_async_clients/_bulk.py +181 -135
- anaplan_sdk/_async_clients/_cloud_works.py +57 -38
- anaplan_sdk/_async_clients/_cw_flow.py +25 -16
- anaplan_sdk/_async_clients/_transactional.py +251 -53
- anaplan_sdk/_clients/_alm.py +246 -45
- anaplan_sdk/_clients/_audit.py +13 -14
- anaplan_sdk/_clients/_bulk.py +180 -123
- anaplan_sdk/_clients/_cloud_works.py +54 -36
- anaplan_sdk/_clients/_cw_flow.py +25 -16
- anaplan_sdk/_clients/_transactional.py +246 -50
- anaplan_sdk/_services.py +392 -0
- anaplan_sdk/models/__init__.py +49 -2
- anaplan_sdk/models/_alm.py +64 -6
- anaplan_sdk/models/_bulk.py +16 -9
- anaplan_sdk/models/_transactional.py +221 -4
- {anaplan_sdk-0.4.5.dist-info → anaplan_sdk-0.5.0a2.dist-info}/METADATA +1 -1
- anaplan_sdk-0.5.0a2.dist-info/RECORD +30 -0
- anaplan_sdk/_base.py +0 -297
- anaplan_sdk-0.4.5.dist-info/RECORD +0 -30
- {anaplan_sdk-0.4.5.dist-info → anaplan_sdk-0.5.0a2.dist-info}/WHEEL +0 -0
- {anaplan_sdk-0.4.5.dist-info → anaplan_sdk-0.5.0a2.dist-info}/licenses/LICENSE +0 -0
@@ -1,65 +1,124 @@
|
|
1
|
+
import logging
|
1
2
|
from asyncio import gather
|
2
3
|
from itertools import chain
|
3
|
-
from typing import Any
|
4
|
+
from typing import Any, Literal, overload
|
4
5
|
|
5
|
-
import
|
6
|
-
|
7
|
-
|
6
|
+
from anaplan_sdk._services import (
|
7
|
+
_AsyncHttpService,
|
8
|
+
parse_calendar_response,
|
9
|
+
parse_insertion_response,
|
10
|
+
validate_dimension_id,
|
11
|
+
)
|
12
|
+
from anaplan_sdk.exceptions import InvalidIdentifierException
|
8
13
|
from anaplan_sdk.models import (
|
14
|
+
CurrentPeriod,
|
15
|
+
Dimension,
|
16
|
+
DimensionWithCode,
|
17
|
+
FiscalYear,
|
9
18
|
InsertionResult,
|
10
19
|
LineItem,
|
11
20
|
List,
|
21
|
+
ListDeletionResult,
|
12
22
|
ListItem,
|
13
23
|
ListMetadata,
|
24
|
+
Model,
|
25
|
+
ModelCalendar,
|
14
26
|
ModelStatus,
|
15
27
|
Module,
|
28
|
+
View,
|
29
|
+
ViewInfo,
|
16
30
|
)
|
17
31
|
|
32
|
+
logger = logging.getLogger("anaplan_sdk")
|
18
33
|
|
19
|
-
|
20
|
-
|
34
|
+
|
35
|
+
class _AsyncTransactionalClient:
|
36
|
+
def __init__(self, http: _AsyncHttpService, model_id: str) -> None:
|
37
|
+
self._http = http
|
21
38
|
self._url = f"https://api.anaplan.com/2/0/models/{model_id}"
|
22
|
-
|
39
|
+
self._model_id = model_id
|
40
|
+
|
41
|
+
async def get_model_details(self) -> Model:
|
42
|
+
"""
|
43
|
+
Retrieves the Model details for the current Model.
|
44
|
+
:return: The Model details.
|
45
|
+
"""
|
46
|
+
res = await self._http.get(self._url, params={"modelDetails": "true"})
|
47
|
+
return Model.model_validate(res["model"])
|
48
|
+
|
49
|
+
async def get_model_status(self) -> ModelStatus:
|
50
|
+
"""
|
51
|
+
Gets the current status of the Model.
|
52
|
+
:return: The current status of the Model.
|
53
|
+
"""
|
54
|
+
res = await self._http.get(f"{self._url}/status")
|
55
|
+
return ModelStatus.model_validate(res["requestStatus"])
|
56
|
+
|
57
|
+
async def wake_model(self) -> None:
|
58
|
+
"""Wake up the current model."""
|
59
|
+
await self._http.post_empty(
|
60
|
+
f"{self._url}/open", headers={"Content-Type": "application/text"}
|
61
|
+
)
|
62
|
+
logger.info(f"Woke up model '{self._model_id}'.")
|
63
|
+
|
64
|
+
async def close_model(self) -> None:
|
65
|
+
"""Close the current model."""
|
66
|
+
await self._http.post_empty(
|
67
|
+
f"{self._url}/close", headers={"Content-Type": "application/text"}
|
68
|
+
)
|
69
|
+
logger.info(f"Closed model '{self._model_id}'.")
|
23
70
|
|
24
|
-
async def
|
71
|
+
async def get_modules(self) -> list[Module]:
|
25
72
|
"""
|
26
73
|
Lists all the Modules in the Model.
|
27
74
|
:return: The List of Modules.
|
28
75
|
"""
|
29
76
|
return [
|
30
77
|
Module.model_validate(e)
|
31
|
-
for e in await self.
|
78
|
+
for e in await self._http.get_paginated(f"{self._url}/modules", "modules")
|
32
79
|
]
|
33
80
|
|
34
|
-
async def
|
81
|
+
async def get_views(self) -> list[View]:
|
35
82
|
"""
|
36
|
-
|
37
|
-
|
83
|
+
Lists all the Views in the Model. This will include all Modules and potentially other saved
|
84
|
+
views.
|
85
|
+
:return: The List of Views.
|
38
86
|
"""
|
39
|
-
|
40
|
-
|
41
|
-
|
87
|
+
params = {"includesubsidiaryviews": True}
|
88
|
+
return [
|
89
|
+
View.model_validate(e)
|
90
|
+
for e in await self._http.get_paginated(f"{self._url}/views", "views", params=params)
|
91
|
+
]
|
92
|
+
|
93
|
+
async def get_view_info(self, view_id: int) -> ViewInfo:
|
94
|
+
"""
|
95
|
+
Gets the detailed information about a View.
|
96
|
+
:param view_id: The ID of the View.
|
97
|
+
:return: The information about the View.
|
98
|
+
"""
|
99
|
+
return ViewInfo.model_validate((await self._http.get(f"{self._url}/views/{view_id}")))
|
42
100
|
|
43
|
-
async def
|
101
|
+
async def get_line_items(self, only_module_id: int | None = None) -> list[LineItem]:
|
44
102
|
"""
|
45
103
|
Lists all the Line Items in the Model.
|
46
104
|
:param only_module_id: If provided, only Line Items from this Module will be returned.
|
47
|
-
:return: All Line Items on this Model.
|
105
|
+
:return: All Line Items on this Model or only from the specified Module.
|
48
106
|
"""
|
49
|
-
|
107
|
+
res = await self._http.get(
|
50
108
|
f"{self._url}/modules/{only_module_id}/lineItems?includeAll=true"
|
51
109
|
if only_module_id
|
52
110
|
else f"{self._url}/lineItems?includeAll=true"
|
53
111
|
)
|
54
|
-
return [LineItem.model_validate(e) for e in
|
112
|
+
return [LineItem.model_validate(e) for e in res.get("items", [])]
|
55
113
|
|
56
|
-
async def
|
114
|
+
async def get_lists(self) -> list[List]:
|
57
115
|
"""
|
58
116
|
Lists all the Lists in the Model.
|
59
117
|
:return: All Lists on this model.
|
60
118
|
"""
|
61
119
|
return [
|
62
|
-
List.model_validate(e)
|
120
|
+
List.model_validate(e)
|
121
|
+
for e in await self._http.get_paginated(f"{self._url}/lists", "lists")
|
63
122
|
]
|
64
123
|
|
65
124
|
async def get_list_metadata(self, list_id: int) -> ListMetadata:
|
@@ -70,21 +129,34 @@ class _AsyncTransactionalClient(_AsyncBaseClient):
|
|
70
129
|
"""
|
71
130
|
|
72
131
|
return ListMetadata.model_validate(
|
73
|
-
(await self.
|
132
|
+
(await self._http.get(f"{self._url}/lists/{list_id}")).get("metadata")
|
74
133
|
)
|
75
134
|
|
76
|
-
|
135
|
+
@overload
|
136
|
+
async def get_list_items(
|
137
|
+
self, list_id: int, return_raw: Literal[False] = False
|
138
|
+
) -> list[ListItem]: ...
|
139
|
+
|
140
|
+
@overload
|
141
|
+
async def get_list_items(
|
142
|
+
self, list_id: int, return_raw: Literal[True] = True
|
143
|
+
) -> list[dict[str, Any]]: ...
|
144
|
+
|
145
|
+
async def get_list_items(
|
146
|
+
self, list_id: int, return_raw: bool = False
|
147
|
+
) -> list[ListItem] | list[dict[str, Any]]:
|
77
148
|
"""
|
78
149
|
Gets all the items in a List.
|
79
150
|
:param list_id: The ID of the List.
|
151
|
+
:param return_raw: If True, returns the items as a list of dictionaries instead of ListItem
|
152
|
+
objects. If you use the result of this call in a DataFrame or you simply pass on the
|
153
|
+
data, you will want to set this to avoid unnecessary (de-)serialization.
|
80
154
|
:return: All items in the List.
|
81
155
|
"""
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
)
|
87
|
-
]
|
156
|
+
res = await self._http.get(f"{self._url}/lists/{list_id}/items?includeAll=true")
|
157
|
+
if return_raw:
|
158
|
+
return res.get("listItems", [])
|
159
|
+
return [ListItem.model_validate(e) for e in res.get("listItems", [])]
|
88
160
|
|
89
161
|
async def insert_list_items(
|
90
162
|
self, list_id: int, items: list[dict[str, str | int | dict]]
|
@@ -106,30 +178,31 @@ class _AsyncTransactionalClient(_AsyncBaseClient):
|
|
106
178
|
:return: The result of the insertion, indicating how many items were added,
|
107
179
|
ignored or failed.
|
108
180
|
"""
|
181
|
+
if not items:
|
182
|
+
return InsertionResult(added=0, ignored=0, failures=[], total=0)
|
109
183
|
if len(items) <= 100_000:
|
110
|
-
|
111
|
-
await self.
|
184
|
+
result = InsertionResult.model_validate(
|
185
|
+
await self._http.post(
|
112
186
|
f"{self._url}/lists/{list_id}/items?action=add", json={"items": items}
|
113
187
|
)
|
114
188
|
)
|
189
|
+
logger.info(f"Inserted {result.added} items into list '{list_id}'.")
|
190
|
+
return result
|
115
191
|
responses = await gather(
|
116
192
|
*(
|
117
|
-
self.
|
193
|
+
self._http.post(
|
194
|
+
f"{self._url}/lists/{list_id}/items?action=add", json={"items": chunk}
|
195
|
+
)
|
118
196
|
for chunk in (items[i : i + 100_000] for i in range(0, len(items), 100_000))
|
119
197
|
)
|
120
198
|
)
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
added += res.get("added", 0)
|
125
|
-
total += res.get("total", 0)
|
126
|
-
ignored += res.get("ignored", 0)
|
127
|
-
|
128
|
-
return InsertionResult(
|
129
|
-
added=added, ignored=ignored, total=total, failures=list(chain.from_iterable(failures))
|
130
|
-
)
|
199
|
+
result = parse_insertion_response(responses)
|
200
|
+
logger.info(f"Inserted {result.added} items into list '{list_id}'.")
|
201
|
+
return result
|
131
202
|
|
132
|
-
async def delete_list_items(
|
203
|
+
async def delete_list_items(
|
204
|
+
self, list_id: int, items: list[dict[str, str | int]]
|
205
|
+
) -> ListDeletionResult:
|
133
206
|
"""
|
134
207
|
Deletes items from a List. If you pass a long list, it will be split into chunks of 100,000
|
135
208
|
items, the maximum allowed by the API.
|
@@ -143,31 +216,41 @@ class _AsyncTransactionalClient(_AsyncBaseClient):
|
|
143
216
|
|
144
217
|
:param list_id: The ID of the List.
|
145
218
|
:param items: The items to delete from the List. Must be a dict with either `code` or `id`
|
146
|
-
as the keys to identify the records to delete.
|
219
|
+
as the keys to identify the records to delete. Specifying both will error.
|
220
|
+
:return: The result of the deletion, indicating how many items were deleted or failed.
|
147
221
|
"""
|
222
|
+
if not items:
|
223
|
+
return ListDeletionResult(deleted=0, failures=[])
|
148
224
|
if len(items) <= 100_000:
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
225
|
+
res = await self._http.post(
|
226
|
+
f"{self._url}/lists/{list_id}/items?action=delete", json={"items": items}
|
227
|
+
)
|
228
|
+
info = ListDeletionResult.model_validate(res)
|
229
|
+
logger.info(f"Deleted {info.deleted} items from list '{list_id}'.")
|
230
|
+
return info
|
154
231
|
|
155
232
|
responses = await gather(
|
156
233
|
*(
|
157
|
-
self.
|
234
|
+
self._http.post(
|
158
235
|
f"{self._url}/lists/{list_id}/items?action=delete", json={"items": chunk}
|
159
236
|
)
|
160
237
|
for chunk in (items[i : i + 100_000] for i in range(0, len(items), 100_000))
|
161
238
|
)
|
162
239
|
)
|
163
|
-
|
240
|
+
info = ListDeletionResult(
|
241
|
+
deleted=sum(res.get("deleted", 0) for res in responses),
|
242
|
+
failures=list(chain.from_iterable(res.get("failures", []) for res in responses)),
|
243
|
+
)
|
244
|
+
logger.info(f"Deleted {info} items from list '{list_id}'.")
|
245
|
+
return info
|
164
246
|
|
165
247
|
async def reset_list_index(self, list_id: int) -> None:
|
166
248
|
"""
|
167
249
|
Resets the index of a List. The List must be empty to do so.
|
168
250
|
:param list_id: The ID of the List.
|
169
251
|
"""
|
170
|
-
await self.
|
252
|
+
await self._http.post_empty(f"{self._url}/lists/{list_id}/resetIndex")
|
253
|
+
logger.info(f"Reset index for list '{list_id}'.")
|
171
254
|
|
172
255
|
async def update_module_data(
|
173
256
|
self, module_id: int, data: list[dict[str, Any]]
|
@@ -186,5 +269,120 @@ class _AsyncTransactionalClient(_AsyncBaseClient):
|
|
186
269
|
:param data: The data to write to the Module.
|
187
270
|
:return: The number of cells changed or the response with the according error details.
|
188
271
|
"""
|
189
|
-
res = await self.
|
272
|
+
res = await self._http.post(f"{self._url}/modules/{module_id}/data", json=data)
|
273
|
+
if "failures" not in res:
|
274
|
+
logger.info(f"Updated {res['numberOfCellsChanged']} cells in module '{module_id}'.")
|
190
275
|
return res if "failures" in res else res["numberOfCellsChanged"]
|
276
|
+
|
277
|
+
async def get_current_period(self) -> CurrentPeriod:
|
278
|
+
"""
|
279
|
+
Gets the current period of the model.
|
280
|
+
:return: The current period of the model.
|
281
|
+
"""
|
282
|
+
res = await self._http.get(f"{self._url}/currentPeriod")
|
283
|
+
return CurrentPeriod.model_validate(res["currentPeriod"])
|
284
|
+
|
285
|
+
async def set_current_period(self, date: str) -> CurrentPeriod:
|
286
|
+
"""
|
287
|
+
Sets the current period of the model to the given date.
|
288
|
+
:param date: The date to set the current period to, in the format 'YYYY-MM-DD'.
|
289
|
+
:return: The updated current period of the model.
|
290
|
+
"""
|
291
|
+
res = await self._http.put(f"{self._url}/currentPeriod", {"date": date})
|
292
|
+
logger.info(f"Set current period to '{date}'.")
|
293
|
+
return CurrentPeriod.model_validate(res["currentPeriod"])
|
294
|
+
|
295
|
+
async def set_current_fiscal_year(self, year: str) -> FiscalYear:
|
296
|
+
"""
|
297
|
+
Sets the current fiscal year of the model.
|
298
|
+
:param year: The fiscal year to set, in the format specified in the model, e.g. FY24.
|
299
|
+
:return: The updated fiscal year of the model.
|
300
|
+
"""
|
301
|
+
res = await self._http.put(f"{self._url}/modelCalendar/fiscalYear", {"year": year})
|
302
|
+
logger.info(f"Set current fiscal year to '{year}'.")
|
303
|
+
return FiscalYear.model_validate(res["modelCalendar"]["fiscalYear"])
|
304
|
+
|
305
|
+
async def get_model_calendar(self) -> ModelCalendar:
|
306
|
+
"""
|
307
|
+
Get the calendar settings of the model.
|
308
|
+
:return: The calendar settings of the model.
|
309
|
+
"""
|
310
|
+
return parse_calendar_response(await self._http.get(f"{self._url}/modelCalendar"))
|
311
|
+
|
312
|
+
async def get_dimension_items(self, dimension_id: int) -> list[DimensionWithCode]:
|
313
|
+
"""
|
314
|
+
Get all items in a dimension. This will fail if the dimensions holds more than 1_000_000
|
315
|
+
items. Valid Dimensions are:
|
316
|
+
|
317
|
+
- Lists (101xxxxxxxxx)
|
318
|
+
- List Subsets (109xxxxxxxxx)
|
319
|
+
- Line Item Subsets (114xxxxxxxxx)
|
320
|
+
- Users (101999999999)
|
321
|
+
For lists and users, you should prefer using the `get_list_items` and `get_users` methods,
|
322
|
+
respectively, instead.
|
323
|
+
:param dimension_id: The ID of the dimension to list items for.
|
324
|
+
:return: A list of Dimension items.
|
325
|
+
"""
|
326
|
+
res = await self._http.get(
|
327
|
+
f"{self._url}/dimensions/{validate_dimension_id(dimension_id)}/items"
|
328
|
+
)
|
329
|
+
return [DimensionWithCode.model_validate(e) for e in res.get("items", [])]
|
330
|
+
|
331
|
+
async def lookup_dimension_items(
|
332
|
+
self, dimension_id: int, codes: list[str] = None, names: list[str] = None
|
333
|
+
) -> list[DimensionWithCode]:
|
334
|
+
"""
|
335
|
+
Looks up items in a dimension by their codes or names. If both are provided, both will be
|
336
|
+
searched for. You must provide at least one of `codes` or `names`. Valid Dimensions to
|
337
|
+
lookup are:
|
338
|
+
|
339
|
+
- Lists (101xxxxxxxxx)
|
340
|
+
- Time (20000000003)
|
341
|
+
- Version (20000000020)
|
342
|
+
- Users (101999999999)
|
343
|
+
:param dimension_id: The ID of the dimension to lookup items for.
|
344
|
+
:param codes: A list of codes to lookup in the dimension.
|
345
|
+
:param names: A list of names to lookup in the dimension.
|
346
|
+
:return: A list of Dimension items that match the provided codes or names.
|
347
|
+
"""
|
348
|
+
if not codes and not names:
|
349
|
+
raise ValueError("At least one of 'codes' or 'names' must be provided.")
|
350
|
+
if not (
|
351
|
+
dimension_id == 101999999999
|
352
|
+
or 101_000_000_000 <= dimension_id < 102_000_000_000
|
353
|
+
or dimension_id == 20000000003
|
354
|
+
or dimension_id == 20000000020
|
355
|
+
):
|
356
|
+
raise InvalidIdentifierException(
|
357
|
+
"Invalid dimension_id. Must be a List (101xxxxxxxxx), Time (20000000003), "
|
358
|
+
"Version (20000000020), or Users (101999999999)."
|
359
|
+
)
|
360
|
+
res = await self._http.post(
|
361
|
+
f"{self._url}/dimensions/{dimension_id}/items", json={"codes": codes, "names": names}
|
362
|
+
)
|
363
|
+
return [DimensionWithCode.model_validate(e) for e in res.get("items", [])]
|
364
|
+
|
365
|
+
async def get_view_dimension_items(self, view_id: int, dimension_id: int) -> list[Dimension]:
|
366
|
+
"""
|
367
|
+
Get the members of a dimension that are part of the given View. This call returns data as
|
368
|
+
filtered by the page builder when they configure the view. This call respects hidden items,
|
369
|
+
filtering selections, and Selective Access. If the view contains hidden or filtered items,
|
370
|
+
these do not display in the response. This will fail if the dimensions holds more than
|
371
|
+
1_000_000 items. The response returns Items within a flat list (no hierarchy) and order
|
372
|
+
is not guaranteed.
|
373
|
+
:param view_id: The ID of the View.
|
374
|
+
:param dimension_id: The ID of the Dimension to get items for.
|
375
|
+
:return: A list of Dimensions used in the View.
|
376
|
+
"""
|
377
|
+
res = await self._http.get(f"{self._url}/views/{view_id}/dimensions/{dimension_id}/items")
|
378
|
+
return [Dimension.model_validate(e) for e in res.get("items", [])]
|
379
|
+
|
380
|
+
async def get_line_item_dimensions(self, line_item_id: int) -> list[Dimension]:
|
381
|
+
"""
|
382
|
+
Get the dimensions of a Line Item. This will return all dimensions that are used in the
|
383
|
+
Line Item.
|
384
|
+
:param line_item_id: The ID of the Line Item.
|
385
|
+
:return: A list of Dimensions used in the Line Item.
|
386
|
+
"""
|
387
|
+
res = await self._http.get(f"{self._url}/lineItems/{line_item_id}/dimensions")
|
388
|
+
return [Dimension.model_validate(e) for e in res.get("dimensions", [])]
|