Exemple #1
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
Exemple #2
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
Exemple #3
0
def webhook(url, payload=None, oid=None, rerun=False):
    """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:
            params = dict()
            if rerun:
                params['rerun'] = True
            response = requests.post(url,
                                     params=params,
                                     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
Exemple #4
0
    def test_webhook_handler_failed(self, q):
        """Test WEBHOOK requeing failed works."""
        self.register()
        self.signin()
        user = user_repo.get(1)
        project = ProjectFactory.create(owner=user, webhook='server')
        task = TaskFactory.create(project=project, n_answers=1)
        AnonymousTaskRunFactory.create(project=project, task=task)
        payload = self.payload(project, task)
        wh = Webhook(project_id=project.id,
                     payload=payload,
                     response='error',
                     response_status_code=500)
        webhook_repo.save(wh)
        wh2 = Webhook(project_id=project.id,
                      payload=payload,
                      response='ok',
                      response_status_code=200)
        webhook_repo.save(wh2)
        wh3 = Webhook(project_id=project.id,
                      payload=payload,
                      response='ok',
                      response_status_code=200)
        webhook_repo.save(wh3)

        wh = webhook_repo.get(1)
        url = "/project/%s/webhook?failed=true" % (project.short_name)
        res = self.app.get(url)
        assert res.status_code == 200, res.status_code
        q.assert_called_once_with(webhook, project.webhook, wh.payload, wh.id)
Exemple #5
0
def webhook(url, payload=None, oid=None, rerun=False):
    """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:
            params = dict()
            if rerun:
                params['rerun'] = True
            response = requests.post(url, params=params,
                                     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
Exemple #6
0
 def test_webhooks_connection_error(self, mock):
     """Test WEBHOOK with connection error works."""
     import requests
     from pybossa.core import webhook_repo
     mock.side_effect = requests.exceptions.ConnectionError
     err_msg = "A webhook should be returned"
     res = webhook('url', self.webhook_payload)
     assert res.response == 'Connection Error', err_msg
     assert res.response_status_code == None, err_msg
     wh = webhook_repo.get(1)
     assert wh.response == res.response, err_msg
     assert wh.response_status_code == res.response_status_code, err_msg
Exemple #7
0
 def test_webhooks_connection_error(self, mock):
     """Test WEBHOOK with connection error works."""
     import requests
     from pybossa.core import webhook_repo
     mock.side_effect = requests.exceptions.ConnectionError
     err_msg = "A webhook should be returned"
     res = webhook('url', self.webhook_payload)
     assert res.response == 'Connection Error', err_msg
     assert res.response_status_code == None, err_msg
     wh = webhook_repo.get(1)
     assert wh.response == res.response, err_msg
     assert wh.response_status_code == res.response_status_code, err_msg
Exemple #8
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
Exemple #9
0
 def test_webhook_handler_post_oid(self):
     """Test WEBHOOK post oid works."""
     self.register()
     self.signin()
     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
Exemple #10
0
    def test_webhook_handler_failed(self, q):
        """Test WEBHOOK requeing failed works."""
        self.register()
        user = user_repo.get(1)
        project = ProjectFactory.create(owner=user, webhook='server')
        task = TaskFactory.create(project=project, n_answers=1)
        AnonymousTaskRunFactory.create(project=project, task=task)
        payload = self.payload(project, task)
        wh = Webhook(project_id=project.id, payload=payload,
                     response='error', response_status_code=500)
        webhook_repo.save(wh)
        wh2 = Webhook(project_id=project.id, payload=payload,
                      response='ok', response_status_code=200)
        webhook_repo.save(wh2)
        wh3 = Webhook(project_id=project.id, payload=payload,
                      response='ok', response_status_code=200)
        webhook_repo.save(wh3)

        wh = webhook_repo.get(1)
        url = "/project/%s/webhook?failed=true" % (project.short_name)
        res = self.app.get(url)
        assert res.status_code == 200, res.status_code
        q.assert_called_once_with(webhook, project.webhook,
                                  wh.payload, wh.id, True)