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.
@@ -1,13 +1,53 @@
1
- import httpx
1
+ import logging
2
+ from typing import Literal, overload
2
3
 
3
- from anaplan_sdk._base import _BaseClient
4
- from anaplan_sdk.models import ModelRevision, Revision, SyncTask
4
+ from anaplan_sdk._services import _HttpService
5
+ from anaplan_sdk.exceptions import AnaplanActionError
6
+ from anaplan_sdk.models import (
7
+ ModelRevision,
8
+ ReportTask,
9
+ Revision,
10
+ SummaryReport,
11
+ SyncTask,
12
+ TaskSummary,
13
+ )
5
14
 
15
+ logger = logging.getLogger("anaplan_sdk")
6
16
 
7
- class _AlmClient(_BaseClient):
8
- def __init__(self, client: httpx.Client, model_id: str, retry_count: int) -> None:
9
- self._url = f"https://api.anaplan.com/2/0/models/{model_id}/alm"
10
- super().__init__(retry_count, client)
17
+
18
+ class _AlmClient:
19
+ def __init__(self, http: _HttpService, model_id: str) -> None:
20
+ self._http = http
21
+ self._model_id = model_id
22
+ self._url = f"https://api.anaplan.com/2/0/models/{model_id}"
23
+
24
+ def change_model_status(self, status: Literal["online", "offline"]) -> None:
25
+ """
26
+ Use this call to change the status of a model.
27
+ :param status: The status of the model. Can be either "online" or "offline".
28
+ """
29
+ logger.info(f"Changed model status to '{status}' for model {self._model_id}.")
30
+ self._http.put(f"{self._url}/onlineStatus", json={"status": status})
31
+
32
+ def get_revisions(self) -> list[Revision]:
33
+ """
34
+ Use this call to return a list of revisions for a specific model.
35
+ :return: A list of revisions for a specific model.
36
+ """
37
+ res = self._http.get(f"{self._url}/alm/revisions")
38
+ return [Revision.model_validate(e) for e in res.get("revisions", [])]
39
+
40
+ def get_latest_revision(self) -> Revision | None:
41
+ """
42
+ Use this call to return the latest revision for a specific model. The response is in the
43
+ same format as in Getting a list of syncable revisions between two models.
44
+
45
+ If a revision exists, the return list should contain one element only which is the
46
+ latest revision.
47
+ :return: The latest revision for a specific model, or None if no revisions exist.
48
+ """
49
+ res = (self._http.get(f"{self._url}/alm/latestRevision")).get("revisions")
50
+ return Revision.model_validate(res[0]) if res else None
11
51
 
12
52
  def get_syncable_revisions(self, source_model_id: str) -> list[Revision]:
13
53
  """
@@ -19,48 +59,79 @@ class _AlmClient(_BaseClient):
19
59
  :param source_model_id: The ID of the source model.
20
60
  :return: A list of revisions that can be synchronized to the target model.
21
61
  """
22
- return [
23
- Revision.model_validate(e)
24
- for e in self._get(
25
- f"{self._url}/syncableRevisions?sourceModelId={source_model_id}"
26
- ).get("revisions", [])
27
- ]
62
+ res = self._http.get(f"{self._url}/alm/syncableRevisions?sourceModelId={source_model_id}")
63
+ return [Revision.model_validate(e) for e in res.get("revisions", [])]
28
64
 
29
- def get_latest_revision(self) -> list[Revision]:
65
+ def create_revision(self, name: str, description: str) -> Revision:
30
66
  """
31
- Use this call to return the latest revision for a specific model. The response is in the
32
- same format as in Getting a list of syncable revisions between two models.
33
-
34
- If a revision exists, the return list should contain one element only which is the
35
- latest revision.
36
- :return: The latest revision for a specific model.
67
+ Create a new revision for the model.
68
+ :param name: The name (title) of the revision.
69
+ :param description: The description of the revision.
70
+ :return: The created Revision Info.
37
71
  """
38
- return [
39
- Revision.model_validate(e)
40
- for e in self._get(f"{self._url}/latestRevision").get("revisions", [])
41
- ]
72
+ res = self._http.post(
73
+ f"{self._url}/alm/revisions", json={"name": name, "description": description}
74
+ )
75
+ rev = Revision.model_validate(res["revision"])
76
+ logger.info(f"Created revision '{name} ({rev.id})'for model {self._model_id}.")
77
+ return rev
42
78
 
