Esempio n. 1
0
    def getEnviroment(self):
        """Gets the id of the environment specified in the TSIClient class constructor.

        Returns:
            str: The environment id.

        Raises:
            TSIEnvironmentError: Raised if the TSI environment does not exist.

        Example:
            >>> from TSIClient import TSIClient as tsi
            >>> client = tsi.TSIClient()
            >>> env = client.getEnviroment()
        """

        authorizationToken = self._getToken()
        url = "https://api.timeseries.azure.com/environments"

        querystring = self._create_querystring()

        payload = ""
        headers = {
            'x-ms-client-application-name': self._applicationName,
            'Authorization': authorizationToken,
            'Content-Type': "application/json",
            'cache-control': "no-cache"
        }

        try:
            response = requests.request("GET",
                                        url,
                                        data=payload,
                                        headers=headers,
                                        params=querystring,
                                        timeout=10)
            response.raise_for_status()
        except requests.exceptions.ConnectTimeout:
            logging.error("TSIClient: The request to the TSI api timed out.")
            raise
        except requests.exceptions.HTTPError:
            logging.error(
                "TSIClient: The request to the TSI api returned an unsuccessfull status code."
            )
            raise

        environments = json.loads(response.text)['environments']
        environmentId = None
        for enviroment in environments:
            if enviroment['displayName'] == self._enviromentName:
                environmentId = enviroment['environmentId']
                break
        if environmentId == None:
            raise TSIEnvironmentError(
                "TSIClient: TSI environment not found. Check the spelling or create an environment in Azure TSI."
            )

        return environmentId
Esempio n. 2
0
    def test_getEnvironments_raises_TSIEnvironmentError(self, requests_mock, client):
        requests_mock.request(
            "POST",
            MockURLs.oauth_url,
            json=MockResponses.mock_oauth
        )
        requests_mock.request(
            "GET",
            MockURLs.env_url,
            exc=TSIEnvironmentError("Azure TSI environment not found. Check the spelling or create an environment in Azure TSI.")
        )

        with pytest.raises(TSIEnvironmentError) as exc_info:
            client.getEnviroment()

        assert "Azure TSI environment not found. Check the spelling or create an environment in Azure TSI." in str(exc_info.value)
    def test_getEnvironments_raises_TSIEnvironmentError(
            self, client, requests_mock):
        requests_mock.request(
            "GET",
            MockURLs.env_url,
            exc=TSIEnvironmentError(
                "Azure TSI environment not found. Check the spelling or create an environment in Azure TSI."
            ),
        )

        with pytest.raises(TSIEnvironmentError) as exc_info:
            client.environment.getEnvironmentId()

        assert (
            "Azure TSI environment not found. Check the spelling or create an environment in Azure TSI."
            in str(exc_info.value))