Ejemplo n.º 1
0
 def setUp(self):
     self.url_builder = UrlBuilder(
         api_root=TEST_API_ROOT,
         url_tail=TEST_URL_TAIL,
         api_version=TEST_API_VERSION_NUMBER,
         username=TEST_USERNAME,
         password=TEST_PASSWORD,
     )
Ejemplo n.º 2
0
class UrlBuilderTestCase(TestCase):
    def setUp(self):
        self.url_builder = UrlBuilder(
            api_root=TEST_API_ROOT,
            url_tail=TEST_URL_TAIL,
            api_version=TEST_API_VERSION_NUMBER,
            username=TEST_USERNAME,
            password=TEST_PASSWORD,
        )

    def test__calculate_sha1(self):
        sha1 = UrlBuilder._calculate_sha1('Hello World')

        self.assertEqual(sha1, '0a4d55a8d778e5022fab701977c5d840bbc486d0')

    def test___get_url_tail(self):
        url = 'http://example.com/?a=5&b=123'
        url_tail = UrlBuilder._get_url_tail(url)
        expected_url_tail = '?a=5&b=123'

        self.assertEqual(url_tail, expected_url_tail)

    def test__hash_the_url(self):
        url = "http://api.statcounter.com/stats/?vn=3&s=visitor&f=json&pi=2292634&n=10&t=1504534122&u=demo_user"
        expected_url_with_sha1 = 'http://api.statcounter.com/stats/?vn=3&s=visitor&f=json&pi=2292634&n=10&t=1504534122&u=demo_user&sha1=71797cacaab926ab028917652d986159d571056b'

        self.assertEqual(self.url_builder._hash_the_url(url),
                         expected_url_with_sha1)

    def test_build_method(self):
        # Python Dict doesn't preserve order, so we use OrderedDict here:
        params = OrderedDict([
            ('s', 'visitor'),
            ('f', 'json'),
            ('pi', 2292634),
            ('n', 10),
            ('t', 1504534122),
        ])

        result_url = self.url_builder.build(params)

        expected_url = "http://api.statcounter.com/stats/?vn=3&s=visitor&f=json&pi=2292634&n=10&t=1504534122&u=demo_user&sha1=71797cacaab926ab028917652d986159d571056b"

        self.assertEqual(result_url, expected_url,
                         "Url was not built properly!")

    def test_multiple_pi_parameter_support(self):
        params = {'pi': [11, 22, 33]}

        result_url = self.url_builder.build(params)

        self.assertIn('&pi=11&pi=22&pi=33', result_url)
Ejemplo n.º 3
0
    def __init__(self, username: str, api_password: str):

        self._url_builder = UrlBuilder(
            api_root=conf.API_ROOT,
            url_tail=self._url_tail,
            username=username,
            password=api_password,
            api_version=conf.API_VERSION_NUMBER,
        )
Ejemplo n.º 4
0
    def retrieve_projects_details(self):
        """
        Retrieve details for ALL projects.

        Docs: http://statcounter.com/api/docs/v3#user-projects
        """
        url_builder = UrlBuilder(
            api_root=conf.API_ROOT,
            url_tail='user_projects/',
            username=self._username,
            password=self._password,
            api_version=conf.API_VERSION_NUMBER,
        )

        url = url_builder.build({})

        response = requests.get(url)
        response.raise_for_status()
        return response.json()['sc_data']
Ejemplo n.º 5
0
    def retrieve_log_size(self):
        """
        Retrieve Account Log Sizes.

        Docs: http://statcounter.com/api/docs/v3#retrieve-logsizes
        """
        url_builder = UrlBuilder(
            api_root=conf.API_ROOT,
            url_tail='account_logsizes/',
            username=self._username,
            password=self._password,
            api_version=conf.API_VERSION_NUMBER,
        )

        url = url_builder.build({})

        response = requests.get(url)
        response.raise_for_status()

        return response.json()['sc_data']
Ejemplo n.º 6
0
    def create(self,
               website_title: str,
               website_url: str,
               public_stats_level: int = 1,
               time_zone: str = 'America/New_York') -> SCProjectData:
        """
        Create a new project.

        Docs: http://statcounter.com/api/docs/v3#create-project

        @public_stats_level:
            0: All public stats are disabled
            1: All stats are public
            2: Only 'Summary Stats' are public

        @time_zone: http://statcounter.com/api/docs/v3#timezone
        """
        url_builder = UrlBuilder(
            api_root=conf.API_ROOT,
            url_tail='add_project/',
            username=self._username,
            password=self._password,
            api_version=conf.API_VERSION_NUMBER,
        )
        params = {  # website_title and website_url should be urlencoded:
            'wt': quote(website_title),
            'wu': quote(website_url),
            'ps': public_stats_level,
            'tz': quote(time_zone),
        }

        url = url_builder.build(params)

        response = requests.get(url)
        response.raise_for_status()

        data = response.json()['sc_data'][0]
        return SCProjectData(
            project_id=data['project_id'],
            security_code=data['security_code'],
        )
Ejemplo n.º 7
0
    def increase_log_size(self, project_id: int, logsize: int) -> None:
        """
        Increase Project Log Size.

        Docs: http://statcounter.com/api/docs/v3#increase-logsize
        """
        url_builder = UrlBuilder(
            api_root=conf.API_ROOT,
            url_tail='update_logsize/',
            username=self._username,
            password=self._password,
            api_version=conf.API_VERSION_NUMBER,
        )
        params = {
            'pi': project_id,
            'ls': logsize,
        }

        url = url_builder.build(params)

        response = requests.get(url)
        response.raise_for_status()
Ejemplo n.º 8
0
    def retrieve_selected_project_details(self, project_id: int):
        """
        Retrieve details for particular user's project.

        Docs: http://statcounter.com/api/docs/v3#select-project
        """
        url_builder = UrlBuilder(
            api_root=conf.API_ROOT,
            url_tail='select_project/',
            username=self._username,
            password=self._password,
            api_version=conf.API_VERSION_NUMBER,
        )
        params = {
            'pi': project_id,
        }

        url = url_builder.build(params)

        response = requests.get(url)
        response.raise_for_status()

        return response.json()['sc_data']
Ejemplo n.º 9
0
    def test___get_url_tail(self):
        url = 'http://example.com/?a=5&b=123'
        url_tail = UrlBuilder._get_url_tail(url)
        expected_url_tail = '?a=5&b=123'

        self.assertEqual(url_tail, expected_url_tail)
Ejemplo n.º 10
0
    def test__calculate_sha1(self):
        sha1 = UrlBuilder._calculate_sha1('Hello World')

        self.assertEqual(sha1, '0a4d55a8d778e5022fab701977c5d840bbc486d0')