def download()

in tools/paconn-cli/paconn/operations/download.py [0:0]


def download(powerapps_rp, settings, destination, overwrite):
    """
    Download operation.
    """
    # Prepare folders
    directory = _prepare_directory(
        destination=destination,
        connector_id=settings.connector_id)

    # Check if files could be overwritten
    if not overwrite:
        overwrite = _ensure_overwrite(settings)

    api_registration = powerapps_rp.get_connector(
        environment=settings.environment,
        connector_id=settings.connector_id)

    if _PROPERTIES not in api_registration:
        raise CLIError('Properties not present in the api registration information.')

    api_properties = api_registration[_PROPERTIES]

    # Property whitelist
    property_keys_whitelist = [
        _CONNECTION_PARAMETERS,
        _CONNECTION_PARAMETER_SET,
        _ICON_BRAND_COLOR,
        _SCRIPT_OPERATIONS,
        _CAPABILITIES,
        _POLICY_TEMPLATE_INSTANCES,
        _PUBLISHER,
        _STACKOWNER
    ]

    # Remove the keys that aren't present in the property JSON
    properties_present = list(
        filter(lambda prop: prop in api_properties, property_keys_whitelist)
    )

    # Only output the white listed properties that are present in the property JSON
    api_properties_selected = {_PROPERTIES: {}}
    api_properties_selected[_PROPERTIES] = {
        prop: api_properties[prop]
        for prop in properties_present
    }

    # Write the api properties
    api_prop = format_json(
        content=api_properties_selected,
        sort_keys=False)

    open(
        file=settings.api_properties,
        mode='w'
        ).write(api_prop)

    # Write the open api definition,
    # either from swagger URL when available or from swagger property.
    if _API_DEFINITIONS in api_properties and _ORIGINAL_SWAGGER_URL in api_properties[_API_DEFINITIONS]:
        original_swagger_url = api_properties[_API_DEFINITIONS][_ORIGINAL_SWAGGER_URL]
        response = requests.get(original_swagger_url, allow_redirects=True)
        response_string = response.content.decode('utf-8-sig')

        swagger = format_json(
            content=json.loads(response_string),
            sort_keys=False)

        open(
            file=settings.api_definition,
            mode='w'
            ).write(swagger)

    # Write the icon
    if _ICON_URI in api_properties:
        icon_url = api_properties[_ICON_URI]
        response = requests.get(icon_url, allow_redirects=True)

        open(
            file=settings.icon,
            mode='wb'
            ).write(response.content)

    # Write the script
    if _SCRIPT_URI in api_properties:
        script_url = api_properties[_SCRIPT_URI]
        response = requests.get(script_url, allow_redirects=True)

        open(
            file=settings.script,
            mode='wb'
            ).write(response.content)
    else:
        settings.script = None

    # Save the settings
    write_settings(settings, overwrite)

    return directory