"Import zip error: please refresh page and try again". [Python] [rest]

167
0
12-30-2021 02:58 AM
by Anonymous User
Not applicable
Hello everyone !
I'm trying to migrate a french's WebAppBuilder (arcOpoleBuilder) from our dev's portal to our prod's portal. I'm succesfully zip the app but, when I'm sending the zip archive with requests, I'm encountered an issue : "Import zip error: please refresh page and try again."

However, when I'm sending the archive with arcopole Builder, this one works.

I've download winmerge to compare the zip that i've created in python and the zip who is uploaded with the archive(who is, finally, the same) and this is not totally the same : Winmerge

Here is my jupyter notebook code :

 

from arcgis.gis import GIS
from getpass import getpass
import requests
import json
import os
from zipfile import *
import urllib
import tempfile
import shutil
import urllib.parse
import os

origine = GIS('https://xxxxxxx.fr/portal', 'xxxxxxx', "xxxxxxx", verify_cert=False)
destination = GIS('https://xxxxxxx.fr/portal', 'xxxxxxx', "xxxxxxx", verify_cert=False)
url = r"{}/webappbuilder/rest/apps/list?portalUrl={}".format(origine.url.replace("/portal", ""), origine.url)
params = {'portalUrl': origine.url, \
                  'token': origine._con.token, \
                  'f': 'json'}
reponseBrut = requests.get(url, data = params, verify = False)
reponse = json.loads(reponseBrut.text)
for appli in reponse:
    nom_appli = "TEST-WAB"
    if (appli['name'] == nom_appli):
        app = appli
        print (app['name'])
        print (app['id'])
url = r"{}/webappbuilder/rest/apps/{}/download".format(origine.url.replace("/portal", ""), 

app['id'])
params = {'token': origine._con.token, \
          'f': 'files'}
reponse = requests.get(url, data = params, verify = False)
f = open(app['name']+".zip", "wb")
f.write(reponse.content)
f.close()
with tempfile.TemporaryDirectory() as repertoireTemporaire:
print (app['id'])
cheminFichier = repertoireTemporaire + "//" + app['name'] + ".zip"
cheminFichier_TMP = repertoireTemporaire + "//tmp.zip"
#print (cheminFichier)
with ZipFile (app['name']+".zip", 'r') as zipOrigine:
    with zipOrigine.open('config.json') as fichierConfig:
        jsonConfig = json.load(fichierConfig)           
        if("portalUrl" in jsonConfig):
            jsonConfig["portalUrl"] = destination.url
            print (jsonConfig["portalUrl"])
                       
            if("map" in jsonConfig):
                map = jsonConfig["map"]
                if("portalUrl" in map):
                    jsonConfig["map"]["portalUrl"] = destination.url + "/"

                    if("itemId" in map):
                        print (jsonConfig["map"]["itemId"])
                        jsonConfig["map"]["itemId"] = "bf550f0249ae4a44baf40792b5c6da9b"
                        print (jsonConfig["map"]["itemId"])
                            
                    if("geometryService" in jsonConfig):
                        jsonConfig["geometryService"] = destination.properties['helperServices']['geometry']['url']
                        print (jsonConfig["geometryService"])
                    with ZipFile(cheminFichier_TMP, 'w') as zipDestination:
                        for fichier in zipOrigine.infolist():
                            data = None
                            if (fichier.filename not in 'config.json'):
                                data = zipOrigine.read(fichier.filename)
                            else:
                                data = json.dumps(jsonConfig, indent=4)
                                #print (data)
                            zipDestination.writestr(fichier, data)

    shutil.move(cheminFichier_TMP, cheminFichier)
    url = r"{}/webappbuilder/rest/apps/upload".format(destination.url.replace("/portal", ""))
    params = {'token': destination._con.token, \
              'f': 'json', \
              'creator': destination.properties["user"]["username"], \
              'portalUrl': 'https://xxxxx.fr'}
    fichier = open(cheminFichier, "rb")
    reponse = requests.post(url, data = params, files = {'file': fichier}, verify = False)
    fichier.close()
    print(reponse.text)

 

 and the output :

 

Import zip error: please refresh page and try again.

 

Any help will be appreciated.

Have a good holiday everyone.

Cheers !

Kant1.dro

Tags (3)
0 Kudos
0 Replies