43
- def get_sync_tasks(self) -> list[SyncTask]:
79
+ def get_sync_tasks(self) -> list[TaskSummary]:
44
80
  """
45
- Use this endpoint to return a list of sync tasks for a target model, where the tasks are
46
- either in progress, or they were completed within the last 48 hours.
81
+ List the sync tasks for a target mode. The returned the tasks are either in progress, or
82
+ they completed within the last 48 hours.
83
+ :return: A list of sync tasks in descending order of creation time.
84
+ """
85
+ res = self._http.get(f"{self._url}/alm/syncTasks")
86
+ return [TaskSummary.model_validate(e) for e in res.get("tasks", [])]
47
87
 
48
- The list is in descending order of when the tasks were created.
49
- :return: A list of sync tasks for a target model.
88
+ def get_sync_task(self, task_id: str) -> SyncTask:
89
+ """
90
+ Get the information for a specific sync task.
91
+ :param task_id: The ID of the sync task.
92
+ :return: The sync task information.
50
93
  """
51
- return [
52
- SyncTask.model_validate(e) for e in self._get(f"{self._url}/syncTasks").get("tasks", [])
53
- ]
94
+ res = self._http.get(f"{self._url}/alm/syncTasks/{task_id}")
95
+ return SyncTask.model_validate(res["task"])
54
96
 
55
- def get_revisions(self) -> list[Revision]:
97
+ def sync_models(
98
+ self,
99
+ source_revision_id: str,
100
+ source_model_id: str,
101
+ target_revision_id: str,
102
+ wait_for_completion: bool = True,
103
+ ) -> SyncTask:
56
104
  """
57
- Use this call to return a list of revisions for a specific model.
58
- :return: A list of revisions for a specific model.
105
+ Create a synchronization task between two revisions. This will synchronize the
106
+ source revision of the source model to the target revision of the target model. This will
107
+ fail if the source revision is incompatible with the target revision.
108
+ :param source_revision_id: The ID of the source revision.
109
+ :param source_model_id: The ID of the source model.
110
+ :param target_revision_id: The ID of the target revision.
111
+ :param wait_for_completion: If True, the method will poll the task status and not return
112
+ until the task is complete. If False, it will spawn the task and return immediately.
113
+ :return: The created sync task.
59
114
  """
60
- return [
61
- Revision.model_validate(e)
62
- for e in self._get(f"{self._url}/revisions").get("revisions", [])
63
- ]
115
+ payload = {
116
+ "sourceRevisionId": source_revision_id,
117
+ "sourceModelId": source_model_id,
118
+ "targetRevisionId": target_revision_id,
119
+ }
120
+ res = self._http.post(f"{self._url}/alm/syncTasks", json=payload)
121
+ task = self.get_sync_task(res["task"]["taskId"])
122
+ logger.info(
123
+ f"Started sync task '{task.id}' from Model '{source_model_id}' "
124
+ f"(Revision '{source_revision_id}') to Model '{self._model_id}'."
125
+ )
126
+ if not wait_for_completion:
127
+ return task
128
+ task = self._http.poll_task(self.get_sync_task, task.id)
129
+ if not task.result.successful:
130
+ msg = f"Sync task {task.id} completed with errors: {task.result.error}."
131
+ logger.error(msg)
132
+ raise AnaplanActionError(msg)
133
+ logger.info(f"Sync task {task.id} completed successfully.")
134
+ return task
64
135
 
65
136
  def get_models_for_revision(self, revision_id: str) -> list[ModelRevision]:
66
137
  """
@@ -69,9 +140,139 @@ class _AlmClient(_BaseClient):
69
140
  :param revision_id: The ID of the revision.
70
141
  :return: A list of models that had a specific revision applied to them.
71
142
  """
