Example #1
0
    def list_messages(self,
                      from_=None,
                      to=None,
                      from_date_time=None,
                      to_date_time=None,
                      direction=None,
                      state=None,
                      delivery_state=None,
                      sort_order=None,
                      size=None,
                      **kwargs):
        """
        Get a list of user's messages

        :param str ``from_``: The phone number to filter the messages that came from
        :param str to: The phone number to filter the messages that was sent to
        :param str from_date_time: The starting date time to filter the messages
            (must be in yyyy-MM-dd hh:mm:ss format, like 2014-05-25 12:00:00.)
        :param str to_date_time: The ending date time to filter the messages (must be in
            yyyy-MM-dd hh:mm:ss format, like 2014-05-25 12:00:00.)
        :param str direction: Filter by direction of message, in - a message that came from the telephone
            network to one of your numbers (an "inbound" message) or out - a message
            that was sent from one of your numbers to the telephone network (an "outbound"
            message)
        :param str state: The message state to filter. Values are 'received', 'queued', 'sending',
            'sent', 'error'
        :param str delivery_state: The message delivery state to filter. Values are 'waiting', 'delivered',
            'not-delivered'
        :param str sort_order: How to sort the messages. Values are 'asc' or 'desc'
        :param str size: Used for pagination to indicate the size of each page requested for querying a list
            of items. If no value is specified the default value is 25. (Maximum value 1000)
        :rtype: types.GeneratorType
        :returns: list of messages

        Example: Search for all messages and are in error::

            message_list = api.list_messages()

            for message in message_list:
                if message['state'] == 'error':
                    print(message['id'])
            ## m-it6ewpyiyadfe
            ## m-pjnqofcjyadfe
            ## m-t2gspvs6iadfe
            ## m-shuh6d6pyadfe
        """

        kwargs['from'] = from_
        kwargs['to'] = to
        kwargs['fromDateTime'] = from_date_time
        kwargs['toDateTime'] = to_date_time
        kwargs['direction'] = direction
        kwargs['state'] = state
        kwargs['deliveryState'] = delivery_state
        kwargs['sortOrder'] = sort_order
        kwargs['size'] = size

        path = '/users/%s/messages' % self.user_id
        return get_lazy_enumerator(
            self, lambda: self._make_request('get', path, params=kwargs))
 def test_get_lazy_enumerator(self):
     """
     get_lazy_enumerator() should return data on demand
     """
     with patch('requests.request', return_value=create_response(200, '[1, 2, 3]')) as p:
         client = get_client()
         results = get_lazy_enumerator(client, lambda: client._make_request(
             'get', 'https://api.catapult.inetwork.com/v1/users/userId/account/transactions?page=0&size=25'))
         p.assert_not_called()
         self.assertEqual([1, 2, 3], list(results))
         p.assert_called_with(
             'get',
             'https://api.catapult.inetwork.com/v1/users/userId/account/transactions?page=0&size=25',
             headers=headers,
             auth=AUTH)
 def test_get_lazy_enumerator_with_several_requests(self):
     """
     get_lazy_enumerator() should request new portion of data on demand
     """
     estimated_json1 = '[1, 2, 3]'
     estimated_json2 = '[4, 5, 6, 7]'
     response1 = create_response(200, estimated_json1)
     response1.headers['link'] = '<transactions?page=0&size=25>; rel="first", ' \
                                 '<transactions?page=1&size=25>; rel="next"'
     response2 = create_response(200, estimated_json2)
     client = get_client()
     with patch('requests.request', return_value=response2) as p:
         results = get_lazy_enumerator(client, lambda: ([1, 2, 3], response1, None))
         self.assertEqual([1, 2, 3, 4, 5, 6, 7], list(results))
         p.assert_called_with(
             'get',
             'transactions?page=1&size=25',
             headers=headers,
             auth=AUTH)