Skip to content

Commit

Permalink
Handle pagination in API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jrdnbradford committed Oct 8, 2024
1 parent b9c8905 commit 8a60688
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions oauthenticator/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ async def _fetch_member_groups(
self,
member_email,
user_email_domain,
http=None,
checked_groups=None,
processed_groups=None,
credentials=None,
Expand All @@ -420,19 +419,26 @@ async def _fetch_member_groups(

headers = {'Authorization': f'Bearer {token}'}
url = f'https://www.googleapis.com/admin/directory/v1/groups?userKey={member_email}'
group_data = await self.httpfetch(
url, headers=headers, label="fetching google groups"
)

member_groups = {
g['email'].split('@')[0]
for g in group_data.get('groups', [])
if g.get('email')
}
self.log.debug(f"Fetched groups for {member_email}: {member_groups}")
member_groups = set()
while url:
response_data = await self.httpfetch(
url, headers=headers, label="fetching google groups"
)

groups = response_data.get('groups', [])
group_names = {g['email'].split('@')[0] for g in groups if g.get('email')}
member_groups.update(group_names)
self.log.debug(f"Fetched groups for {member_email}: {member_groups}")

next_page_token = response_data.get('nextPageToken')
if next_page_token:
url = f'https://www.googleapis.com/admin/directory/v1/groups?userKey={member_email}&pageToken={next_page_token}'
else:
url = None

checked_groups.update(member_groups)
self.log.debug(f"Checked groups after update: {checked_groups}")
self.log.debug(f"{member_email} is a direct member of groups: {member_groups}")

if self.include_nested_groups:
for group in member_groups:
Expand All @@ -442,7 +448,6 @@ async def _fetch_member_groups(
nested_groups = await self._fetch_member_groups(
f"{group}@{user_email_domain}",
user_email_domain,
http,
checked_groups,
processed_groups,
)
Expand Down

0 comments on commit 8a60688

Please sign in to comment.