<commit_before>#!/usr/bin/env python3
#
# Copyright 2016 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import requests
import errors
import json
from datetime import datetime

"""A library for interacting with the ThousandEyes API.

To begin using this library, instantiate an object from class Api with the constructor arguments api_url,
username & password.

E.g.: te_api = thousandeyes.Api("https://api.thousandeyes.com", "myusername", "mypassword")
"""
__author__ = "<NAME>"
__copyright__ = "Copyright 2016 <NAME>"
__license__ = "Apache 2.0"
__title__ = "ThousandEyes"
__version__ = "1.0"


class Api(object):
    """ThousandEyes API class.

    Contains all of the relevant API functions for ThousandEyes.

    Attributes:
        api_request: A thousandeyes.ApiRequest instance.
    """
    def __init__(self, api_request=None):
        if api_request is None:
            raise errors.Error("[%s.%s] - You must provide a ThousandEyes APIRequest instance."
                               % (__name__, self.__class__.__name__))
        else:
            self.api_request = api_request

    def status(self):
        """Fetches the current status of the ThousandEyes API.

        Returns:
            A boolean of True if the service is ok and False if not.
        """
        response = self.api_request.get("/status.json")

        if response.status_code == 200:
            return True
        else:
            return False

    def get_network_tests(self):
        """Fetches the current list of network tests from the ThousandEyes API.

        All network tests are returned from Thousand Eyes as Json. The Json response is then split into
        separate tests, decoded and mapped into individual thousandeyes.NetworkTest objects. These objects are then
        added to a list for return to the caller.

        Returns:
            A list of thousandeyes.NetworkTest instances.
        """
        response = self.api_request.get("/tests/network.json")
        tests = json.loads(response.text)
        test_list = []

        for test in tests["test"]:
            # Instantiate a new NetworkTest instance and pass current test for json decoding.
            new_test = NetworkTest()
            new_test.from_json(test)

            # Add the newly populated NetworkTest instance to the list.
            test_list.append(new_test)

        return test_list

    def create_network_test(self, test=None):
        """Creates a new network test on ThousandEyes from the passed instance.

        Args:
            test = A populated thousandeyes.NetworkTest instance.

        Returns:
            A a boolean indicating success (True) or failure (False).
        """
        if test is None:
            raise errors.Error("[%s.%s] - You must provide a populated ThousandEyes test instance to create."
                               % (__name__, self.__class__.__name__))
        else:
            response = self.api_request.post("/tests/network/new.json", test.to_json())

            if response.status_code == 201:
                return True
            else:
                return False

    def delete_network_test(self, test_id=None):
        """Deletes the specified network test from ThousandEyes.

        Args:
            test_id = The test id that should be deleted.

        Returns:
            A a boolean indicating success (True) or failure (False).
        """
        if test_id is None:
            raise errors.Error("[%s.%s] - You must provide a ThousandEyes test id to delete."
                               % (__name__, self.__class__.__name__))
        else:
            response = self.api_request.delete("/tests/network/%s/delete.json" % test_id)

            if response.status_code == 204:
                return True<commit_msg>Updated to support version 6 of the ThousandEyes API<commit_after>#!/usr/bin/env python3
#
# Copyright 2016 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import requests
import errors
import json
from datetime import datetime

"""A library for interacting with the ThousandEyes API.

To begin using this library, instantiate an object from class Api with the constructor arguments api_url,
username & password.

E.g.: te_api = thousandeyes.Api("https://api.thousandeyes.com", "myusername", "mypassword")
"""
__author__ = "<NAME>"
__copyright__ = "Copyright 2016 <NAME>"
__license__ = "Apache 2.0"
__title__ = "ThousandEyes"
__version__ = "1.0"


class Api(object):
    """ThousandEyes API class.

    Contains all of the relevant API functions for ThousandEyes.

    Attributes:
        api_request: A thousandeyes.ApiRequest instance.
    """
    def __init__(self, api_request=None):
        if api_request is None:
            raise errors.Error("[%s.%s] - You must provide a ThousandEyes APIRequest instance."
                               % (__name__, self.__class__.__name__))
        else:
            self.api_request = api_request

    def status(self):
        """Fetches the current status of the ThousandEyes API.

        Returns:
            A boolean of True if the service is ok and False if not.
        """
        response = self.api_request.get("/status.json")

        if response.status_code == 200:
            return True
        else:
            return False

    def get_network_tests(self):
        """Fetches the current list of network tests from the ThousandEyes API.

        All network tests are returned from Thousand Eyes as Json. The Json response is then split into
        separate tests, decoded and mapped into individual thousandeyes.NetworkTest objects. These objects are then
        added to a list for return to the caller.

        Returns:
            A list of thousandeyes.NetworkTest instances.
        """
        response = self.api_request.get("/tests/agent-to-server.json")
        tests = json.loads(response.text)
        test_list = []

        for test in tests["test"]:
            # Instantiate a new NetworkTest instance and pass current test for json decoding.
            new_test = NetworkTest()
            new_test.from_json(test)

            # Add the newly populated NetworkTest instance to the list.
            test_list.append(new_test)

        return test_list

    def create_network_test(self, test=None):
        """Creates a new network test on ThousandEyes from the passed instance.

        Args:
            test = A populated thousandeyes.NetworkTest instance.

        Returns:
            A a boolean indicating success (True) or failure (False).
        """
        if test is None:
            raise errors.Error("[%s.%s] - You must provide a populated ThousandEyes test instance to create."
                               % (__name__, self.__class__.__name__))
        else:
            response = self.api_request.post("/tests/agent-to-server/new.json", test.to_json())

            if response.status_code == 201:
                return True
            else:
                return False

    def delete_network_test(self, test_id=None):
        """Deletes the specified network test from ThousandEyes.

        Args:
            test_id = The test id that should be deleted.

        Returns:
            A a boolean indicating success (True) or failure (False).
        """
        if test_id is None:
            raise errors.Error("[%s.%s] - You must provide a ThousandEyes test id to delete."
                               % (__name__, self.__class__.__name__))
        else:
            response = self.api_request.delete("/tests/agent-to-server/%s/delete.json" % test_id)

            if response.status_code == 204:
                return True
{'fraction_non_alphanumeric': 0.08008332368938781, 'fraction_numerical': 0.006133549357713227, 'mean_word_length': 2.3251250480954213, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7342742', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2217, 'n_words': 847}