Note:尽管所有这些脚本都已经过测试并发现功能完全正常,但由于大量潜在用例,Anaplan并未明确支持我们客户构建的自定义脚本。本文仅用于信息,不建议将来的任何产品方向。
Python 3提供了许多与API互动的选择。本文将解释如何使用Python 3来自动化我们的养蜂场中可用的许多请求,可以在https://anaplan.docs.apiary.io/#。
This article assumes you have the requests (version 2.18.4), base64, and JSON modules installed, as well as the Python 3 version 3.6.4. Please make sure you are installing these modules with Python 3, and not for an older version of Python. For more information on these modules, please see their respective websites:
Note:使用前,请在每个脚本的顶部阅读评论,因为它们更详细地详细介绍了每个脚本的假设。
首先,让我们谈谈身份验证。每个连接到我们API的脚本运行都将需要提供有效的身份验证。有两种方法可以验证我将要介绍的Python脚本。
证书身份验证将要求您拥有有效的Anaplan证书,您可以阅读更多有关该证书这里。Once you have your certificate saved locally, to properly convert your Anaplan certificate to be usable with the API, first you will needOpenssl。Once you have that, you will need to convert the certificate to PEM format by running the following code in your terminal:
openssl x509 -inform der -in证书 - (certnumber).cer -out certtest.pem
如果您使用证书授权,我们在本文中使用的脚本将假定您知道与证书关联的Anaplan帐户电子邮件。如果您不知道,可以通过在终端中运行以下代码来从PEM文件中提取通用名称(CN):
openssl x509 -text -in certtest.pem
要与API一起使用,PEM证书字符串将需要转换为base64,但是我们将要涵盖的脚本将为您负责,因此我不会在本节中介绍。
要使用基本身份验证,您将需要了解正在使用的Anaplan帐户电子邮件以及密码。本文中的所有脚本将在顶部附近具有以下代码:
#插入使用用户名的Anaplan帐户电子邮件='---------------#如果使用CERT AUTH,请用您的PEM转换证书#文件名替换Cert.PEM。否则,请删除此行。cert = open('cert.pem')。read()#如果使用基本验证,请插入密码。否则,请删除此行。password =''#取消注释您的身份验证方法(证书或基本)。卸下另一个。user ='anaplancertificate' + str(base64.b64encode(((f'{username}:{cert}')。+ str(base64.b64encode(((f'{username}:{password}'#).encode('utf-8'))。解码('utf-8')
Regardless of the authentication method, you will need to set the username variable to the Anaplan account email being used.
If you are using a certificate to authenticate, you will need to have your PEM converted certificate in the same folder or a child folder of the one you are running the scripts from. If your certificate is in a child folder, please remember to include the file path when replacing cert.pem (e.g. cert/cert.pem). You can remove the password line and its comments, and its respective user variable.
如果您使用的是基本身份验证,则需要将密码变量设置为Anaplan帐户密码,并且可以删除CERT行,其注释及其各自的用户变量。
本文涵盖的大多数脚本将要求您知道您正在尝试处理的文件,操作等的ID或元数据。每个获取此信息的脚本都标题为得到_____.py。例如,如果您想获取文件的元数据,您将运行得到Files.py, which will write the file metadata for each file in the selected model, in the selected workspace, in an array to a JSON file titledfiles.json。然后,您可以打开JSON文件,查找您需要引用的文件,并在其他脚本中使用该条目中的元数据。
提示:如果打开JSON文件的原始数据选项卡,则可以更容易复制整个元数据集。
以下是下载的链接get____.py脚本。Each得到脚本使用requests.get将GET请求发送到适当的API端点的方法。
可以将文件上传到Anaplan API端点,无论是块还是单个块。根据我们的养蜂场:
我们建议您将文件上传到几个块中。这使您可以恢复在最终块上传之前失败的上传。此外,您可以在上传操作上压缩文件。我们建议压缩大于50MB的单个块。这将创建一个私有文件。
Note:使用该文件必须存在于Anaplan中的API上传文件。如果该文件以前没有上传,则必须使用Anaplan用户界面最初上传。然后,您可以使用API执行该文件的后续上传。
我们要构建我们要参考的脚本,以便如果由于任何原因中断脚本,或者文件的任何特定块无法上传,则简单地将脚本重新启动,将启动在最后一个成功的片段开始,从而再次上传文件。亚搏彩票手机版免费下载为此,必须使用以下终端脚本使用标准命名约定最初使用标准命名约定进行分配。
split -b [numberOfbytes] [路径和文件名] [输出文件的前缀]
您可以在设置适当的文件路径时将文件存储在任何位置Chunkfileprefix(例如。chunkfileprefix =''upload_chunks/块 - ”This will look for file chunks namedchunk-aa,chunk-ab,chunk-ac等,最多块in the folderscript_origin/upload_chunks/。It is very unlikely that you will ever exceed块)。这将使脚本知道在哪里寻找要上传的文件的块。您可以下载以从此链接运行多个块上传的脚本:chunkupload.py。
Note:这assumed naming conventions will only be standard if using终端,如果使用Windows中的另一种方法将文件分开,则它们不一定可行。如果您使用的是Windows,则需要创建一种方法来标准化块的命名{chunkfileprefix}(aa -zz)或者run the script as detailed in the蜂房。
Note:这chunkupload.py脚本通过将最后一个成功块的名称写入.txt文件来跟踪最后一个成亚搏彩票手机版免费下载功的块Chunkstop.txt。一旦导入成功完成,该文件就会删除。亚搏彩票手机版免费下载如果在脚本的运行之间修改了文件,则脚本可能无法正常运行。最好的做法是,如果您想从第一个块开始上传,则将文件删除并删除。
仅当文件足够小以在合理的时间范围内上传时,才应使用单个块上传。如果上传失败,则必须从一开始就重新开始。如果您的文件具有不同的名称,则其版本的服务器的名称,您将需要修改行31(“姓名” : '') to reflect the name of the local file. This script runs a single put request to the API endpoint to upload the file. You can download the script for running a single chunk upload from this link:singlechunkupload.py
这import.pyscript sends a post request to the API endpoint for the selected import. You will need to set the importData value to the metadata for the import. See获取每个脚本所需的信息了解更多信息。您可以下载脚本running an import from this link:import.py。
Once the import is finished, the script will write the metadata for the import task in an array topostImport.json, which you can use to verify which task you want to view the status of while running theImportStatus.py脚本。这ImportStatus.py脚本将返回与所选expertID及其各自列表索引相关的所有任务的列表。如果您想检查上一次运行导入的状态,请确保您正在检查postImport.json要验证您具有正确的任务。输入任务的索引,脚本将将任务状态写入文件中的数组importStatus.json。如果任务仍在进行中,它将打印任务状态和进度。If the task finished and a failure dump is available, it will write the failure dump in comma delimited format toimportDump.csv可用于审查失败的原因。如果任务没有失败,您将收到一条消息,告诉您导入已经完成而没有失败。您可以下载脚本ImportStatus.py从此链接:ImportStatus.py
Note:如果您使用旧任务ID检查任务的状态以自上次检查以来已运行的导入,则转储将不再存在,并且importDump.csv将被HTTP错误覆盖,任务的状态将为410走了。
这export.pyscript sends a post request to the API endpoint for the selected export. You will need to set the exportData value to the metadata for the export. See获取每个脚本所需的信息了解更多信息。您可以下载以从此链接运行导出的脚本:export.py
Once the export is finished, the script will write the metadata for the export task in an array topostExport.json, which you can use to verify which task you want to view the status of while running theexportStatus.py脚本。这exportStatus.py脚本将返回与所选导出相关的所有任务及其各自列表索引的列表。如果您想检查上一次运行导入的状态,请确保您正在检查postExport.json要验证您具有正确的任务。输入任务的索引,脚本将将任务状态写入文件中的数组exportstatus.json。如果任务仍在进行中,它将打印任务状态和进度。重要的是要注意,如果导出失败,将不会生成故障转储。您可以下载脚本exportStatus.py从此链接:exportStatus.py
这action.py脚本将POST请求发送到所选操作的API端点(用于与进出口以外的其他操作一起使用)。您需要将ActionData值设置为元数据以进行操作。看获取每个脚本所需的信息了解更多信息。您可以下载脚本running an action from this link:ActionStatus.py。
这process.py脚本将发布请求发送到所选过程的API端点。您需要将ProcessData值设置为该过程的元数据。看获取每个脚本所需的信息了解更多信息。您可以下载以从此链接运行过程的脚本:Process.py.
Once the process is finished, the script will write the metadata for the process task in an array topostProcess.json, which you can use to verify which task you want to view the status of while running theProcessStatus.py脚本。这ProcessStatus.py脚本将返回与所选ProcessID及其各自列表索引相关的所有任务的列表。如果您想检查上一次运行导入的状态,请确保您正在检查postProcess.json要验证您具有正确的任务。输入任务的索引,脚本将将任务状态写入文件中的数组processStatus.json。如果任务仍在进行中,它将打印任务状态和进度。If the task finished and a failure dump is available, it will write the failure dump in comma delimited format toprocessDump.csv可用于审查失败的原因。重要的是要注意,只有在该过程中的一个导入失败时,该过程本身不会生成故障转储。如果任务没有失败,您将收到一条消息,告诉您该过程已经完成而没有失败。您可以下载脚本ProcessStatus.py从此链接:ProcessStatus.py。
从Anaplan API端点下载文件将在端点上存在的许多块中下载文件。重要的是要注意,您应该设置变量fileName到姓名它在文件元数据中具有。首先,下载单个块元数据将以一个数组写成下载Chunkdata.json以供参考。然后,脚本将通过块下载文件,并将每个块写入新的本地文件,其名称与文件元数据中列出的“名称”相同。您可以从此链接下载此脚本的链接:downloadFile.py.
Note:如果文件已经与您的脚本相同的文件夹中存在,则名称与姓名在文件的元数据中值,本地文件将被从服务器下载的文件覆盖。
您可以删除用户可以访问Anaplan服务器中的任何文件的文件内容。Note:这只会删除私人内容。默认内容和导入数据源模型对象将保留。您可以从此链接下载此脚本的链接:deletefile.py。
在本节中,我将列出上述每个请求的代码,包括API URL和完成呼叫所需的标题。我将把内容正确授权:标题空白。授权标题值可以是Basic encoded_username: password或者anaplancertificate encoded_commonname:pem_certificate_string(看证书授权 - 使用的anaplan-api有关编码证书的更多信息)
Note:requests.get只会从服务器生成响应主体,除非写入本地文件,否则不会在本地保存任何数据。
requests.get('https://api.anaplan.com/1/3/workspaces/',headers ='授权':)
requests.get('https://api.anaplan.com/1/3/models/',headers = {'授权':})或requests.get('https://api.anaplan.com/1/3/workspaces/{wguid}/models',headers = {'授权':})
requests.get(f'https://api.anaplan.com/1/3/models/ {mguid}',headers = {'授权':})
获取文件,导入,导出,操作或进程的请求在很大程度上是相同的。改变文件进口,导出,操作或流程运行。
requests.get('https://api.anaplan.com/1/3/workspaces/ {wguid}/models/{mguid}/files',headers = {'授权':})
requests.get('https://api.anaplan.com/1/3/workspaces/{wGuid}/models/{mGuid}/files/{fileID}/chunks', headers={'Authorization':})
requests.post('https://api.anaplan.com/1/3/workspaces/ {wguid}/models/{mguid}/files/{fileid}/chunks/chunks/{chunknumber}',headers = {'授权':,'content-type':'application/json'},json = {filemetadata})
requests.put (https://api.anaplan.com/1/3/workspaces/{wGuid}/models/{mGuid}/files/{fileID}/chunks/{chunkNumber}', headers={'Authorization': , 'Content-Type': 'application/octet-stream'}, data={raw contents of local chunk file})
requests.put (https://api.anaplan.com/1/3/workspaces/{wGuid}/models/{mGuid}/files/{fileID}/complete', headers=={'Authorization': , 'Content-Type': 'application/json'}, json={fileMetaData})
requests.put (https://api.anaplan.com/1/3/workspaces/{wGuid}/models/{mGuid}/files/{fileID}', headers={'Authorization': , 'Content-Type': 'application/octet-stream'}, data={raw contents of local file})
进口,出口和流程的邮政请求在很大程度上是相同的。改变进口出口,操作或流程运行。
requests.post('https://api.anaplan.com/1/3/workspaces/ {wguid}/models/{mguid}/imports/imports/imports/{id}/tasks',headers = {'授权':,'-type':'application/json'},data = json.dumps({'localEname':'en_us'}))
requests.post('https://api.anaplan.com/1/3/workspaces/ {wguid}/models/{mguid}/imports/imports/imports/{id}/tasks',data = {'localEname':'en_us':'en_us'},标题= {'授权':,'content-type':'application/json'})
获取导入,导出,操作和过程任务列表的请求基本相同。改变进口出口,操作或进程以获取每个任务列表。
requests.get('https://api.anaplan.com/1/3/workspaces/ {wguid}/models/{mguid}/imports/imports/{impertId}/tasks',headers = {'授权':})
这得到request for import, export, action and process task statuses are largely the same. Change进口出口,操作或进程以获取每个任务列表。Note:只有导入和流程才会产生故障转储。
requests.get('https://api.anaplan.com/1/3/workspaces/ {wguid}/models/{mguid}/imports/imports/{id}/tasks/taskid/{taskid}'headers = {'授权{'授权':}))
Note:对于要下载的文件的每个块,您将需要获取块元数据。
requests.get('https://api.anaplan.com/1/3/workspaces/{wGuid}/models/{mGuid}/files/{fileID}/chunks/{chunkID}, headers={'Authorization': ,'Accept': 'application/octet-stream'})
Note:这只会删除私人内容。默认内容和导入数据源模型对象将保留。
requests.delete('https://api.anaplan.com/1/3/workspaces/{wGuid}/models/{mGuid}/files/{fileID}', headers={'Authorization': , 'Content-type': 'application/json'}
Note:SFDC用户管理is not covered in this article, but the same concepts from the scripts provided can be applied to SFDC user administration. For more information on SFDC user administration see the apiary entry forSFDC用户管理。