Пример #1
0
    def get_messages(self, limit=25, *, query=None, order_by=None, batch=None, download_attachments=False):
        """
        Downloads messages from this folder

        :param limit: limits the result set. Over 999 uses batch.
        :param query: applies a filter to the request such as 'displayName:HelloFolder'
        :param order_by: orders the result set based on this condition
        :param batch: Returns a custom iterator that retrieves items in batches allowing
            to retrieve more items than the limit. Download_attachments is ignored.
        :param download_attachments: downloads message attachments
        """

        if self.root:
            url = self.build_url(self._endpoints.get('root_messages'))
        else:
            url = self.build_url(self._endpoints.get('folder_messages').format(id=self.folder_id))

        if limit is None or limit > self.protocol.max_top_value:
            batch = self.protocol.max_top_value

        if batch:
            download_attachments = False

        params = {'$top': batch if batch else limit}

        if order_by:
            params['$orderby'] = order_by

        if query:
            if isinstance(query, str):
                params['$filter'] = query
            else:
                params.update(query.as_params())

        try:
            response = self.con.get(url, params=params)
        except Exception as e:
            log.error('Error donwloading messages in folder {}. Error {}'.format(self.name, e))
            return []
        log.debug('Getting messages in folder {} Response: {}'.format(self.name, str(response)))

        if response.status_code != 200:
            log.debug('Getting messages Request failed: {}'.format(response.reason))
            return []

        data = response.json()

        # Everything received from the cloud must be passed with self._cloud_data_key
        messages = [self.message_constructor(parent=self, download_attachments=download_attachments,
                                             **{self._cloud_data_key: message})
                    for message in data.get('value', [])]
        if batch:
            return Pagination(parent=self, data=messages, constructor=self.message_constructor,
                              next_link=data.get(NEXT_LINK_KEYWORD, None), limit=limit)
        else:
            return messages
Пример #2
0
    def get_folders(self,
                    limit=None,
                    *,
                    query=None,
                    order_by=None,
                    batch=None):
        """
        Returns a list of child folders

        :param limit: limits the result set. Over 999 uses batch.
        :param query: applies a filter to the request such as "displayName eq 'HelloFolder'"
        :param order_by: orders the result set based on this condition
        :param batch: Returns a custom iterator that retrieves items in batches allowing to retrieve more items than the limit.
        """

        if self.root:
            url = self.build_url(self._endpoints.get('root_folders'))
        else:
            url = self.build_url(
                self._endpoints.get('child_folders').format(id=self.folder_id))

        if limit is None or limit > self.protocol.max_top_value:
            batch = self.protocol.max_top_value

        params = {'$top': batch if batch else limit}

        if order_by:
            params['$orderby'] = order_by

        if query:
            if isinstance(query, str):
                params['$filter'] = query
            else:
                params.update(query.as_params())

        response = self.con.get(url, params=params)
        if not response:
            return []

        data = response.json()

        # Everything received from the cloud must be passed with self._cloud_data_key
        self_class = getattr(self, 'folder_constructor', type(self))
        folders = [
            self_class(parent=self, **{self._cloud_data_key: folder})
            for folder in data.get('value', [])
        ]
        next_link = data.get(NEXT_LINK_KEYWORD, None)
        if batch and next_link:
            return Pagination(parent=self,
                              data=folders,
                              constructor=self_class,
                              next_link=next_link,
                              limit=limit)
        else:
            return folders
Пример #3
0
    def get_folders(self, limit=None, *, query=None, order_by=None, batch=None):
        """
        Returns a list of child folders

        :param limit: limits the result set. Over 999 uses batch.
        :param query: applies a filter to the request such as "displayName eq 'HelloFolder'"
        :param order_by: orders the result set based on this condition
        :param batch: Returns a custom iterator that retrieves items in batches allowing to retrieve more items than the limit.
        """

        if self.root:
            url = self.build_url(self._endpoints.get('root_folders'))
        else:
            url = self.build_url(self._endpoints.get('child_folders').format(id=self.folder_id))

        if limit is None or limit > self.protocol.max_top_value:
            batch = self.protocol.max_top_value

        params = {'$top': batch if batch else limit}

        if order_by:
            params['$orderby'] = order_by

        if query:
            if isinstance(query, str):
                params['$filter'] = query
            else:
                params.update(query.as_params())

        try:
            response = self.con.get(url, params=params)
        except Exception as e:
            log.error('Error requesting child folders of {}. Error: {}'.format(self.name, str(e)))
            return []

        if response.status_code != 200:
            log.debug('Getting folders Request failed: {}'.format(response.reason))
            return []

        data = response.json()

        # Everything received from the cloud must be passed with self._cloud_data_key
        folders = [Folder(parent=self, **{self._cloud_data_key: folder}) for folder in data.get('value', [])]
        if batch:
            return Pagination(parent=self, data=folders, constructor=self.__class__,
                              next_link=data.get(NEXT_LINK_KEYWORD, None), limit=limit)
        else:
            return folders
