def call_aladdin_service(query): version = str(parse_version(core_version)) correlation_id = telemetry_core._session.correlation_id # pylint: disable=protected-access subscription_id = telemetry_core._get_azure_subscription_id() # pylint: disable=protected-access context = { "versionNumber": version, } # Only pull in the contextual values if we have consent if telemetry_core.is_telemetry_enabled(): context['correlationId'] = correlation_id if telemetry_core.is_telemetry_enabled() and subscription_id is not None: context['subscriptionId'] = subscription_id api_url = 'https://app.aladdin.microsoft.com/api/v1.0/examples' headers = {'Content-Type': 'application/json'} response = requests.get(api_url, params={ 'query': query, 'clientType': 'AzureCli', 'context': json.dumps(context) }, headers=headers) return response
def call_aladdin_service(query): version = str(parse_version(core_version)) correlation_id = telemetry_core._session.correlation_id # pylint: disable=protected-access subscription_id = telemetry_core._get_azure_subscription_id() # pylint: disable=protected-access # Used for DDOS protection and rate limiting user_id = telemetry_core._get_user_azure_id() # pylint: disable=protected-access hashed_user_id = hashlib.sha256(user_id.encode('utf-8')).hexdigest() context = { "versionNumber": version, } # Only pull in the contextual values if we have consent if telemetry_core.is_telemetry_enabled(): context['correlationId'] = correlation_id if telemetry_core.is_telemetry_enabled() and subscription_id is not None: context['subscriptionId'] = subscription_id api_url = 'https://app.aladdin.microsoft.com/api/v1.0/examples' headers = {'Content-Type': 'application/json', 'X-UserId': hashed_user_id} response = requests.get(api_url, params={ 'query': query, 'clientType': 'AzureCli', 'context': json.dumps(context), 'commandOnly': True, 'numberOfExamples': 5 }, headers=headers) return response
def call_aladdin_service(query): context = { 'session_id': telemetry_core._session._get_base_properties()['Reserved.SessionId'], # pylint: disable=protected-access 'subscription_id': telemetry_core._get_azure_subscription_id(), # pylint: disable=protected-access 'client_request_id': telemetry_core._session.application.data['headers'] ['x-ms-client-request-id'], # pylint: disable=protected-access 'installation_id': telemetry_core._get_installation_id() # pylint: disable=protected-access } if (query and query.startswith("az ")): query = '"' + query + '"' service_input = { 'paragraphText': "<div id='dummyHeader'></div>", 'currentPageUrl': "", 'query': "ALADDIN-CLI:" + query, 'context': context } api_url = 'https://aladdinservice-staging.azurewebsites.net/api/aladdin/generateCards' headers = {'Content-Type': 'application/json'} response = requests.post(api_url, headers=headers, json=service_input) return response
def call_aladdin_service(query): client_request_id = '' if telemetry_core._session.application: # pylint: disable=protected-access client_request_id = telemetry_core._session.application.data[ 'headers']['x-ms-client-request-id'] # pylint: disable=protected-access session_id = telemetry_core._session._get_base_properties()[ 'Reserved.SessionId'] # pylint: disable=protected-access subscription_id = telemetry_core._get_azure_subscription_id() # pylint: disable=protected-access client_request_id = client_request_id # pylint: disable=protected-access installation_id = telemetry_core._get_installation_id() # pylint: disable=protected-access version = str(parse_version(core_version)) context = { "sessionId": session_id, "subscriptionId": subscription_id, "clientRequestId": client_request_id, "installationId": installation_id, "versionNumber": version } api_url = 'https://app.aladdin.microsoft.com/api/v1.0/examples' headers = {'Content-Type': 'application/json'} response = requests.get(api_url, params={ 'query': query, 'clientType': 'AzureCli', 'context': json.dumps(context), 'commandOnly': True, 'numberOfExamples': 5 }, headers=headers) return response
def get_recommend_from_api(command_list, type, top_num=5, error_info=None): # pylint: disable=unused-argument '''query next command from web api''' import requests url = "https://cli-recommendation.azurewebsites.net/api/RecommendationService" user_id = telemetry._get_user_azure_id() # pylint: disable=protected-access hashed_user_id = hashlib.sha256(user_id.encode('utf-8')).hexdigest() payload = { "command_list": json.dumps(command_list), "type": type, "top_num": top_num, 'error_info': error_info, 'cli_version': version, 'user_id': hashed_user_id } correlation_id = telemetry._session.correlation_id subscription_id = telemetry._get_azure_subscription_id() if telemetry.is_telemetry_enabled(): if correlation_id: payload['correlation_id'] = correlation_id if subscription_id: payload['subscription_id'] = subscription_id response = requests.post(url, json.dumps(payload)) if response.status_code != 200: raise RecommendationError( "Failed to connect to '{}' with status code '{}' and reason '{}'". format(url, response.status_code, response.reason)) recommends = [] if 'data' in response.json(): recommends = response.json()['data'] return recommends
def call_aladdin_service(query): correlation_id = telemetry_core._session.correlation_id # pylint: disable=protected-access subscription_id = telemetry_core._get_azure_subscription_id() # pylint: disable=protected-access version = str(parse_version(core_version)) context = { "correlationId": "", "subscriptionId": "", "versionNumber": version } # Only pull in the other values if we have consent if telemetry_core.is_telemetry_enabled(): context.update(correlationId=correlation_id, subscriptionId=subscription_id) api_url = 'https://app.aladdin.microsoft.com/api/v1.0/examples' headers = {'Content-Type': 'application/json'} response = requests.get(api_url, params={ 'query': query, 'clientType': 'AzureCli', 'context': json.dumps(context), 'commandOnly': True, 'numberOfExamples': 5 }, headers=headers) return response
def call_aladdin_service(query): client_request_id = '' if telemetry_core._session.application: # pylint: disable=protected-access client_request_id = telemetry_core._session.application.data['headers']['x-ms-client-request-id'] # pylint: disable=protected-access context = { 'session_id': telemetry_core._session._get_base_properties()['Reserved.SessionId'], # pylint: disable=protected-access 'subscription_id': telemetry_core._get_azure_subscription_id(), # pylint: disable=protected-access 'client_request_id': client_request_id, # pylint: disable=protected-access 'installation_id': telemetry_core._get_installation_id(), # pylint: disable=protected-access 'version_number': str(parse_version(core_version)) } service_input = { 'paragraphText': "<div id='dummyHeader'></div>", 'currentPageUrl': "", 'query': "ALADDIN-CLI:" + query, 'context': context } api_url = 'https://aladdinservice-prod.azurewebsites.net/api/aladdin/generateCards' headers = {'Content-Type': 'application/json'} response = requests.post(api_url, headers=headers, json=service_input) return response
def call_aladdin_service(command, parameters, version): _log_debug( 'call_aladdin_service: version: "%s", command: "%s", parameters: "%s"', version, command, parameters) response = None correlation_id = telemetry._session.correlation_id # pylint: disable=protected-access subscription_id = telemetry._get_azure_subscription_id() # pylint: disable=protected-access is_telemetry_enabled = telemetry.is_telemetry_enabled() telemetry_context = { 'correlationId': correlation_id, 'subscriptionId': subscription_id } telemetry_context = { k: v for k, v in telemetry_context.items() if v is not None and is_telemetry_enabled } if not is_telemetry_enabled: _log_debug(TELEMETRY_IS_DISABLED_STR) else: _log_debug(TELEMETRY_IS_ENABLED_STR) if subscription_id is None: _log_debug(TELEMETRY_MISSING_SUBSCRIPTION_ID_STR) if correlation_id is None: _log_debug(TELEMETRY_MISSING_CORRELATION_ID_STR) context = {**telemetry_context, "versionNumber": version} query = {"command": command, "parameters": parameters} api_url = 'https://app.aladdin.microsoft.com/api/v1.0/suggestions' headers = {'Content-Type': 'application/json'} try: response = requests.get(api_url, params={ 'query': json.dumps(query), 'clientType': 'AzureCli', 'context': json.dumps(context) }, headers=headers) except RequestException as ex: _log_debug('requests.get() exception: %s', ex) return response
def set_custom_properties(prop, name, value): if name and value is not None: # 10 characters limit for strings prop['{}{}'.format( FIND_EXTENSION_PREFIX, name)] = value[:10] if isinstance(value, str) else value prop['{}{}'.format( FIND_EXTENSION_PREFIX, 'session_id')] = telemetry_core._session._get_base_properties()[ # pylint: disable=protected-access 'Reserved.SessionId'] # pylint: disable=protected-access prop['{}{}'.format( FIND_EXTENSION_PREFIX, 'subscription_id')] = telemetry_core._get_azure_subscription_id() # pylint: disable=protected-access prop['{}{}'.format( FIND_EXTENSION_PREFIX, 'client_request_id')] = telemetry_core._session.application.data[ # pylint: disable=protected-access 'headers']['x-ms-client-request-id'] # pylint: disable=protected-access prop['{}{}'.format( FIND_EXTENSION_PREFIX, 'installation_id')] = telemetry_core._get_installation_id() # pylint: disable=protected-access
def call_aladdin_service(command, parameters, version): _log_debug( 'call_aladdin_service: version: "%s", command: "%s", parameters: "%s"', version, command, parameters) response = None correlation_id = telemetry._session.correlation_id # pylint: disable=protected-access subscription_id = telemetry._get_azure_subscription_id() # pylint: disable=protected-access if subscription_id and correlation_id: context = { "sessionId": correlation_id, "subscriptionId": subscription_id, "versionNumber": version } query = {"command": command, "parameters": parameters} api_url = 'https://app.aladdin.microsoft.com/api/v1.0/suggestions' headers = {'Content-Type': 'application/json'} try: response = requests.get(api_url, params={ 'query': json.dumps(query), 'clientType': 'AzureCli', 'context': json.dumps(context) }, headers=headers) except RequestException as ex: _log_debug('requests.get() exception: %s', ex) else: if subscription_id is None: _log_debug(TELEMETRY_MISSING_SUBSCRIPTION_ID_STR) if correlation_id is None: _log_debug(TELEMETRY_MISSING_CORRELATION_ID_STR) _log_debug(UNABLE_TO_CALL_SERVICE_STR) return response
def call_aladdin_service(query): session_id = telemetry_core._session._get_base_properties()[ 'Reserved.SessionId'] # pylint: disable=protected-access subscription_id = telemetry_core._get_azure_subscription_id() # pylint: disable=protected-access version = str(parse_version(core_version)) context = { "sessionId": session_id, "subscriptionId": subscription_id, "versionNumber": version } api_url = 'https://app.aladdin.microsoft.com/api/v1.0/examples' headers = {'Content-Type': 'application/json'} response = requests.get(api_url, params={ 'query': query, 'clientType': 'AzureCli', 'context': json.dumps(context) }, headers=headers) return response
def _set_aladdin_recommendations(self): """Set recommendations from aladdin service. Call the aladdin service API, parse the response and set the recommendations. """ import hashlib import json import requests from requests import RequestException from http import HTTPStatus from azure.cli.core import __version__ as version api_url = 'https://app.aladdin.microsoft.com/api/v1.0/suggestions' correlation_id = telemetry._session.correlation_id # pylint: disable=protected-access subscription_id = telemetry._get_azure_subscription_id() # pylint: disable=protected-access # Used for DDOS protection and rate limiting user_id = telemetry._get_user_azure_id() # pylint: disable=protected-access hashed_user_id = hashlib.sha256(user_id.encode('utf-8')).hexdigest() headers = { 'Content-Type': 'application/json', 'X-UserId': hashed_user_id } context = { 'versionNumber': version, 'errorType': self._get_error_type() } if telemetry.is_telemetry_enabled(): if correlation_id: context['correlationId'] = correlation_id if subscription_id: context['subscriptionId'] = subscription_id parameters = [ item for item in self.parameters if item not in ['--debug', '--verbose', '--only-show-errors'] ] query = {"command": self.command, "parameters": ','.join(parameters)} response = None try: response = requests.get(api_url, params={ 'query': json.dumps(query), 'clientType': 'AzureCli', 'context': json.dumps(context) }, headers=headers, timeout=1) except RequestException as ex: logger.debug('Recommendation requests.get() exception: %s', ex) telemetry.set_debug_info('AladdinRecommendationService', ex.__class__.__name__) recommendations = [] if response and response.status_code == HTTPStatus.OK: for result in response.json(): # parse the reponse and format the recommendation command, parameters, placeholders = result['command'],\ result['parameters'].split(','),\ result['placeholders'].split('♠') recommendation = 'az {} '.format(command) for parameter, placeholder in zip(parameters, placeholders): recommendation += '{} {} '.format(parameter, placeholder) recommendations.append(recommendation.strip()) self.aladdin_recommendations.extend(recommendations)