72
- return [
73
- ModelRevision.model_validate(e)
74
- for e in self._get(f"{self._url}/revisions/{revision_id}/appliedToModels").get(
75
- "appliedToModels", []
76
- )
77
- ]
143
+ res = self._http.get(f"{self._url}/alm/revisions/{revision_id}/appliedToModels")
144
+ return [ModelRevision.model_validate(e) for e in res.get("appliedToModels", [])]
145
+
146
+ def create_comparison_report(
147
+ self,
148
+ source_revision_id: str,
149
+ source_model_id: str,
150
+ target_revision_id: str,
151
+ wait_for_completion: bool = True,
152
+ ) -> ReportTask:
153
+ """
154
+ Generate a full comparison report between two revisions. This will list all the changes made
155
+ to the source revision compared to the target revision.
156
+ :param source_revision_id: The ID of the source revision.
157
+ :param source_model_id: The ID of the source model.
158
+ :param target_revision_id: The ID of the target revision.
159
+ :param wait_for_completion: If True, the method will poll the task status and not return
160
+ until the task is complete. If False, it will spawn the task and return immediately.
161
+ :return: The created report task summary.
162
+ """
163
+ payload = {
164
+ "sourceRevisionId": source_revision_id,
165
+ "sourceModelId": source_model_id,
166
+ "targetRevisionId": target_revision_id,
167
+ }
168
+ res = self._http.post(f"{self._url}/alm/comparisonReportTasks", json=payload)
169
+ task = self.get_comparison_report_task(res["task"]["taskId"])
170
+ logger.info(
171
+ f"Started Comparison Report task '{task.id}' between Model '{source_model_id}' "
172
+ f"(Revision '{source_revision_id}') and Model '{self._model_id}'."
173
+ )
174
+ if not wait_for_completion:
175
+ return task
176
+ task = self._http.poll_task(self.get_comparison_report_task, task.id)
177
+ if not task.result.successful:
178
+ msg = f"Comparison Report task {task.id} completed with errors: {task.result.error}."
179
+ logger.error(msg)
180
+ raise AnaplanActionError(msg)
181
+ logger.info(f"Comparison Report task {task.id} completed successfully.")
182
+ return task
183
+
184
+ def get_comparison_report_task(self, task_id: str) -> ReportTask:
185
+ """
186
+ Get the task information for a comparison report task.
187
+ :param task_id: The ID of the comparison report task.
188
+ :return: The report task information.
189
+ """
190
+ res = self._http.get(f"{self._url}/alm/comparisonReportTasks/{task_id}")
191
+ return ReportTask.model_validate(res["task"])
192
+
193
+ def get_comparison_report(self, task: ReportTask) -> bytes:
194
+ """
195
+ Get the report for a specific task.
196
+ :param task: The report task object containing the task ID.
197
+ :return: The binary content of the comparison report.
198
+ """
199
+ return self._http.get_binary(
200
+ f"{self._url}/alm/comparisonReports/"
201
+ f"{task.result.target_revision_id}/{task.result.source_revision_id}"
202
+ )
203
+
204
+ @overload
205
+ def create_comparison_summary(
206
+ self,
207
+ source_revision_id: str,
208
+ source_model_id: str,
209
+ target_revision_id: str,
210
+ wait_for_completion: Literal[True] = True,
211
+ ) -> SummaryReport: ...
212
+
213
+ @overload
214
+ def create_comparison_summary(
215
+ self,
216
+ source_revision_id: str,
217
+ source_model_id: str,
218
+ target_revision_id: str,
219
+ wait_for_completion: Literal[False] = False,
220
+ ) -> ReportTask: ...
221
+
222
+ def create_comparison_summary(
223
+ self,
224
+ source_revision_id: str,
225
+ source_model_id: str,
226
+ target_revision_id: str,
227
+ wait_for_completion: bool = True,
228
+ ) -> ReportTask | SummaryReport:
229
+ """
230
+ Generate a comparison summary between two revisions.
231
+ :param source_revision_id: The ID of the source revision.
232
+ :param source_model_id: The ID of the source model.
233
+ :param target_revision_id: The ID of the target revision.
234
+ :param wait_for_completion: If True, the method will poll the task status and not return
235
+ until the task is complete. If False, it will spawn the task and return immediately.
236
+ :return: The created summary task or the summary report, if `wait_for_completion` is True.
237
+ """
238
+ payload = {
239
+ "sourceRevisionId": source_revision_id,
240
+ "sourceModelId": source_model_id,
241
+ "targetRevisionId": target_revision_id,
242
+ }
243
+ res = self._http.post(f"{self._url}/alm/summaryReportTasks", json=payload)
244
+ task = self.get_comparison_summary_task(res["task"]["taskId"])
245
+ logger.info(
246
+ f"Started Comparison Summary task '{task.id}' between Model '{source_model_id}' "
247
+ f"(Revision '{source_revision_id}') and Model '{self._model_id}'."
248
+ )
249
+ if not wait_for_completion:
250
+ return task
251
+ task = self._http.poll_task(self.get_comparison_summary_task, task.id)
252
+ if not task.result.successful:
253
+ msg = f"Comparison Summary task {task.id} completed with errors: {task.result.error}."
254
+ logger.error(msg)
255
+ raise AnaplanActionError(msg)
256
+ logger.info(f"Comparison Summary task {task.id} completed successfully.")
257
+ return self.get_comparison_summary(task)
258
+
259
+ def get_comparison_summary_task(self, task_id: str) -> ReportTask:
260
+ """
261
+ Get the task information for a comparison summary task.
262
+ :param task_id: The ID of the comparison summary task.
263
+ :return: The report task information.
264
+ """
265
+ res = self._http.get(f"{self._url}/alm/summaryReportTasks/{task_id}")
266
+ return ReportTask.model_validate(res["task"])
267
+
268
+ def get_comparison_summary(self, task: ReportTask) -> SummaryReport:
269
+ """
270
+ Get the comparison summary for a specific task.
271
+ :param task: The summary task object containing the task ID.
272
+ :return: The binary content of the comparison summary.
273
+ """
274
+ res = self._http.get(
275
+ f"{self._url}/alm/summaryReports/"
276
+ f"{task.result.target_revision_id}/{task.result.source_revision_id}"
277
+ )
278
+ return SummaryReport.model_validate(res["summaryReport"])
@@ -1,21 +1,18 @@
1
- from typing import Literal
1
+ from typing import Any, Literal
2
2
 
