コード例 #1
0
ファイル: google.py プロジェクト: steffenmllr/sync-engine
    def get_items(self, sync_from_dt=None, max_results=100000):
        """
        Fetches and parses fresh contact data.

        Parameters
        ----------
        sync_from_dt: datetime, optional
            If given, fetch contacts that have been updated since this time.
            Otherwise fetch all contacts
        max_results: int, optional
            The maximum number of contact entries to fetch.

        Yields
        ------
        ..models.tables.base.Contact
            The contacts that have been updated since the last account sync.

        Raises
        ------
        ValidationError, PermissionsError
            If no data could be fetched because of invalid credentials or
            insufficient permissions, respectively.

        """
        query = gdata.contacts.client.ContactsQuery()
        # TODO(emfree): Implement batch fetching
        # Note: The Google contacts API will only return 25 results if
        # query.max_results is not explicitly set, so have to set it to a large
        # number by default.
        query.max_results = max_results
        if sync_from_dt:
            query.updated_min = datetime.isoformat(sync_from_dt) + 'Z'
        query.showdeleted = True
        google_client = self._get_google_client()
        try:
            results = google_client.GetContacts(q=query).entry
            return [self._parse_contact_result(result) for result in results]
        except gdata.client.RequestError as e:
            # This is nearly always because we authed with Google OAuth
            # credentials for which the contacts API is not enabled.
            self.log.info('contact sync request failure', message=e)
            raise PermissionsError('contact sync request failure')
        except gdata.client.Unauthorized:
            self.log.warning('Invalid access token; refreshing and retrying')
            # Raises an OAuth error if no valid token exists
            with session_scope() as db_session:
                account = db_session.query(GmailAccount).get(self.account_id)
                g_token_manager.get_token_for_contacts(account,
                                                       force_refresh=True)

            # Retry - there must be some valid credentials left
            return self.get_items(self,
                                  sync_from_dt=sync_from_dt,
                                  max_results=max_results)
コード例 #2
0
ファイル: google.py プロジェクト: olofster/inbox
    def get_items(self, sync_from_time=None, max_results=100000):
        """
        Fetches and parses fresh contact data.

        Parameters
        ----------
        sync_from_time: str, optional
            A time in ISO 8601 format: If not None, fetch data for contacts
            that have been updated since this time. Otherwise fetch all contact
            data.
        max_results: int, optional
            The maximum number of contact entries to fetch.

        Yields
        ------
        ..models.tables.base.Contact
            The contacts that have been updated since the last account sync.

        Raises
        ------
        ValidationError, PermissionsError
            If no data could be fetched because of invalid credentials or
            insufficient permissions, respectively.

        """
        query = gdata.contacts.client.ContactsQuery()
        # TODO(emfree): Implement batch fetching
        # Note: The Google contacts API will only return 25 results if
        # query.max_results is not explicitly set, so have to set it to a large
        # number by default.
        query.max_results = max_results
        query.updated_min = sync_from_time
        query.showdeleted = True
        google_client = self._get_google_client()
        try:
            results = google_client.GetContacts(q=query).entry
            return [self._parse_contact_result(result) for result in results]
        except gdata.client.RequestError as e:
            # This is nearly always because we authed with Google OAuth
            # credentials for which the contacts API is not enabled.
            self.log.info('contact sync request failure', message=e)
            raise PermissionsError('contact sync request failure')