Пример #4
0
    def get_contacts(self,
                     limit=100,
                     *,
                     query=None,
                     order_by=None,
                     batch=None):
        """ Gets a list of contacts from this address book

        When querying the Global Address List the Users endpoint will be used.
        Only a limited set of information will be available unless you have
        access to scope 'User.Read.All' which requires App Administration
        Consent.

        Also using endpoints has some limitations on the querying capabilities.

        To use query an order_by check the OData specification here:
        http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/
        part2-url-conventions/odata-v4.0-errata03-os-part2-url-conventions
        -complete.html

        :param limit: max no. of contacts to get. Over 999 uses batch.
        :type limit: int or None
        :param query: applies a OData filter to the request
        :type query: Query or str
        :param order_by: orders the result set based on this condition
        :type order_by: Query or str
        :param int batch: batch size, retrieves items in
         batches allowing to retrieve more items than the limit.
        :return: list of contacts
        :rtype: list[Contact] or Pagination
        """

        if self.main_resource == GAL_MAIN_RESOURCE:
            # using Users endpoint to access the Global Address List
            url = self.build_url(self._endpoints.get('gal'))
        else:
            if self.root:
                url = self.build_url(self._endpoints.get('root_contacts'))
            else:
                url = self.build_url(
                    self._endpoints.get('folder_contacts').format(
                        id=self.folder_id))

        if limit is None or limit > self.protocol.max_top_value:
            batch = self.protocol.max_top_value

        params = {'$top': batch if batch else limit}

        if order_by:
            params['$orderby'] = order_by

        if query:
            if isinstance(query, str):
                params['$filter'] = query
            else:
                params.update(query.as_params())

        response = self.con.get(url, params=params)
        if not response:
            return []

        data = response.json()

        # Everything received from cloud must be passed as self._cloud_data_key
        contacts = [
            self.contact_constructor(parent=self,
                                     **{self._cloud_data_key: contact})
            for contact in data.get('value', [])
        ]

        next_link = data.get(NEXT_LINK_KEYWORD, None)

        if batch and next_link:
            return Pagination(parent=self,
                              data=contacts,
                              constructor=self.contact_constructor,
                              next_link=next_link,
                              limit=limit)
        else:
            return contacts
Пример #5
0
    def get_contacts(self,
                     limit=100,
                     *,
                     query=None,
                     order_by=None,
                     batch=None):
        """
        Gets a list of contacts from this address book

        When quering the Global Address List the Users enpoint will be used.
        Only a limited set of information will be available unless you have acces to
         scope 'User.Read.All' wich requires App Administration Consent.
        Also using the Users enpoint has some limitations on the quering capabilites.

        To use query an order_by check the OData specification here:
        http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/part2-url-conventions/odata-v4.0-errata03-os-part2-url-conventions-complete.html

        :param limit: Number of elements to return. Over 999 uses batch.
        :param query: a OData valid filter clause
        :param order_by: OData valid order by clause
        :param batch: Returns a custom iterator that retrieves items in batches allowing
            to retrieve more items than the limit.
        """

        if self.main_resource == GAL_MAIN_RESOURCE:
            # using Users endpoint to access the Global Address List
            url = self.build_url(self._endpoints.get('gal'))
        else:
            if self.root:
                url = self.build_url(self._endpoints.get('root_contacts'))
            else:
                url = self.build_url(
                    self._endpoints.get('folder_contacts').format(
                        self.folder_id))

        if limit is None or limit > self.protocol.max_top_value:
            batch = self.protocol.max_top_value

        params = {'$top': batch if batch else limit}

        if order_by:
            params['$orderby'] = order_by

        if query:
            if isinstance(query, str):
                params['$filter'] = query
            else:
                params.update(query.as_params())

        try:
            response = self.con.get(url, params=params)
        except Exception as e:
            log.error('Error getting contacts. Error {}'.format(str(e)))
            return []

        if response.status_code != 200:
            log.debug('Getting contacts Request failed: {}'.format(
                response.reason))
            return []

        data = response.json()

        # Everything received from the cloud must be passed with self._cloud_data_key
        contacts = [
            self.contact_constructor(parent=self,
                                     **{self._cloud_data_key: contact})
            for contact in data.get('value', [])
        ]

        next_link = data.get(NEXT_LINK_KEYWORD, None)

        if batch and next_link:
            return Pagination(parent=self,
                              data=contacts,
                              constructor=self.contact_constructor,
                              next_link=data.get(NEXT_LINK_KEYWORD, None),
                              limit=limit)
        else:
            return contacts
Пример #6
0
    def get_messages(self,
                     limit=25,
                     *,
                     query=None,
                     order_by=None,
                     batch=None,
                     download_attachments=False):
        """
        Downloads messages from this folder

        :param int limit: limits the result set. Over 999 uses batch.
        :param query: applies a filter to the request such as
         "displayName eq 'HelloFolder'"
        :type query: Query or str
        :param order_by: orders the result set based on this condition
        :type order_by: Query or str
        :param int batch: batch size, retrieves items in
         batches allowing to retrieve more items than the limit.
        :param bool download_attachments: whether or not to download attachments
        :return: list of messages
        :rtype: list[Message] or Pagination
        """

        if self.root:
            url = self.build_url(self._endpoints.get('root_messages'))
        else:
            url = self.build_url(
                self._endpoints.get('folder_messages').format(
                    id=self.folder_id))

        if limit is None or limit > self.protocol.max_top_value:
            batch = self.protocol.max_top_value

        if batch:
            download_attachments = False

        params = {'$top': batch if batch else limit}

        if order_by:
            params['$orderby'] = order_by

        if query:
            if isinstance(query, str):
                params['$filter'] = query
            else:
                params.update(query.as_params())

        response = self.con.get(url, params=params)
        if not response:
            return []

        data = response.json()

        # Everything received from cloud must be passed as self._cloud_data_key
        messages = [
            self.message_constructor(parent=self,
                                     download_attachments=download_attachments,
                                     **{self._cloud_data_key: message})
            for message in data.get('value', [])
        ]

        next_link = data.get(NEXT_LINK_KEYWORD, None)
        if batch and next_link:
            return Pagination(parent=self,
                              data=messages,
                              constructor=self.message_constructor,
                              next_link=next_link,
                              limit=limit)
        else:
            return messages