async def test_result_returned_successfully(self): """Testing for a successful return of a result from Cdm Http Client.""" github_url = 'https://raw.githubusercontent.com/' corpus_path = '/Microsoft/CDM/master/schemaDocuments/foundations.cdm.json' cdm_http_client = CdmHttpClient(github_url) cdm_http_request = CdmHttpRequest(corpus_path, 1, 'GET') cdm_http_request.timeout = 5000 cdm_http_request.maximum_timeout = 10000 cdm_http_request.headers = {'User-Agent': 'CDM'} cdm_http_response = await cdm_http_client._send_async(cdm_http_request, self.call_back) self.assertEqual(200, cdm_http_response.status_code)
async def test_number_of_retries_exceeded_exception(self): """Testing for a failed return of a result back from Cdm Http Client.""" github_url = 'https://raw.githubusercontent2.com/' corpus_path = '/Microsoft/CDM/master/schemaDocuments/foundations.cdm.json' cdm_http_client = CdmHttpClient(github_url) cdm_http_request = CdmHttpRequest(corpus_path, 1, 'GET') cdm_http_request.timeout = 5000 cdm_http_request.maximum_timeout = 10000 cdm_http_request.headers = {'User-Agent': 'CDM'} try: await cdm_http_client._send_async(cdm_http_request, self.call_back) except Exception as e: self.assertRaises(CdmNumberOfRetriesExceededException)
def _set_up_cdm_request(self, path: str, headers: Optional[Dict], method: str) -> 'CdmHttpRequest': request = CdmHttpRequest(path) request.headers = headers or {} request.method = method request.timeout = self.timeout request.maximum_timeout = self.maximum_timeout request.number_of_retries = self.number_of_retries return request
async def post_kusto_query(self, query: str) -> None: """ Get an authorization token and send the query to Kusto. :param query: The Kusto query command to be posted to the cluster. """ auth_token = self._config._get_authentication_token() query_endpoint = 'https://{0}.kusto.windows.net/v1/rest/mgmt'.format( self._config.kusto_cluster_name) kusto_host = '{0}.kusto.windows.net'.format( self._config.kusto_cluster_name) query_body = '{{"db":"{0}","csl":"{1}"}}'.format( self._config.kusto_database_name, query) headers = { 'Accept': 'application/json', 'Authorization': auth_token, 'Host': kusto_host } # type: Dict[str, str] http_request = CdmHttpRequest(query_endpoint) http_request.method = 'POST' http_request.headers = headers http_request.content = query_body http_request.content_type = 'application/json' http_request.number_of_retries = self.max_num_retries http_request.timeout = self.timeout_milliseconds http_request.maximum_timeout = self.max_timeout_milliseconds response = await self._http_client._send_async( http_request, self._get_retry_wait_time, self._ctx) if response is None: raise Exception( 'Kusto query post failed. The result of a request is undefined.' ) if not response.is_successful: raise Exception('Kusto query post failed. HTTP {0} - {1}.'.format( response.status_code, response.reason))