Example #1
0
 def create_nudge_batch(nudge_subscription_id):
     """ :type nudge_subscription_id: str
         :rtype dict"""
     host_url = config.get('nudge').get('host_url')
     json_body = {'SubscriptionId': nudge_subscription_id}
     return make_nudge_request(url='%s/CreateBatch' % host_url,
                               json=json_body).json()
Example #2
0
    def wait_for_nudge_activation(self,
                                  nudge_sub_id,
                                  sleep_time=10,
                                  retries=9):
        """Wait for a nudge subscription to become active.
        
        :type sleep_time: int
        :type retries: int
        :type nudge_subs_id: str
        :rtype bool
        :returns True if the subscription has activated
        """
        for _ in xrange(retries + 1):
            response = make_nudge_request(
                url='{}/GetSubscription'.format(
                    config.get('nudge').get('host_url')),
                json={'SubscriptionId': nudge_sub_id},
            )

            state = response.json()['State']
            if state == 'INACTIVE':
                raise Exception('Nudge subscription has been deactivated')
            if state == 'ACTIVE':
                return True

            time.sleep(sleep_time)

        return False
Example #3
0
 def ack_nudge_elements(nudge_subscription_id, batch_id):
     """ :type nudge_subscription_id: str
         :type batch_id: str
         :rtype dict"""
     host_url = config.get('nudge').get('host_url')
     json_body = {
         'SubscriptionId': nudge_subscription_id,
         'BatchId': batch_id,
     }
     return make_nudge_request(url='%s/Consume' % host_url,
                               json=json_body).json()
Example #4
0
 def get_latest_nudge_batches(nudge_subscription_id, prev_batch_id=None):
     """ :type nudge_subscription_id: str
         :type prev_batch_id: str
         :rtype list[str]"""
     host_url = config.get('nudge').get('host_url')
     json_body = {
         'SubscriptionId': nudge_subscription_id,
     }
     if prev_batch_id:
         json_body['PreviousBatchId'] = prev_batch_id
     return make_nudge_request(url='%s/GetSubscriptionBatches' % host_url,
                               json=json_body).json()['Batches']
Example #5
0
    def get_nudge_batch_elements(nudge_subscription_id, batch_id):
        """ :type nudge_subscription_id: str
            :type batch_id: str
            :rtype Nudge.SubscriptionElement"""
        limit = 10000
        offset = 0
        host_url = config.get('nudge').get('host_url')
        while True:
            response = make_nudge_request(
                url='%s/GetBatchElements' % host_url,
                json={
                    'SubscriptionId': nudge_subscription_id,
                    'BatchId': batch_id,
                    'Limit': limit,
                    'Offset': offset
                },
            )
            elements = response.json()['Elements']
            if len(elements) == 0:
                break
            for e in elements:
                yield e

            offset += limit