3
- import httpx
4
-
5
- from anaplan_sdk._base import _BaseClient
3
+ from anaplan_sdk._services import _HttpService
6
4
  from anaplan_sdk.models import User
7
5
 
8
6
  Event = Literal["all", "byok", "user_activity"]
9
7
 
10
8
 
11
- class _AuditClient(_BaseClient):
12
- def __init__(self, client: httpx.Client, retry_count: int, thread_count: int) -> None:
9
+ class _AuditClient:
10
+ def __init__(self, http: _HttpService) -> None:
11
+ self._http = http
13
12
  self._limit = 10_000
14
- self._thread_count = thread_count
15
13
  self._url = "https://audit.anaplan.com/audit/api/1/events"
16
- super().__init__(retry_count, client)
17
14
 
18
- def list_users(self, search_pattern: str | None = None) -> list[User]:
15
+ def get_users(self, search_pattern: str | None = None) -> list[User]:
19
16
  """
20
17
  Lists all the Users in the authenticated users default tenant.
21
18
  :param search_pattern: Optional filter for users. When provided, case-insensitive matches
@@ -25,7 +22,7 @@ class _AuditClient(_BaseClient):
25
22
  params = {"s": search_pattern} if search_pattern else None
26
23
  return [
27
24
  User.model_validate(e)
28
- for e in self._get_paginated(
25
+ for e in self._http.get_paginated(
29
26
  "https://api.anaplan.com/2/0/users", "users", params=params
30
27
  )
31
28
  ]
@@ -36,19 +33,21 @@ class _AuditClient(_BaseClient):
36
33
  :return: The requested or currently authenticated User.
37
34
  """
38
35
  return User.model_validate(
39
- self._get(f"https://api.anaplan.com/2/0/users/{user_id}").get("user")
36
+ self._http.get(f"https://api.anaplan.com/2/0/users/{user_id}").get("user")
40
37
  )
41
38
 
42
- def get_events(self, days_into_past: int = 30, event_type: Event = "all") -> list:
39
+ def get_events(
40
+ self, days_into_past: int = 30, event_type: Event = "all"
41
+ ) -> list[dict[str, Any]]:
43
42
  """
44
43
  Get audit events from Anaplan Audit API.
45
44
  :param days_into_past: The nuber of days into the past to get events for. The API provides
46
45
  data for up to 30 days.
47
46
  :param event_type: The type of events to get.
48
- :return: A list of audit events.
47
+ :return: A list of log entries, each containing a dictionary with event details.
49
48
  """
50
49
  return list(
51
- self._get_paginated(
50
+ self._http.get_paginated(
52
51
  self._url,
53
52
  "response",
54
53
  params={"type": event_type, "intervalInHours": days_into_past * 24},