def download(powerapps_rp, settings, destination): """ Download operation. """ # Prepare folders directory = _prepare_directory( destination=destination, connector_id=settings.connector_id) 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] # Save the settings SettingsSerializer.to_json(settings, 'settings.json') # Property whitelist property_keys_whitelist = [ _CONNECTION_PARAMETERS, _ICON_BRAND_COLOR, _CAPABILITIES, _POLICY_TEMPLATE_INSTANCES ] # 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 open(settings.api_properties, mode='w').write(format_json(api_properties_selected)) # 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(json.loads(response_string)) open(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(settings.icon, mode='wb').write(response.content) return directory
def to_json(settings, filename): """ Serializes a settings object into the settings.json """ settings_dict = SettingsSerializer.serialize(settings) json_str = format_json(settings_dict) open(filename, 'w').write(json_str)
def to_json_string(settings): """ Serializes a settings object into string """ settings_dict = SettingsSerializer.serialize(settings) json_str = format_json(settings_dict) return json_str
def request(self, verb, endpoint, headers=None, payload=None): """ Send a request to the given url """ all_headers = {} if self.credentials: token_type = self.credentials[_TOKEN_TYPE] token = self.credentials[_ACCESS_TOKEN] all_headers = { 'Authorization': '{token_type} {token}'.format(token_type=token_type, token=token) } if headers: all_headers.update(headers) response = requests.request(verb, endpoint, headers=all_headers, json=payload) try: response.raise_for_status() except requests.exceptions.HTTPError as exception: exception_str = str(exception) response_content = json.loads(response.content) response_content = format_json(response_content) if payload: LOGGER.debug('PAYLOAD') LOGGER.debug(payload) LOGGER.debug('RESPONSE') LOGGER.debug(response_content) display(response_content) raise CLIError(exception_str) return response
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 ] # 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