Exporting the Files by API using Python

Hi,

I have around 16 exports to be automated through the python scripts. And I have defined the separate list of files with the file id and file names. Also I have defined separate list of export actions along with the ID and Export names and passing it to a function which will download the file. All the exports has the Chunk count as 0. So will this create a problem when I download the file which is around 30 to 40 MB.

 

I have attached the screenshot of the output where the first export has around two chunks and other are fails because of the 0 chunks. Please suggest any solution of this.

 

Thanks

Regards,

Riyaz Pasha

Best Answer

  • Hi @JaredDolich ,

     

    Thanks for the Quick response. I am using the similar piece of code to download the file. But It works for the first export and fails for the other exports. I have question without running the export can I download the file directly with the API?.

    If this question is answered I think then I will tweak my code and the exports will work.

     

    All the exports have been set to "Myself Keep Private" but still it works for me. So all the export action has to be set to "Everyone"? 

    Currently I have defined all the 16 exports. But for all these exports the processes has been built in the model. I tried to run those process through the API(using Python) but I was not able to download the files. Do the processes need to be handled differently?. If so please could you drop a sample code to run the processes(exports) and download the files.  

     

     

    Regards,

    Riyaz Pasha

Answers

  • @riyazpasha 

    Some thoughts.

    • No issue with Chunks if you're using Python.
    • But you should iterate on the chunks by first querying Anaplan (JSON will be returned) to determine how many chunks are needed. I'll post the code below.
    • Having said that, your issue looks different. It looks like it's not finding that ID.
    • The good news is that the export action and the download action use the same ID (different RESTful endpoints though).
    • Lastly, make sure you have saved the export action as "Everyone". This can revert back to private if you change the saved view and forget to save instead as "everyone"

    # 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))

     

  • When you run an export through the API it saves the result to a file on the server, split into bite-sized chunks. The file is unique to that export and user, and if the same user runs the same export again that file will be replaced. If it is not accessed in 48 hours, the file will be removed and you get a zero chunk count. The file can be located by the ID or name of the export, and this remains the case if the export is part of a process (in your screenshot it looks like you ran the first export again before trying to download the file for the second export).

    The set default file option is only necessary if you need, for example, a skeleton copy with headers etc to be presented to an integration tool for a user who has not recently run the export. This is more useful for import files where tools need to know what columns to include (and for admins maintaining import definitions), and is a one-off operation rather than a setting. If you don't need it, just leave it as-is (private).