使用 Autodesk 更新文件版本 API
Update File Version with Autodesk API
我现在可以 and create an but am unable to update the version. Using the code in Step 6 of this tutorial 即使将 versions:autodesk.core:File
替换为 versions:autodesk.bim360:File
也无法正常工作。从错误消息 Request contains 0 includes instead of 1
来看,我似乎需要第 5 步示例中的 included
块的一部分,但我不确定它应该是什么样子;鉴于外部 relationships
块在第 5 步和第 6 步之间的格式不匹配,我假设内部内容也不同。
def create_version_for_file(self, project_name, file_name, folder_id, object_id, storage_id):
project_id = self.get_project_id_by_name(project_name)
existing_object_id = self.get_version_id_for_file_in_folder(project_name, folder_id, file_name)
url = '{}data/v1/projects/{}/items'.format(self.DOMAIN, project_id)
logger.info('Starting version create at %s for file_name %s, folder %s, object %s',
url, file_name, folder_id, object_id)
if existing_object_id:
logger.info('Creating version for existing object')
data = self._get_version_data_for_existing_file(file_name, object_id, storage_id)
else:
logger.info('Creating version for new object')
data = self._get_version_json_for_new_file(file_name, folder_id, object_id)
response = self.session.post(url, json=data, headers={
'content-type': 'application/vnd.api+json',
'accept': 'application/vnd.api+json'
})
if response.status_code != status.HTTP_201_CREATED:
logger.warn('Version create for %s failed with status %s: %s', file_name, response.status_code,
response.content)
return None
return json.loads(response.content)
def _get_version_json_for_new_file(self, file_name, folder_id, object_id):
return {
"jsonapi": {"version": "1.0"},
"data": {
"type": "items",
"attributes": {
"displayName": file_name,
"extension": {
"type": "items:autodesk.bim360:File",
"version": "1.0"
}
},
"relationships": {
"tip": {
"data": {
"type": "versions",
"id": "1"
}
},
"parent": {
"data": {
"type": "folders",
"id": folder_id
}
}
}
},
"included": [
{
"type": "versions",
"id": "1",
"attributes": {
"name": file_name,
"extension": {
"type": "versions:autodesk.bim360:File",
"version": "1.0"
}
},
"relationships": {
"storage": {
"data": {
"type": "objects",
"id": object_id
}
}
}
}
]
}
def _get_version_data_for_existing_file(self, file_name, object_id, storage_id):
return {
"jsonapi": {
"version": "1.0"
},
"data": {
"type": "versions",
"attributes": {
"name": file_name,
"extension": {
"type": "versions:autodesk.bim360:File",
"version": "1.0"
}
},
"relationships": {
"item": {
"data": {
"type": "items",
"id": object_id
}
},
"storage": {
"data": {
"type": "objects",
"id": storage_id
}
}
}
}
}
您一定是遗漏了一些导致它无法正常工作的细节,但是由于您提供的描述太少,因此没有人可以据此判断...为了创建新版本,您首先需要创建一个新存储位置,将文件上传到该位置,然后 post 引用现有项目 ID 的新版本。
或者查看我的那个节点 js 示例 upload a file and create a new item or version 如果该项目存在。
您更新版本的数据对我来说似乎不正确,请尝试以下步骤。
- 在文件 V1 所在的同一文件夹中创建一个新的存储位置
- 将文件的 v2 上传到在以下步骤中创建的新存储位置。
- 从您成功上传V1的manifest结果中获取资源的item ID或调用以下端点获取ID https://developer.autodesk.com/en/docs/data/v2/reference/http/projects-project_id-folders-folder_id-contents-GET
获得所有这些信息后,要传递的数据将如下所示。
{
"jsonapi": { "version": "1.0" },
"data": {
"type": "versions",
"attributes": {
"name": "v2File.rvt",
"extension": { "type": "versions:autodesk.bim360:File", "version": "1.0"}
},
"relationships": {
"item": { "data": { "type": "items", "id": "urn:adsk.wipprod:dm.lineage:8jehs1iSQGu_wXpZ977bjA" } },
"storage": { "data": { "type": "objects", "id": "urn:adsk.objects:os.object:wip.dm.prod/a7fde689-62cf-49c1-9273-8df976568996.rvt" } }
}
}
}
我能想到的唯一区别是您的 objectId 不正确。下图是使用 API.
上传的两个版本的证明
您的负载是正确的。要创建第二个版本,您必须将请求发送到 CreateVersion endpoint.
url = '{}data/v1/projects/{}/versions'.format(self.DOMAIN, project_id)
我现在可以 versions:autodesk.core:File
替换为 versions:autodesk.bim360:File
也无法正常工作。从错误消息 Request contains 0 includes instead of 1
来看,我似乎需要第 5 步示例中的 included
块的一部分,但我不确定它应该是什么样子;鉴于外部 relationships
块在第 5 步和第 6 步之间的格式不匹配,我假设内部内容也不同。
def create_version_for_file(self, project_name, file_name, folder_id, object_id, storage_id):
project_id = self.get_project_id_by_name(project_name)
existing_object_id = self.get_version_id_for_file_in_folder(project_name, folder_id, file_name)
url = '{}data/v1/projects/{}/items'.format(self.DOMAIN, project_id)
logger.info('Starting version create at %s for file_name %s, folder %s, object %s',
url, file_name, folder_id, object_id)
if existing_object_id:
logger.info('Creating version for existing object')
data = self._get_version_data_for_existing_file(file_name, object_id, storage_id)
else:
logger.info('Creating version for new object')
data = self._get_version_json_for_new_file(file_name, folder_id, object_id)
response = self.session.post(url, json=data, headers={
'content-type': 'application/vnd.api+json',
'accept': 'application/vnd.api+json'
})
if response.status_code != status.HTTP_201_CREATED:
logger.warn('Version create for %s failed with status %s: %s', file_name, response.status_code,
response.content)
return None
return json.loads(response.content)
def _get_version_json_for_new_file(self, file_name, folder_id, object_id):
return {
"jsonapi": {"version": "1.0"},
"data": {
"type": "items",
"attributes": {
"displayName": file_name,
"extension": {
"type": "items:autodesk.bim360:File",
"version": "1.0"
}
},
"relationships": {
"tip": {
"data": {
"type": "versions",
"id": "1"
}
},
"parent": {
"data": {
"type": "folders",
"id": folder_id
}
}
}
},
"included": [
{
"type": "versions",
"id": "1",
"attributes": {
"name": file_name,
"extension": {
"type": "versions:autodesk.bim360:File",
"version": "1.0"
}
},
"relationships": {
"storage": {
"data": {
"type": "objects",
"id": object_id
}
}
}
}
]
}
def _get_version_data_for_existing_file(self, file_name, object_id, storage_id):
return {
"jsonapi": {
"version": "1.0"
},
"data": {
"type": "versions",
"attributes": {
"name": file_name,
"extension": {
"type": "versions:autodesk.bim360:File",
"version": "1.0"
}
},
"relationships": {
"item": {
"data": {
"type": "items",
"id": object_id
}
},
"storage": {
"data": {
"type": "objects",
"id": storage_id
}
}
}
}
}
您一定是遗漏了一些导致它无法正常工作的细节,但是由于您提供的描述太少,因此没有人可以据此判断...为了创建新版本,您首先需要创建一个新存储位置,将文件上传到该位置,然后 post 引用现有项目 ID 的新版本。
或者查看我的那个节点 js 示例 upload a file and create a new item or version 如果该项目存在。
您更新版本的数据对我来说似乎不正确,请尝试以下步骤。
- 在文件 V1 所在的同一文件夹中创建一个新的存储位置
- 将文件的 v2 上传到在以下步骤中创建的新存储位置。
- 从您成功上传V1的manifest结果中获取资源的item ID或调用以下端点获取ID https://developer.autodesk.com/en/docs/data/v2/reference/http/projects-project_id-folders-folder_id-contents-GET
获得所有这些信息后,要传递的数据将如下所示。
{ "jsonapi": { "version": "1.0" }, "data": { "type": "versions", "attributes": { "name": "v2File.rvt", "extension": { "type": "versions:autodesk.bim360:File", "version": "1.0"} }, "relationships": { "item": { "data": { "type": "items", "id": "urn:adsk.wipprod:dm.lineage:8jehs1iSQGu_wXpZ977bjA" } }, "storage": { "data": { "type": "objects", "id": "urn:adsk.objects:os.object:wip.dm.prod/a7fde689-62cf-49c1-9273-8df976568996.rvt" } } } } }
我能想到的唯一区别是您的 objectId 不正确。下图是使用 API.
上传的两个版本的证明您的负载是正确的。要创建第二个版本,您必须将请求发送到 CreateVersion endpoint.
url = '{}data/v1/projects/{}/versions'.format(self.DOMAIN, project_id)