# -*- coding: utf-8 -*-"""This module provides a core batch retrieval engine for the Gmail API,enabling efficient fetching of multiple messages or threads in a single requestto minimize HTTP overhead and improve performance."""importtypingasTtry:frommore_itertoolsimportbatchedexceptImportErrorase:fromitertoolsimportbatchedifT.TYPE_CHECKING:fromgoogleapiclient._apis.gmail.v1importGmailResource
[docs]defbatch_get(gmail:"GmailResource",method:T.Callable,ids:list[T.Any],id_arg_name:str,batch_size:int=50,kwargs:dict[str,T.Any]|None=None,):""" Core batch retrieval engine for Gmail API get methods. Provides the underlying batch mechanism used by :func:`~pythonic_gmail.client.batch_get_messages` and :func:`~pythonic_gmail.client.batch_get_threads`. Groups multiple API calls into efficient batch requests to reduce HTTP overhead. :param gmail: Gmail API client resource :param method: Gmail API method that returns a Resource for execution :param ids: List of IDs to retrieve (message IDs or thread IDs) :param id_arg_name: Parameter name for the ID in the API method :param batch_size: Number of items to fetch per batch request :param kwargs: Additional parameters for the API call :returns: List of retrieved objects from the API responses **Examples**: Direct usage with messages get API:: messages = batch_get( gmail=gmail_client, method=gmail_client.users().messages().get, ids=["msg1", "msg2", "msg3"], id_arg_name="id", batch_size=2, kwargs={"userId": "me", "format": "minimal"} ) Direct usage with threads get API:: threads = batch_get( gmail=gmail_client, method=gmail_client.users().threads().get, ids=["thread1", "thread2", "thread3"], id_arg_name="id", batch_size=2, kwargs={"userId": "me", "format": "minimal"} ) .. note:: This is a low-level function. Most users should use :func:`~pythonic_gmail.client.batch_get_messages` or :func:`~pythonic_gmail.client.batch_get_threads` instead. Uses Gmail's batch API for efficient retrieval. See the `batch guide <https://developers.google.com/workspace/gmail/api/guides/batch>`_ for more details. .. seealso:: :func:`~pythonic_gmail.client.batch_get_messages` and :func:`~pythonic_gmail.client.batch_get_threads` for high-level interfaces. """ifkwargsisNone:kwargs={}items=list()defcallback(request_id,response,exception,):ifexceptionisnotNone:print(f"Error for request {request_id}: {exception}")else:items.append(response)forsub_idsinbatched(ids,batch_size):batch_request=gmail.new_batch_http_request()foridinsub_ids:kwargs[id_arg_name]=idbatch_request.add(method(**kwargs),callback=callback,)batch_request.execute()returnitems