Пример #1
0
 def notify(
     expect: bool,  # whether the message should make it
     max_attempts: Optional[int] = None,  # how many attempts to allow
     responses: List[Tuple[
         float,
         int]] = None,  # a list of (delay, http_status) tuples, one per attempt
     attempts=None
 ):  # expected number of attempts, currently only used to estimate the running time
     if responses is None:
         responses = [(0.0, 200)]
     verify = random.random() > .5
     notification_id = str(next(self.notification_id))
     body: JSON = dict(notification_id=notification_id,
                       responses=responses,
                       verify=verify)
     notification = Notification.create(
         notification_id=notification_id,
         subscription_id=str(random.choice(self.subscription_ids)),
         url=f"http://{self.address}:{self.port}/{notification_id}",
         method='POST',
         encoding='application/json',
         body=body,
         attempts=max_attempts,
         hmac_key=PostTestHandler.hmac_secret_key if verify else None,
         hmac_key_id='1234' if verify else None)
     nonlocal total_attempts
     total_attempts += min(
         notification.attempts,
         self.num_queues) if attempts is None else attempts
     (expected_receptions
      if expect else expected_misses).add(notification_id)
     self.notifier.enqueue(notification)
Пример #2
0
    def _notify_subscriber(self, doc: BundleDocument, subscription: dict):
        transaction_id = str(uuid.uuid4())
        subscription_id = subscription['id']
        endpoint = Endpoint.from_subscription(subscription)

        payload = dict(transaction_id=transaction_id,
                       subscription_id=subscription_id,
                       es_query=subscription['es_query'],
                       match=dict(bundle_uuid=doc.fqid.uuid,
                                  bundle_version=doc.fqid.version))

        definitions = subscription.get('attachments')
        # Only mention attachments in the notification if the subscription does, too.
        if definitions is not None:
            payload['attachments'] = attachment.select(definitions, doc)

        if endpoint.encoding == 'application/json':
            body = payload
        elif endpoint.encoding == 'multipart/form-data':
            body = endpoint.form_fields.copy()
            body[endpoint.payload_form_field] = json.dumps(payload)
        else:
            raise ValueError(f"Encoding {endpoint.encoding} is not supported")

        try:
            hmac_key = subscription['hmac_secret_key']
        except KeyError:
            hmac_key = None
            hmac_key_id = None
        else:
            hmac_key = hmac_key.encode()
            hmac_key_id = subscription.get('hmac_key_id', "hca-dss:" + subscription_id)

        notification = Notification.create(notification_id=transaction_id,
                                           subscription_id=subscription_id,
                                           url=endpoint.callback_url,
                                           method=endpoint.method,
                                           encoding=endpoint.encoding,
                                           body=body,
                                           hmac_key=hmac_key,
                                           hmac_key_id=hmac_key_id,
                                           correlation_id=str(doc.fqid))
        if self.notifier:
            logger.info(f"Queueing asynchronous notification {notification} for bundle {doc.fqid}")
            self.notifier.enqueue(notification)
        else:
            logger.info(f"Synchronously sending notification {notification} about bundle {doc.fqid}")
            notification.deliver_or_raise()