Web AppBuilder - From REST API - Python

990
0
02-17-2020 07:18 AM
Pierre-VincentVROT
Esri Contributor

Hello,

I would like to create a WebApp, 'Web Mapping Application', from Python. Unfortunately, there is not a module to run the Web AppBuilder from Python (yet?)...

Consequently, I try to exploit the Portal for ArcGIS REST APIs to add a new object from the base configuration of a WebApp. Unsuccessfully, I fail. Either I'm missing information, or I'm misusing URLs, which is the most likely option.

Here's the order of the calls I'm theoretically trying to make:

  1. GET /portal/apps/webappbuilder/stemapp/predefined-apps/default/config.json
  2. POST /portal/sharing/rest/content/users/{username}/addItem
  3. GET /portal/sharing/rest/content/items/{webappid}?f=json&token={usertoken}
  4. POST /portal/sharing/rest/content/users/pvvrot/items/{webappid}/update

Unfortunately, the second URL seems to fail. Either because I'm editing the wrong parameters, or because I'm missing parameters, or because I'm missing headers. Currently, the server returns a status_code=200 and a completely empty body.

Here is a code snippet

def test_webapp():

    now = datetime.datetime.now()
    nowstr = now.strftime("%Y%m%d%H%M")
    token = get_token_portal()
    bdata = {"f": "json", "token": token}

    webapp_title = f"Test_{nowstr}"
    webapp_tags = ["test"]

    # -------------------------------------------
    # Get default conf
    url = f"{url_portal}/apps/webappbuilder/stemapp/predefined-apps/default/config.json"
    r_conf = requests.get(url=url, data=bdata, verify=False)
    webapp_conf = r_conf.json()

    # -------------------------------------------
    # Change default conf
    webapp_conf["theme"]["sharedTheme"] = {
        "isPortalSupport": True,
        "useHeader": False,
        "useLogo": False
    }
    webapp_conf["portalUrl"] = url_portal
    webapp_conf["authorizedCrossOriginDomains"] = [portal_fqdn]
    webapp_conf["title"] = webapp_title
    webapp_conf["geometryService"] = f"{url_server}/rest/services/Utilities/Geometry/GeometryServer"
    # Topographie
    webapp_conf["map"]["itemId"] = "b77129664c034fc2b21bc3ea9e8d3300"
    webapp_conf["map"]["mapOptions"] = {
        "extent": {
            "xmin": -18000000,
            "ymin": -12000000,
            "xmax": 18000000,
            "ymax": 16000000,
            "spatialReference": {
                "wkid": 102100
            }
        }
    }  # from mapProperties ?
    webapp_conf["isWebTier"] = False
    webapp_conf["httpProxy"] = {
        "useProxy": True,
        "url": f"//{portal_fqdn}/sharing/proxy"
    }

    # -------------------------------------------
    # Send add item
    url = f"{url_portal}/sharing/rest/content/users/{username}/addItem"
    strtags = ",".join([x for x in webapp_tags])
    desc = webapp_title.replace("_", " ")
    data = {
        "f": "json",
        "token": token,
        "title": webapp_title,
        "type": "Web Mapping Application",
        "typeKeywords": f"Web AppBuilder, Ready To Use, Python, WAB2D",
        "text": str(webapp_conf).replace('"', '\\"'),
        "snippet": desc,
        "tags": strtags,
    }
    r_newwebapp = requests.post(url=url, data=data, verify=False)

    print(r_newwebapp.status_code)
    print(dict(r_newwebapp.headers))
    print(r_newwebapp.text)

Results

'{r_newwebapp.status_code}'
200
'{r_newwebapp.headers}'
{   'Connection': 'Keep-Alive',
    'Content-Length': '0',
    'Content-Type': 'unknown/unknown',
    'Date': 'Mon, 17 Feb 2020 14:43:24 GMT',
    'Keep-Alive': 'timeout=5, max=100',
    'Server': 'Apache/2.4.41 (Win64) OpenSSL/1.1.1c'}
'{r_newwebapp.text}'
''

Is there anyone who can help me?

Thank you in advance,

Pierre

0 Replies