File download issue in running export with python API

Hi Everyone,


I have been trying to run an export action and a process containing the export action using python API for Anaplan. The output shows python API running the export perfectly but no file gets downloaded. Can anyone suggest the reason for the same and if any workaround is available for this issue ?

 

I need the user to enter the data in module in Anaplan and need the python script whenever run to be able to pick the latest copy of the data from the module.

 

Any help would be appreciated.

 

Regards,

Aakash Sachdeva

Best Answer

  • Hey @aakash,

     

    Not 100% sure what you have included in your code but hopefully this will get you in the right direction.

    With Imports and Exports with the API you have to conduct multiple steps. Running the export will start that export task in Anaplan and return a task ID. You can use that to monitor the task to confirm that it has completed. Once it has completed you need to confirm the number of chunks to then get that chunk of data. The API includes these additional steps within the Export Actions and Download Files for Actions section.

     

    https://anaplanbulkapi20.docs.apiary.io/#reference/export-actions

Answers

  • hi Johndorma,
    Thanks a lot. I didn't know about the steps involved after getting the task ID. I followed your advice and my file is downloading perfectly now.
    For anyone struggling with python API to do the same, following steps are involved,
    1. Get Task ID
    2. Check Status of Task
    3. Download the file.

    Regards,
    Aakash Sachdeva
  • Hi @aakash ,

    I'm also facing the same issue. Can I please look at your code(dummy) to understand how you have done it so that I can create the same type of function to retrieve the file.

     

    Regards,

    Riyaz Pasha

  • Adding @aakash for visibility to @leomachiel question.

    The steps I use for Python are:

    • Get ID of export action
    • Run the export action
    • Get the chunk value
    • Download the export file

    Python Script using Basic Authentication and automating the chunk value:

    # This script downloads a file in chunks. It will write all chunks to a newly
    # created local file with the same name as the file.
    import requests
    import base64
    import sys
    import json
    from getAuthentication import userBA
    from getGUIDs import wGuid, mGuid, fileID, fileName

    url = (f'https://api.anaplan.com/1/3/workspaces/{wGuid}/models/{mGuid}/' +
    f'files/{fileID}/chunks')

    getHeaders = {
    'Authorization': userBA
    }

    downloadHeaders = {
    'Authorization': userBA,
    'Accept': 'application/octet-stream'
    }

    getChunkData = requests.get(url,
    headers=getHeaders)
    with open('downloadChunkData.json', 'wb') as f:
    f.write(getChunkData.text.encode('utf-8'))

    with open('downloadChunkData.json', 'r') as f:
    f2 = json.load(f)

    with open(f'{fileName}', 'wb') as f:
    for i in f2:
    chunkData = i
    chunkID = i['id']
    print(f'Getting chunk {chunkID}')
    getChunk = requests.get(url + f'/{chunkID}',
    headers=downloadHeaders)
    f.write(getChunk.content)
    print('Status code: ' + str(getChunk.status_code))

     

  • Hi @leomachiel ,

     

    Apologies for late reply. Unfortunately I don't have a code available at this moment, however one observation can be useful. Apparently there is a script provided to run an export using API, but running the export alone wouldn't download the file, the export will give you a taskID , you can view the task ID , which will later be required to run another script to download the required file.

    Hope that helps.

    Regards,

    Aakash