Exemplo n.º 1
0
def _send_request(webhook_id, url, key, data):
    serialized_data = UnicodeJSONRenderer().render(data)
    signature = _generate_signature(serialized_data, key)
    headers = {
        "X-TAIGA-WEBHOOK-SIGNATURE": signature,
        "Content-Type": "application/json"
    }
    request = requests.Request('POST', url, data=serialized_data, headers=headers)
    prepared_request = request.prepare()

    session = requests.Session()
    try:
        response = session.send(prepared_request)
        webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url,
                                                status=response.status_code,
                                                request_data=data,
                                                request_headers=dict(prepared_request.headers),
                                                response_data=response.content,
                                                response_headers=dict(response.headers),
                                                duration=response.elapsed.total_seconds())
    except RequestException as e:
        webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url, status=0,
                                                request_data=data,
                                                request_headers=dict(prepared_request.headers),
                                                response_data="error-in-request: {}".format(str(e)),
                                                response_headers={},
                                                duration=0)
    session.close()

    ids = [log.id for log in WebhookLog.objects.filter(webhook_id=webhook_id).order_by("-id")[10:]]
    WebhookLog.objects.filter(id__in=ids).delete()
    return webhook_log
Exemplo n.º 2
0
def _send_request(webhook_id, url, key, data):
    serialized_data = UnicodeJSONRenderer().render(data)
    signature = _generate_signature(serialized_data, key)
    headers = {
        "X-TAIGA-WEBHOOK-SIGNATURE": signature,        # For backward compatibility
        "X-Hub-Signature": "sha1={}".format(signature),
        "Content-Type": "application/json"
    }

    if settings.WEBHOOKS_BLOCK_PRIVATE_ADDRESS:
        try:
            urls.validate_private_url(url)
        except (urls.IpAddresValueError, urls.HostnameException) as e:
            # Error validating url
            webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url,
                                                    status=0,
                                                    request_data=data,
                                                    request_headers=dict(),
                                                    response_data="error-in-request: {}".format(
                                                        str(e)),
                                                    response_headers={},
                                                    duration=0)
            _remove_leftover_webhooklogs(webhook_id)

            return webhook_log

    request = requests.Request('POST', url, data=serialized_data, headers=headers)
    prepared_request = request.prepare()

    with requests.Session() as session:
        try:
            response = session.send(prepared_request)
        except RequestException as e:
            # Error sending the webhook
            webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url, status=0,
                                                    request_data=data,
                                                    request_headers=dict(prepared_request.headers),
                                                    response_data="error-in-request: {}".format(str(e)),
                                                    response_headers={},
                                                    duration=0)
        else:
            # Webhook was sent successfully

            # response.content can be a not valid json so we encapsulate it
            response_data = json.dumps({"content": response.text})
            webhook_log = WebhookLog.objects.create(webhook_id=webhook_id, url=url,
                                                    status=response.status_code,
                                                    request_data=data,
                                                    request_headers=dict(prepared_request.headers),
                                                    response_data=response_data,
                                                    response_headers=dict(response.headers),
                                                    duration=response.elapsed.total_seconds())
        finally:
            _remove_leftover_webhooklogs(webhook_id)

    return webhook_log
Exemplo n.º 3
0
def _send_request(url, data):
    serialized_data = UnicodeJSONRenderer().render(data)

    if settings.CELERY_ENABLED:
        requests.post(url, data=serialized_data)
        return

    try:
        requests.post(url, data=serialized_data)
    except Exception:
        logger.error("Error sending request to slack")
def _send_request(url, notify, data):
    data["notify"] = notify
    serialized_data = UnicodeJSONRenderer().render(data)
    headers = {
        'Content-type': 'application/json',
    }
    if settings.CELERY_ENABLED:
        requests.post(url, data=serialized_data, headers=headers)
        return
    try:
        requests.post(url, data=serialized_data, headers=headers)
    except Exception:
        logger.error("Error sending request to HipChat")
Exemplo n.º 5
0
def _send_request(url, token, data):
    serialized_data = UnicodeJSONRenderer().render(data)
    headers = {
        'Authorization': 'Bearer ' + token,
        'Content-type': 'application/json',
    }
    if settings.CELERY_ENABLED:
        requests.post(url, data=serialized_data, headers=headers)
        return
    try:
        requests.post(url, data=serialized_data, headers=headers)
    except Exception:
        logger.error("Error sending request to LetsChat")
Exemplo n.º 6
0
def _send_request(webhook_id, url, key, data):
    serialized_data = UnicodeJSONRenderer().render(data)
    signature = _generate_signature(serialized_data, key)
    headers = {
        "X-TAIGA-WEBHOOK-SIGNATURE": signature,  # For backward compatibility
        "X-Hub-Signature": "sha1={}".format(signature),
        "Content-Type": "application/json"
    }
    request = requests.Request('POST',
                               url,
                               data=serialized_data,
                               headers=headers)
    prepared_request = request.prepare()

    with requests.Session() as session:
        try:
            response = session.send(prepared_request)
        except RequestException as e:
            # Error sending the webhook
            webhook_log = WebhookLog.objects.create(
                webhook_id=webhook_id,
                url=url,
                status=0,
                request_data=data,
                request_headers=dict(prepared_request.headers),
                response_data="error-in-request: {}".format(str(e)),
                response_headers={},
                duration=0)
        else:
            # Webhook was sent successfully

            # response.content can be a not valid json so we encapsulate it
            response_data = json.dumps({"content": response.text})
            webhook_log = WebhookLog.objects.create(
                webhook_id=webhook_id,
                url=url,
                status=response.status_code,
                request_data=data,
                request_headers=dict(prepared_request.headers),
                response_data=response_data,
                response_headers=dict(response.headers),
                duration=response.elapsed.total_seconds())
        finally:
            # Only the last ten webhook logs traces are required
            # so remove the leftover
            ids = (WebhookLog.objects.filter(
                webhook_id=webhook_id).order_by("-id").values_list(
                    'id', flat=True)[10:])
            WebhookLog.objects.filter(id__in=ids).delete()

    return webhook_log