FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects

Synchronise Lookup groups.

Merged Dean Rasheed requested to merge issue-10-synchronise-lookup-groups into master
1 unresolved thread
Files
4
+ 50
0
@@ -114,3 +114,53 @@ def list_all_in_list(directory_service, list_cb, *, item_ids=[], id_key='key', b
break
return resources
def get_all_in_list(directory_service, get_cb, *, item_ids=[], id_key='key', batch_size=1000,
retries=2, retry_delay=5, **kwargs):
"""
Wrapper for Google Client SDK get()-style callables, operating on a list of items. Invokes the
"get_cb" Google API method for each item in the "item_ids" list, returning a list resources.
The key used to identify the items in Google is specified by the "id_key" argument.
This is equivalent to calling "get_cb" for each item in the "item_ids" list, and collecting all
the results in a list, except that it uses the Google batch processing API to reduce the number
of API calls.
"""
# Invoke get_cb for each item in the item_ids list, using the batch processing API. The
# Directory API supports a maximum batch size of 1000. See:
# https://developers.google.com/admin-sdk/directory/v1/guides/batch
resources = []
# Batch response handler
def handle_batch_response(request_id, response, exception):
if exception:
raise exception
resources.append(response)
for i in range(0, len(item_ids), batch_size):
batch_item_ids = item_ids[i:i+batch_size]
# Form the batch request
batch = directory_service.new_batch_http_request()
for item_id in batch_item_ids:
request = get_cb(**kwargs, **{id_key: item_id})
batch.add(request, callback=handle_batch_response)
# Process the batch request
while True:
try:
batch.execute()
except HttpError as err:
if (err.resp.status == 503 and retries > 0):
retries -= 1
LOG.warn('503: Service unavailable - retrying')
sleep(retry_delay)
continue
if retries == 0:
LOG.error('503: Service unavailable - retry count exceeded')
raise
break
return resources
Loading