aps-automation-sdk 0.1.0__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.
- aps_automation_sdk/__init__.py +51 -0
- aps_automation_sdk/acc.py +205 -0
- aps_automation_sdk/classes.py +387 -0
- aps_automation_sdk/core.py +252 -0
- aps_automation_sdk/dsl.py +59 -0
- aps_automation_sdk/utils.py +161 -0
- aps_automation_sdk-0.1.0.dist-info/METADATA +227 -0
- aps_automation_sdk-0.1.0.dist-info/RECORD +10 -0
- aps_automation_sdk-0.1.0.dist-info/WHEEL +4 -0
- aps_automation_sdk-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
|
|
2
|
+
from .classes import (
|
|
3
|
+
Activity,
|
|
4
|
+
ActivityParameter,
|
|
5
|
+
ActivityInputParameter,
|
|
6
|
+
ActivityOutputParameter,
|
|
7
|
+
ActivityJsonParameter,
|
|
8
|
+
AppBundle,
|
|
9
|
+
WorkItem,
|
|
10
|
+
ActivityInputParameterAcc,
|
|
11
|
+
ActivityOutputParameterAcc,
|
|
12
|
+
UploadActivityInputParameter,
|
|
13
|
+
WorkItemAcc
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
from .utils import (
|
|
17
|
+
set_nickname,
|
|
18
|
+
get_token,
|
|
19
|
+
get_nickname,
|
|
20
|
+
delete_activity,
|
|
21
|
+
delete_appbundle,
|
|
22
|
+
create_bucket,
|
|
23
|
+
create_appbundle_version,
|
|
24
|
+
move_or_create_alias,
|
|
25
|
+
publish_appbundle_update
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"Activity",
|
|
30
|
+
"ActivityParameter",
|
|
31
|
+
"ActivityInputParameter",
|
|
32
|
+
"ActivityOutputParameter",
|
|
33
|
+
"ActivityJsonParameter",
|
|
34
|
+
"AppBundle",
|
|
35
|
+
"WorkItem",
|
|
36
|
+
"ActivityInputParameterAcc",
|
|
37
|
+
"ActivityOutputParameterAcc",
|
|
38
|
+
"UploadActivityInputParameter",
|
|
39
|
+
"WorkItemAcc",
|
|
40
|
+
"get_token",
|
|
41
|
+
"get_nickname",
|
|
42
|
+
"delete_activity",
|
|
43
|
+
"delete_appbundle",
|
|
44
|
+
"create_bucket",
|
|
45
|
+
"create_appbundle_version",
|
|
46
|
+
"move_or_create_alias",
|
|
47
|
+
"publish_appbundle_update",
|
|
48
|
+
"set_nickname"
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import urllib.parse
|
|
3
|
+
import json
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
APS_BASE_URL = "https://developer.api.autodesk.com"
|
|
7
|
+
PROJECTS_V1 = f"{APS_BASE_URL}/project/v1"
|
|
8
|
+
DATA_V1 = f"{APS_BASE_URL}/data/v1"
|
|
9
|
+
DATA_V2 = f"{APS_BASE_URL}/data/v2"
|
|
10
|
+
DA_V3 = f"{APS_BASE_URL}/da/us-east/v3"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def bearer(token: str) -> dict[str, str]:
|
|
14
|
+
return {"Authorization": f"Bearer {token}"}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def item_from_version(project_id: str, version_urn: str, token: str) -> str:
|
|
18
|
+
"""Get item ID from version URN."""
|
|
19
|
+
if "?version=" not in version_urn:
|
|
20
|
+
raise RuntimeError("version URN must include '?version=N'")
|
|
21
|
+
url = f"{DATA_V1}/projects/{project_id}/versions/{urllib.parse.quote(version_urn, safe='')}/item"
|
|
22
|
+
r = requests.get(url, headers=bearer(token), timeout=30)
|
|
23
|
+
r.raise_for_status()
|
|
24
|
+
data = r.json().get("data", {})
|
|
25
|
+
if data.get("type") != "items" or "id" not in data:
|
|
26
|
+
raise RuntimeError(
|
|
27
|
+
f"Unexpected payload for versions->item, {json.dumps(r.json())[:400]}"
|
|
28
|
+
)
|
|
29
|
+
return data["id"]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def parent_folder_from_item(project_id: str, item_id: str, token: str) -> str:
|
|
33
|
+
"""Get parent folder ID from item ID."""
|
|
34
|
+
url = f"{DATA_V1}/projects/{project_id}/items/{urllib.parse.quote(item_id, safe='')}/parent"
|
|
35
|
+
r = requests.get(url, headers=bearer(token), timeout=30)
|
|
36
|
+
r.raise_for_status()
|
|
37
|
+
data = r.json().get("data", {})
|
|
38
|
+
if data.get("type") != "folders" or "id" not in data:
|
|
39
|
+
raise RuntimeError(
|
|
40
|
+
f"Unexpected payload for item->parent, {json.dumps(r.json())[:400]}"
|
|
41
|
+
)
|
|
42
|
+
return data["id"]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def resolve_parent_folder(project_id: str, any_urn: str, token: str) -> str:
|
|
46
|
+
"""Resolve parent folder ID from any version URN."""
|
|
47
|
+
item_id = item_from_version(project_id, any_urn, token)
|
|
48
|
+
folder_id = parent_folder_from_item(project_id, item_id, token)
|
|
49
|
+
return folder_id
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def get_item_tip_version(
|
|
53
|
+
project_id: str, item_lineage_urn: str, token: str
|
|
54
|
+
) -> dict[str, Any]:
|
|
55
|
+
url = f"{DATA_V1}/projects/{project_id}/items/{urllib.parse.quote(item_lineage_urn, safe=':')}/tip"
|
|
56
|
+
r = requests.get(url, headers=bearer(token), timeout=30)
|
|
57
|
+
r.raise_for_status()
|
|
58
|
+
return r.json()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def find_tip_storage_id(tip_payload: dict[str, Any]) -> str:
|
|
62
|
+
"""
|
|
63
|
+
Return the storage objectId for the tip version.
|
|
64
|
+
"""
|
|
65
|
+
nodes = [tip_payload.get("data", {})] + tip_payload.get("included", [])
|
|
66
|
+
for node in nodes:
|
|
67
|
+
rel = node.get("relationships", {})
|
|
68
|
+
st = rel.get("storage") or {}
|
|
69
|
+
data_rel = st.get("data") or {}
|
|
70
|
+
sid = data_rel.get("id")
|
|
71
|
+
if sid:
|
|
72
|
+
return sid
|
|
73
|
+
raise RuntimeError("No storage id found in tip payload")
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def create_storage(project_id: str, folder_urn: str, file_name: str, token: str) -> str:
|
|
77
|
+
"""POST /data/v1/projects/{project_id}/storage, returns storage objectId"""
|
|
78
|
+
url = f"{DATA_V1}/projects/{project_id}/storage"
|
|
79
|
+
payload = {
|
|
80
|
+
"jsonapi": {"version": "1.0"},
|
|
81
|
+
"data": {
|
|
82
|
+
"type": "objects",
|
|
83
|
+
"attributes": {"name": file_name},
|
|
84
|
+
"relationships": {
|
|
85
|
+
"target": {"data": {"type": "folders", "id": folder_urn}}
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
r = requests.post(
|
|
90
|
+
url,
|
|
91
|
+
headers={**bearer(token), "Content-Type": "application/vnd.api+json"},
|
|
92
|
+
json=payload,
|
|
93
|
+
timeout=30,
|
|
94
|
+
)
|
|
95
|
+
r.raise_for_status()
|
|
96
|
+
storage_id = r.json().get("data", {}).get("id")
|
|
97
|
+
if not storage_id:
|
|
98
|
+
raise RuntimeError("Storage creation returned no id")
|
|
99
|
+
return storage_id
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def to_data_url_json(obj: dict) -> str:
|
|
103
|
+
"""Convert a dict to a minified JSON data URL for inline payload."""
|
|
104
|
+
return "data:application/json," + urllib.parse.quote(
|
|
105
|
+
json.dumps(obj, separators=(",", ":"))
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def find_item_by_name(
|
|
110
|
+
project_id: str, folder_urn: str, file_name: str, token: str
|
|
111
|
+
) -> str | None:
|
|
112
|
+
url = f"{DATA_V1}/projects/{project_id}/folders/{folder_urn}/contents"
|
|
113
|
+
r = requests.get(url, headers=bearer(token), timeout=30)
|
|
114
|
+
r.raise_for_status()
|
|
115
|
+
for entry in r.json().get("data", []):
|
|
116
|
+
if (
|
|
117
|
+
entry.get("type") == "items"
|
|
118
|
+
and entry.get("attributes", {}).get("displayName") == file_name
|
|
119
|
+
):
|
|
120
|
+
return entry.get("id")
|
|
121
|
+
return None
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def create_version_for_item(
|
|
125
|
+
project_id: str, item_id: str, file_name: str, storage_id: str, token: str
|
|
126
|
+
) -> dict[str, Any]:
|
|
127
|
+
url = f"{DATA_V1}/projects/{project_id}/versions"
|
|
128
|
+
payload = {
|
|
129
|
+
"jsonapi": {"version": "1.0"},
|
|
130
|
+
"data": {
|
|
131
|
+
"type": "versions",
|
|
132
|
+
"attributes": {
|
|
133
|
+
"name": file_name,
|
|
134
|
+
"extension": {
|
|
135
|
+
"type": "versions:autodesk.bim360:File",
|
|
136
|
+
"version": "1.0",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
"relationships": {
|
|
140
|
+
"item": {"data": {"type": "items", "id": item_id}},
|
|
141
|
+
"storage": {"data": {"type": "objects", "id": storage_id}},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
r = requests.post(
|
|
146
|
+
url,
|
|
147
|
+
headers={**bearer(token), "Content-Type": "application/vnd.api+json"},
|
|
148
|
+
json=payload,
|
|
149
|
+
timeout=60,
|
|
150
|
+
)
|
|
151
|
+
r.raise_for_status()
|
|
152
|
+
return r.json()
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def create_item_with_first_version(
|
|
156
|
+
project_id: str,
|
|
157
|
+
folder_urn: str,
|
|
158
|
+
file_name: str,
|
|
159
|
+
storage_id: str,
|
|
160
|
+
token: str,
|
|
161
|
+
version: int = 1,
|
|
162
|
+
) -> dict[str, Any]:
|
|
163
|
+
"""
|
|
164
|
+
POST /data/v1/projects/{project_id}/items to create a new Item and first Version
|
|
165
|
+
referencing the storage we prepared and Automation wrote to.
|
|
166
|
+
"""
|
|
167
|
+
url = f"{DATA_V1}/projects/{project_id}/items"
|
|
168
|
+
payload = {
|
|
169
|
+
"jsonapi": {"version": "1.0"},
|
|
170
|
+
"data": {
|
|
171
|
+
"type": "items",
|
|
172
|
+
"attributes": {
|
|
173
|
+
"displayName": file_name,
|
|
174
|
+
"extension": {"type": "items:autodesk.bim360:File", "version": "1.0"},
|
|
175
|
+
},
|
|
176
|
+
"relationships": {
|
|
177
|
+
"tip": {"data": {"type": "versions", "id": str(version)}},
|
|
178
|
+
"parent": {"data": {"type": "folders", "id": folder_urn}},
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
"included": [
|
|
182
|
+
{
|
|
183
|
+
"type": "versions",
|
|
184
|
+
"id": "1",
|
|
185
|
+
"attributes": {
|
|
186
|
+
"name": file_name,
|
|
187
|
+
"extension": {
|
|
188
|
+
"type": "versions:autodesk.bim360:File",
|
|
189
|
+
"version": "1.0",
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
"relationships": {
|
|
193
|
+
"storage": {"data": {"type": "objects", "id": storage_id}}
|
|
194
|
+
},
|
|
195
|
+
}
|
|
196
|
+
],
|
|
197
|
+
}
|
|
198
|
+
r = requests.post(
|
|
199
|
+
url,
|
|
200
|
+
headers={**bearer(token), "Content-Type": "application/vnd.api+json"},
|
|
201
|
+
json=payload,
|
|
202
|
+
timeout=60,
|
|
203
|
+
)
|
|
204
|
+
r.raise_for_status()
|
|
205
|
+
return r.json()
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from typing import Literal, Any, Optional
|
|
3
|
+
from pydantic import BaseModel, Field, PrivateAttr
|
|
4
|
+
from .core import (
|
|
5
|
+
get_signed_s3_upload,
|
|
6
|
+
put_to_signed_url,
|
|
7
|
+
complete_signed_s3_upload,
|
|
8
|
+
build_oss_urn,
|
|
9
|
+
get_signed_s3_download,
|
|
10
|
+
dowload_from_signed_url,
|
|
11
|
+
create_activity,
|
|
12
|
+
create_activity_alias,
|
|
13
|
+
upload_appbundle,
|
|
14
|
+
create_appbundle_alias,
|
|
15
|
+
register_appbundle,
|
|
16
|
+
run_work_item,
|
|
17
|
+
poll_workitem_status,
|
|
18
|
+
run_public_work_item
|
|
19
|
+
)
|
|
20
|
+
from .utils import create_bucket
|
|
21
|
+
from .dsl import RegisterBundleResponse, UploadParameters
|
|
22
|
+
from aps_automation_sdk.acc import get_item_tip_version, find_tip_storage_id, create_storage, create_item_with_first_version, create_version_for_item, find_item_by_name
|
|
23
|
+
|
|
24
|
+
class ActivityParameter(BaseModel):
|
|
25
|
+
name: str
|
|
26
|
+
localName: str
|
|
27
|
+
verb: Literal["get", "put", "post"]
|
|
28
|
+
description: str
|
|
29
|
+
zip: bool = Field(default=False)
|
|
30
|
+
ondemand: bool = Field(default=False)
|
|
31
|
+
required: bool = Field(default=False)
|
|
32
|
+
|
|
33
|
+
# storage, optional for JSON params
|
|
34
|
+
bucketKey: Optional[str] = None
|
|
35
|
+
objectKey: Optional[str] = None
|
|
36
|
+
|
|
37
|
+
# roles
|
|
38
|
+
is_output: bool = False
|
|
39
|
+
is_engine_input: bool = False
|
|
40
|
+
|
|
41
|
+
def oss_keys(self) -> tuple[str, str]:
|
|
42
|
+
if not self.bucketKey or not self.objectKey:
|
|
43
|
+
raise ValueError(f"{self.name}: bucketKey and objectKey are required for OSS operations")
|
|
44
|
+
return self.bucketKey, self.objectKey
|
|
45
|
+
|
|
46
|
+
def ensure_bucket(self, token: str) -> None:
|
|
47
|
+
try:
|
|
48
|
+
create_bucket(bucketKey=self.bucketKey, token=token)
|
|
49
|
+
except Exception:
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
def upload_file_to_oss(self, file_path: str, token: str) -> None:
|
|
53
|
+
bucketKey, objectKey = self.oss_keys()
|
|
54
|
+
self.ensure_bucket(token)
|
|
55
|
+
signed = get_signed_s3_upload(bucketKey=bucketKey, objectKey=objectKey, token=token)
|
|
56
|
+
put_to_signed_url(signed_url=signed.urls[0], file_path=file_path)
|
|
57
|
+
complete_signed_s3_upload(bucketKey=bucketKey, objectKey=objectKey, uploadKey=signed.uploadKey, token=token)
|
|
58
|
+
|
|
59
|
+
def download_to(self, output_path: str, token: str) -> None:
|
|
60
|
+
if not self.is_output:
|
|
61
|
+
raise ValueError(f"{self.name}: download_to is only valid for output parameters")
|
|
62
|
+
bucketKey, objectKey = self.oss_keys()
|
|
63
|
+
signed = get_signed_s3_download(bucketKey=bucketKey, objectKey=objectKey, token=token)
|
|
64
|
+
dowload_from_signed_url(signed_url=signed["url"], output_path=output_path)
|
|
65
|
+
|
|
66
|
+
def generate_oss_urn(self) -> str:
|
|
67
|
+
bucketKey, objectKey = self.oss_keys()
|
|
68
|
+
return build_oss_urn(bucketKey=bucketKey, objectKey=objectKey)
|
|
69
|
+
|
|
70
|
+
def to_api_param(self) -> dict[str, Any]:
|
|
71
|
+
return {
|
|
72
|
+
"localName": self.localName,
|
|
73
|
+
"zip": self.zip,
|
|
74
|
+
"ondemand": self.ondemand,
|
|
75
|
+
"verb": self.verb,
|
|
76
|
+
"description": self.description,
|
|
77
|
+
"required": self.required,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class ActivityInputParameter(ActivityParameter):
|
|
82
|
+
is_output: bool = False
|
|
83
|
+
|
|
84
|
+
def work_item_arg(self, token: str) -> dict[str, Any]:
|
|
85
|
+
return {
|
|
86
|
+
self.name: {
|
|
87
|
+
"url": self.generate_oss_urn(),
|
|
88
|
+
"verb": self.verb,
|
|
89
|
+
"headers": {"Authorization": f"Bearer {token}"},
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class ActivityOutputParameter(ActivityParameter):
|
|
95
|
+
is_output: bool = True
|
|
96
|
+
|
|
97
|
+
def work_item_arg(self, _token: str) -> dict[str, Any]:
|
|
98
|
+
return {
|
|
99
|
+
self.name: {
|
|
100
|
+
"url": self.generate_oss_urn(),
|
|
101
|
+
"verb": self.verb,
|
|
102
|
+
"headers": {"Authorization": f"Bearer {_token}"},
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class ActivityJsonParameter(ActivityParameter):
|
|
108
|
+
content: dict | None = None
|
|
109
|
+
|
|
110
|
+
def work_item_arg(self) -> dict[str, Any]:
|
|
111
|
+
data_str = json.dumps(self.content, separators=(",", ":"))
|
|
112
|
+
return {self.name: {"url": f"data:application/json,{data_str}"}}
|
|
113
|
+
|
|
114
|
+
def set_content(self, data: dict) -> None:
|
|
115
|
+
self.content = data
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class Activity(BaseModel):
|
|
119
|
+
id: str
|
|
120
|
+
parameters: list[ActivityParameter]
|
|
121
|
+
engine: Optional[str] = None
|
|
122
|
+
appbundle_full_name: str
|
|
123
|
+
description: str
|
|
124
|
+
alias: str
|
|
125
|
+
commandLine: Optional[list[str]] = None
|
|
126
|
+
script: str | None = None
|
|
127
|
+
|
|
128
|
+
def param_map(self) -> dict[str, dict[str, Any]]:
|
|
129
|
+
return {p.name: p.to_api_param() for p in self.parameters}
|
|
130
|
+
|
|
131
|
+
@staticmethod
|
|
132
|
+
def short_appbundle_id(appbundle_full_alias: str) -> str:
|
|
133
|
+
right = appbundle_full_alias.split(".", 1)[-1]
|
|
134
|
+
return right.split("+", 1)[0]
|
|
135
|
+
|
|
136
|
+
def set_revit_command_line(self) -> None:
|
|
137
|
+
revit_input = next((p for p in self.parameters if isinstance(p, ActivityInputParameter) and p.is_engine_input), None)
|
|
138
|
+
if revit_input is None:
|
|
139
|
+
raise ValueError("No Revit input parameter marked as engine input")
|
|
140
|
+
appbundle_short_id = self.short_appbundle_id(self.appbundle_full_name)
|
|
141
|
+
self.commandLine = [
|
|
142
|
+
"$(engine.path)\\revitcoreconsole.exe "
|
|
143
|
+
f'/i "$(args[{revit_input.name}].path)" '
|
|
144
|
+
f'/al "$(appbundles[{appbundle_short_id}].path)"'
|
|
145
|
+
]
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def set_autocad_command_line(self) -> None:
|
|
149
|
+
autocad_input = next((p for p in self.parameters if isinstance(p, ActivityInputParameter) and p.is_engine_input), None)
|
|
150
|
+
if autocad_input is None:
|
|
151
|
+
raise ValueError("No AutoCAD input parameter marked as engine input")
|
|
152
|
+
appbundle_short_id = self.short_appbundle_id(self.appbundle_full_name)
|
|
153
|
+
|
|
154
|
+
cmd = (
|
|
155
|
+
"$(engine.path)\\accoreconsole.exe "
|
|
156
|
+
f'/i "$(args[{autocad_input.name}].path)" '
|
|
157
|
+
f'/al "$(appbundles[{appbundle_short_id}].path)"'
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
if self.script:
|
|
161
|
+
cmd += ' /s "$(settings[script].path)"'
|
|
162
|
+
|
|
163
|
+
self.commandLine = [cmd]
|
|
164
|
+
|
|
165
|
+
def to_api_dict(self) -> dict[str, Any]:
|
|
166
|
+
activity_dict = {
|
|
167
|
+
"id": self.id,
|
|
168
|
+
"commandLine": self.commandLine,
|
|
169
|
+
"parameters": self.param_map(),
|
|
170
|
+
"engine": self.engine,
|
|
171
|
+
"appbundles": [self.appbundle_full_name],
|
|
172
|
+
"description": self.description,
|
|
173
|
+
}
|
|
174
|
+
if self.script:
|
|
175
|
+
activity_dict["settings"] = {"script": self.script}
|
|
176
|
+
return activity_dict
|
|
177
|
+
|
|
178
|
+
def deploy(self, token: str) -> None:
|
|
179
|
+
create_activity(token=token, payload=self.to_api_dict())
|
|
180
|
+
create_activity_alias(activity_id=self.id, alias_id=self.alias, version=1, token=token)
|
|
181
|
+
|
|
182
|
+
class AppBundle(BaseModel):
|
|
183
|
+
appBundleId: str
|
|
184
|
+
engine: str
|
|
185
|
+
alias: str
|
|
186
|
+
zip_path: str
|
|
187
|
+
description: str
|
|
188
|
+
version: int = 0 # updated on deploy
|
|
189
|
+
|
|
190
|
+
def register(self, token: str) -> RegisterBundleResponse:
|
|
191
|
+
return register_appbundle(
|
|
192
|
+
appBundleId=self.appBundleId,
|
|
193
|
+
engine=self.engine,
|
|
194
|
+
description=self.description,
|
|
195
|
+
token=token,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
def upload(self, uploadParameters: UploadParameters) -> int:
|
|
199
|
+
return upload_appbundle(
|
|
200
|
+
upload_parameters=uploadParameters,
|
|
201
|
+
zip_path=self.zip_path,
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
def create_alias(self, token: str) -> dict:
|
|
205
|
+
return create_appbundle_alias(
|
|
206
|
+
app_id=self.appBundleId,
|
|
207
|
+
alias_id=self.alias,
|
|
208
|
+
version=self.version,
|
|
209
|
+
token=token,
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
def deploy(self, token: str) -> int:
|
|
213
|
+
reg = self.register(token)
|
|
214
|
+
self.upload(reg.uploadParameters)
|
|
215
|
+
self.version = int(reg.version)
|
|
216
|
+
self.create_alias(token)
|
|
217
|
+
return self.version
|
|
218
|
+
|
|
219
|
+
class WorkItem(BaseModel):
|
|
220
|
+
parameters: list[ActivityParameter]
|
|
221
|
+
activity_full_alias: str
|
|
222
|
+
|
|
223
|
+
def build_arguments(self, token: str) -> dict[str, Any]:
|
|
224
|
+
"""Build work item arguments from all parameters."""
|
|
225
|
+
payload = {}
|
|
226
|
+
for param in self.parameters:
|
|
227
|
+
if isinstance(param, ActivityJsonParameter):
|
|
228
|
+
payload |= param.work_item_arg()
|
|
229
|
+
else:
|
|
230
|
+
payload |= param.work_item_arg(token)
|
|
231
|
+
return payload
|
|
232
|
+
|
|
233
|
+
def run(self, token: str) -> str:
|
|
234
|
+
args = self.build_arguments(token)
|
|
235
|
+
response = run_work_item(
|
|
236
|
+
token=token,
|
|
237
|
+
full_activity_alias=self.activity_full_alias,
|
|
238
|
+
work_item_args=args
|
|
239
|
+
)
|
|
240
|
+
work_item_id = response.get("id")
|
|
241
|
+
if not work_item_id:
|
|
242
|
+
raise RuntimeError("No work item id returned from run_work_item")
|
|
243
|
+
return work_item_id
|
|
244
|
+
|
|
245
|
+
def poll(self, work_item_id: str, token: str, max_wait: int = 600, interval: int = 10) -> dict[str, Any]:
|
|
246
|
+
return poll_workitem_status(work_item_id, token, max_wait=max_wait, interval=interval)
|
|
247
|
+
|
|
248
|
+
def execute(self, token: str, max_wait: int = 600, interval: int = 10) -> dict[str, Any]:
|
|
249
|
+
work_item_id = self.run(token)
|
|
250
|
+
return self.poll(work_item_id, token, max_wait=max_wait, interval=interval)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
class ActivityInputParameterAcc(ActivityInputParameter):
|
|
254
|
+
linage_urn: str | None = None
|
|
255
|
+
project_id: str | None = None
|
|
256
|
+
|
|
257
|
+
def get_acc_storage_url(self, token: str) -> str:
|
|
258
|
+
tip_payload = get_item_tip_version(
|
|
259
|
+
project_id= self.project_id,
|
|
260
|
+
item_lineage_urn= self.linage_urn,
|
|
261
|
+
token=token
|
|
262
|
+
)
|
|
263
|
+
acc_storage_url = find_tip_storage_id(tip_payload)
|
|
264
|
+
return acc_storage_url
|
|
265
|
+
|
|
266
|
+
def work_item_arg_3lo(self, token_3lo: str) -> dict[str, Any]:
|
|
267
|
+
acc_storage_url = self.get_acc_storage_url(token=token_3lo)
|
|
268
|
+
return {
|
|
269
|
+
self.name: {
|
|
270
|
+
"url": acc_storage_url,
|
|
271
|
+
"verb": self.verb,
|
|
272
|
+
"headers": {"Authorization": f"Bearer {token_3lo}"},
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
class UploadActivityInputParameter(ActivityInputParameter):
|
|
277
|
+
folder_id: str
|
|
278
|
+
project_id: str
|
|
279
|
+
file_name: str
|
|
280
|
+
file_path: str
|
|
281
|
+
|
|
282
|
+
def upload_and_create(self, token: str) -> tuple[str, str]:
|
|
283
|
+
|
|
284
|
+
item_id = find_item_by_name(self.project_id, self.folder_id, self.file_name, token)
|
|
285
|
+
# Always create fresh storage for the incoming bytes
|
|
286
|
+
storage_id = create_storage(project_id=self.project_id, folder_urn=self.folder_id, file_name=self.file_name, token=token)
|
|
287
|
+
bucket_key, object_key = storage_id.split("urn:adsk.objects:os.object:")[1].split("/", 1)
|
|
288
|
+
signed = get_signed_s3_upload(bucketKey=bucket_key, objectKey=object_key, token=token)
|
|
289
|
+
print("**"*20)
|
|
290
|
+
print(f"{signed=}")
|
|
291
|
+
put_to_signed_url(signed_url=signed.urls[0], file_path=self.file_path)
|
|
292
|
+
complete_signed_s3_upload(bucketKey=bucket_key, objectKey=object_key, uploadKey=signed.uploadKey, token=token)
|
|
293
|
+
|
|
294
|
+
if item_id:
|
|
295
|
+
# Create a new version on the existing item
|
|
296
|
+
_ = create_version_for_item(
|
|
297
|
+
project_id=self.project_id,
|
|
298
|
+
item_id=item_id,
|
|
299
|
+
file_name=self.file_name,
|
|
300
|
+
storage_id=storage_id,
|
|
301
|
+
token=token,
|
|
302
|
+
)
|
|
303
|
+
lineage_urn = item_id
|
|
304
|
+
acc_storage_url = storage_id
|
|
305
|
+
return acc_storage_url, lineage_urn
|
|
306
|
+
|
|
307
|
+
# First version on a brand new item
|
|
308
|
+
resp = create_item_with_first_version(
|
|
309
|
+
project_id=self.project_id,
|
|
310
|
+
folder_urn=self.folder_id,
|
|
311
|
+
file_name=self.file_name,
|
|
312
|
+
storage_id=storage_id,
|
|
313
|
+
token=token,
|
|
314
|
+
)
|
|
315
|
+
lineage_urn = resp["data"]["id"]
|
|
316
|
+
acc_storage_url = storage_id
|
|
317
|
+
return acc_storage_url, lineage_urn
|
|
318
|
+
|
|
319
|
+
def work_item_arg_3lo(self, token_3lo: str) -> dict[str, Any]:
|
|
320
|
+
acc_storage_url, lineage_urn = self.upload_and_create(token_3lo)
|
|
321
|
+
return {
|
|
322
|
+
self.name: {
|
|
323
|
+
"url": acc_storage_url,
|
|
324
|
+
"verb": self.verb,
|
|
325
|
+
"headers": {"Authorization": f"Bearer {token_3lo}"},
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
class ActivityOutputParameterAcc(ActivityOutputParameter):
|
|
331
|
+
folder_id: str
|
|
332
|
+
project_id: str
|
|
333
|
+
file_name: str
|
|
334
|
+
_storage_id: Optional[str] = PrivateAttr(default=None)
|
|
335
|
+
|
|
336
|
+
def work_item_arg_3lo(self, token_3lo: str) -> dict[str, Any]:
|
|
337
|
+
storage_id = create_storage(project_id=self.project_id, folder_urn=self.folder_id, file_name=self.file_name, token=token_3lo)
|
|
338
|
+
self._storage_id = storage_id
|
|
339
|
+
return {
|
|
340
|
+
self.name: {
|
|
341
|
+
"url": self._storage_id,
|
|
342
|
+
"verb": self.verb,
|
|
343
|
+
"headers": {"Authorization": f"Bearer {token_3lo}"},
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
def create_acc_item(self, token: str):
|
|
348
|
+
if not self._storage_id:
|
|
349
|
+
raise RuntimeError("No storage have being creaded")
|
|
350
|
+
|
|
351
|
+
resp = create_item_with_first_version(
|
|
352
|
+
project_id=self.project_id,
|
|
353
|
+
folder_urn=self.folder_id,
|
|
354
|
+
file_name=self.file_name,
|
|
355
|
+
storage_id=self._storage_id,
|
|
356
|
+
token=token
|
|
357
|
+
)
|
|
358
|
+
return resp
|
|
359
|
+
|
|
360
|
+
class WorkItemAcc(WorkItem):
|
|
361
|
+
|
|
362
|
+
def build_arguments_3lo(self, token3lo: str) ->dict[str, Any]:
|
|
363
|
+
payload: dict[str, Any] = {}
|
|
364
|
+
for param in self.parameters:
|
|
365
|
+
if isinstance(param, ActivityJsonParameter):
|
|
366
|
+
payload |= param.work_item_arg()
|
|
367
|
+
elif isinstance(param, (ActivityInputParameterAcc, ActivityOutputParameterAcc, UploadActivityInputParameter)):
|
|
368
|
+
payload |= param.work_item_arg_3lo(token3lo)
|
|
369
|
+
else:
|
|
370
|
+
raise TypeError(
|
|
371
|
+
f"Parameter '{param.name}' must be ActivityInputParameterAcc or ActivityOutputParameterAcc for 3LO"
|
|
372
|
+
)
|
|
373
|
+
return payload
|
|
374
|
+
|
|
375
|
+
def run_public_activity(self, token3lo: str, activity_signature: str):
|
|
376
|
+
args = self.build_arguments_3lo(token3lo)
|
|
377
|
+
response = run_public_work_item(
|
|
378
|
+
token=token3lo,
|
|
379
|
+
full_activity_alias=self.activity_full_alias,
|
|
380
|
+
work_item_args=args,
|
|
381
|
+
signature=activity_signature,
|
|
382
|
+
)
|
|
383
|
+
workitem_id = response.get("id")
|
|
384
|
+
if not workitem_id:
|
|
385
|
+
raise RuntimeError("No work item id returned from run_public_work_item")
|
|
386
|
+
|
|
387
|
+
return workitem_id
|