Beispiel #1
0
def webhook(url, payload=None, oid=None):
    """Post to a webhook."""
    from flask import current_app
    from readability.readability import Document
    try:
        import json
        from pybossa.core import sentinel, webhook_repo, project_repo
        project = project_repo.get(payload['project_id'])
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        if oid:
            webhook = webhook_repo.get(oid)
        else:
            webhook = Webhook(project_id=payload['project_id'],
                              payload=payload)
        if url:
            response = requests.post(url, data=json.dumps(payload), headers=headers)
            webhook.response = Document(response.text).summary()
            webhook.response_status_code = response.status_code
        else:
            raise requests.exceptions.ConnectionError('Not URL')
        if oid:
            webhook_repo.update(webhook)
            webhook = webhook_repo.get(oid)
        else:
            webhook_repo.save(webhook)
    except requests.exceptions.ConnectionError:
        webhook.response = 'Connection Error'
        webhook.response_status_code = None
        webhook_repo.save(webhook)
    finally:
        if project.published and webhook.response_status_code != 200 and current_app.config.get('ADMINS'):
            subject = "Broken: %s webhook failed" % project.name
            body = 'Sorry, but the webhook failed'
            mail_dict = dict(recipients=current_app.config.get('ADMINS'),
                             subject=subject, body=body, html=webhook.response)
            send_mail(mail_dict)
    if current_app.config.get('SSE'):
        publish_channel(sentinel, payload['project_short_name'],
                        data=webhook.dictize(), type='webhook',
                        private=True)
    return webhook
Beispiel #2
0
 def test_webhook_handler_post_oid(self):
     """Test WEBHOOK post oid works."""
     self.register()
     user = user_repo.get(1)
     project = ProjectFactory.create(owner=user)
     task = TaskFactory.create(project=project, n_answers=1)
     AnonymousTaskRunFactory.create(project=project, task=task)
     payload = self.payload(project, task)
     webhook = Webhook(project_id=project.id,
                       payload=payload,
                       response='OK',
                       response_status_code=200)
     webhook_repo.save(webhook)
     webhook = webhook_repo.get(1)
     url = "/project/%s/webhook/%s" % (project.short_name, webhook.id)
     res = self.app.post(url)
     tmp = json.loads(res.data)
     assert res.status_code == 200, res.status_code
     assert tmp['payload']['project_short_name'] == project.short_name
     assert tmp['payload']['project_id'] == project.id
     assert tmp['payload']['task_id'] == task.id
Beispiel #3
0
def webhook(url, payload=None, oid=None):
    """Post to a webhook."""
    from flask import current_app
    try:
        import json
        from pybossa.core import sentinel, webhook_repo
        headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
        if oid:
            webhook = webhook_repo.get(oid)
        else:
            webhook = Webhook(project_id=payload['project_id'],
                              payload=payload)
        if url:
            response = requests.post(url,
                                     data=json.dumps(payload),
                                     headers=headers)
            webhook.response = response.text
            webhook.response_status_code = response.status_code
        else:
            raise requests.exceptions.ConnectionError('Not URL')
        if oid:
            webhook_repo.update(webhook)
            webhook = webhook_repo.get(oid)
        else:
            webhook_repo.save(webhook)
    except requests.exceptions.ConnectionError:
        webhook.response = 'Connection Error'
        webhook.response_status_code = None
        webhook_repo.save(webhook)
    if current_app.config.get('SSE'):
        publish_channel(sentinel,
                        payload['project_short_name'],
                        data=webhook.dictize(),
                        type='webhook',
                        private=True)
    return webhook
 def setUp(self):
     super(TestWebhookRepository, self).setUp()
     self.webhook_repo = WebhookRepository(db)
     TaskRunFactory.create()
     webhook = Webhook(project_id=1, payload=self.payload)
     self.webhook_repo.save(webhook)