使用Autodesk API更新文件版本 [英] Update File Version with Autodesk API

查看:128
本文介绍了使用Autodesk API更新文件版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在能够上传文件并创建文件的初始版本,但无法更新版本.使用本教程的步骤6 中的代码即使将versions:autodesk.core:File替换为versions:autodesk.bim360:File,它也不起作用.从错误消息Request contains 0 includes instead of 1,看来我需要步骤5中示例中的included块的一部分,但是我不确定它的外观.鉴于外部relationships块在第5步和第6步之间的格式不匹配,我假设内部内容也有所不同.

I am now able to upload a file and create an initial version for the file but am unable to update the version. Using the code in Step 6 of this tutorial does not work even after replacing versions:autodesk.core:File with versions:autodesk.bim360:File. From the error message, Request contains 0 includes instead of 1, it seems I need part of the included block from the example in Step 5 but I am not sure what it should look like; given the outer relationships blocks do not match in format between Steps 5 and 6, I am assuming the inner content varies as well.

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
                    }
                }
            }
        }
    }

推荐答案

您的有效载荷是正确的.要创建第二个版本,您必须将请求发送到 CreateVersion端点.

Your payloads are correct. To create the second version, you have to send your request to the CreateVersion endpoint.

    url = '{}data/v1/projects/{}/versions'.format(self.DOMAIN, project_id)

这篇关于使用Autodesk API更新文件版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