Skip to content

Commit

Permalink
gpt : _gpt_get_domains : recursive call to read pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
vhenon committed Sep 13, 2023
1 parent bfb111a commit 1f16d50
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 14 deletions.
2 changes: 1 addition & 1 deletion googlepostmasterapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
__version__ = '0.0.2';
__version__ = '0.0.3';
24 changes: 20 additions & 4 deletions googlepostmasterapi/gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
sys.path.insert ( 0, os.path.dirname ( os.path.dirname ( os.path.abspath ( __file__ ) ) ) );
from googlepostmasterapi.datas import FlatDatas;
from googlepostmasterapi.stats import Stats;
from googlepostmasterapi.utils import write_std;
from googlepostmasterapi.utils import recursive_call, write_std;

class GPostmaster ( object ):
"""Download datas from Google postmaster tools
Expand Down Expand Up @@ -118,13 +118,29 @@ def _init_service ( self, token ):
);


def _gpt_get_domains ( self ):
"""Call GPT to get all domains
def _gpt_get_domains ( self, next_page = None ):
"""Call GPT to get all domains. Recursive call on pagination
Arguments:
next_page (str): Token to get next page of domains. Default : None
Returns:
list: List of dict with all domains, format : [ { 'name': ..., 'createTime': ..., 'permission': ... } ]
"""
return self._service.domains ().list ().execute ();
"""All domains"""
ret = self._service.domains ().list (
pageToken = next_page
).execute ();

if ( 'nextPageToken' in ret ):
"""Domains from next page. Recursive call"""
tmp = recursive_call (
self._gpt_get_domains,
next_page = ret [ 'nextPageToken' ]
);
ret [ 'domains' ] += tmp [ 'domains' ];

return ret;


def get_domains ( self ):
Expand Down
17 changes: 17 additions & 0 deletions googlepostmasterapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ def write_std ( logs ):
"""
for log in logs:
sys.stdout.write ( "{}\n".format ( log ) );


def recursive_call ( method, *args, **kargs ):
"""Method to abstract recursive call
Arguments:
method (string): Mehtod name to call on current instance
args (mixed()): Arguments to send to method
kargs (dict): Keyword arguments to send to method
Returns:
mixed: Method returns
"""
return method (
*args,
**kargs
);
86 changes: 77 additions & 9 deletions tests/gpostmaster/GPostmaster__gpt_get_domainsTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,85 @@ def test_calls ( self ):
with patch ( 'tests.gpostmaster.GPostmaster__gpt_get_domainsTest.RMock.domains', return_value = RMock () ) as domains_call:
with patch ( 'tests.gpostmaster.GPostmaster__gpt_get_domainsTest.RMock.list', return_value = RMock () ) as list_call:
with patch ( 'tests.gpostmaster.GPostmaster__gpt_get_domainsTest.RMock.execute', return_value = 'random-returns' ) as execute_call:
w = GPostmaster (
token = 'random-token'
);
w._service = RMock ();
with patch ( 'googlepostmasterapi.gpt.recursive_call' ) as r_call:
w = GPostmaster (
token = 'random-token'
);
w._service = RMock ();

ret = w._gpt_get_domains ();

self.assertEqual ( ret, 'random-returns' );
domains_call.assert_called_once_with ();
list_call.assert_called_once_with (
pageToken = None
);
execute_call.assert_called_once_with ();
r_call.assert_not_called ();

ret = w._gpt_get_domains ();

def test_arg_next_page ( self ):
with patch ( 'googlepostmasterapi.gpt.GPostmaster._init_resources' ) as init_ressources:
with patch ( 'tests.gpostmaster.GPostmaster__gpt_get_domainsTest.RMock.domains', return_value = RMock () ) as domains_call:
with patch ( 'tests.gpostmaster.GPostmaster__gpt_get_domainsTest.RMock.list', return_value = RMock () ) as list_call:
with patch ( 'tests.gpostmaster.GPostmaster__gpt_get_domainsTest.RMock.execute', return_value = 'random-returns' ) as execute_call:
with patch ( 'googlepostmasterapi.gpt.recursive_call' ) as r_call:
w = GPostmaster (
token = 'random-token'
);
w._service = RMock ();

ret = w._gpt_get_domains (
next_page = 'random-next-page'
);

self.assertEqual ( ret, 'random-returns' );
domains_call.assert_called_once_with ();
list_call.assert_called_once_with (
pageToken = 'random-next-page'
);
execute_call.assert_called_once_with ();
r_call.assert_not_called ();

self.assertEqual ( ret, 'random-returns' );
self.assertEqual ( domains_call.call_count, 1 );
self.assertEqual ( list_call.call_count, 1 );
self.assertEqual ( execute_call.call_count, 1 );

def test_recursive_on_pagination ( self ):
with patch ( 'googlepostmasterapi.gpt.GPostmaster._init_resources' ) as init_ressources:
with patch ( 'tests.gpostmaster.GPostmaster__gpt_get_domainsTest.RMock.domains', return_value = RMock () ) as domains_call:
with patch ( 'tests.gpostmaster.GPostmaster__gpt_get_domainsTest.RMock.list', return_value = RMock () ) as list_call:
with patch ( 'tests.gpostmaster.GPostmaster__gpt_get_domainsTest.RMock.execute' ) as execute_call:
with patch ( 'googlepostmasterapi.gpt.recursive_call' ) as r_call:
execute_call.return_value = {
'nextPageToken': 'random-next-page-token',
'domains': [ 'random-domain-1', 'random-domain-2' ]
};
r_call.return_value = {
'domains': [ 'random-domain-3' ]
};

w = GPostmaster (
token = 'random-token'
);
w._service = RMock ();

ret = w._gpt_get_domains ();

self.assertEqual ( ret, {
'nextPageToken': 'random-next-page-token',
'domains': [
'random-domain-1',
'random-domain-2',
'random-domain-3'
]
} );
domains_call.assert_called_once_with ();
list_call.assert_called_once_with (
pageToken = None
);
execute_call.assert_called_once_with ();
r_call.assert_called_once_with (
w._gpt_get_domains,
next_page = 'random-next-page-token'
);



Expand Down
2 changes: 2 additions & 0 deletions tests/recursive_call/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
36 changes: 36 additions & 0 deletions tests/recursive_call/recursive_callTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os;
import unittest;
from shutil import copyfile;
from unittest.mock import patch, Mock;
from datetime import datetime;

from pprint import pprint ;

from googlepostmasterapi.utils import recursive_call;


def r_mock ( *args, **kargs ):
pass;


class recursive_callTest ( unittest.TestCase ):
def test_calls ( self ):
with patch ( 'tests.recursive_call.recursive_callTest.r_mock', return_value = 'random-returns' ) as f_call:
ret = recursive_call (
r_mock,
'random-arg-1',
'random-arg-2',
another_key_1 = 'another-value_1',
another_key_2 = 'another-value_2'
);

self.assertEqual ( ret, 'random-returns' );

f_call.assert_called_once_with (
'random-arg-1',
'random-arg-2',
another_key_1 = 'another-value_1',
another_key_2 = 'another-value_2'
);
2 changes: 2 additions & 0 deletions tests/write_std/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

0 comments on commit 1f16d50

Please sign in to comment.