integrations: Add webhook code, API endpoint, and tests for GoSquared.

This commit is contained in:
Jackson
2016-12-18 12:08:22 +11:00
committed by showell
parent db894b9912
commit 4ff4f12aea
5 changed files with 229 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@@ -0,0 +1,182 @@
{
"triggeredAlert": {
"id": 28632,
"boundary": "lower",
"value": "3",
"type": "concurrents"
},
"siteDetails": {
"user_id": 81923,
"acct": "GSN-595854-T",
"email": "asdasd@fasdf.com",
"first_name": "sadasd",
"last_name": "",
"site_name": "http://requestb.in/",
"domain": "requestb.in",
"url": "http://requestb.in/",
"timezone": "Australia/Sydney"
},
"snapshot": {
"time": {
"time": 1481952442380,
"local": "2016-12-17T16:27:22+11:00"
},
"concurrents": {
"visitors": 0,
"returning": 0,
"pages": 0,
"active": 0,
"tagged": 0
},
"timeSeries": {
"visitors.total": [{
"time": "2016-12-17T15:57:22+11:00",
"value": 0
}, {
"time": "2016-12-17T15:58:22+11:00",
"value": 0
}, {
"time": "2016-12-17T15:59:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:00:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:01:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:02:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:03:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:04:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:05:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:06:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:07:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:08:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:09:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:10:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:11:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:12:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:13:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:14:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:15:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:16:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:17:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:18:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:19:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:20:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:21:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:22:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:23:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:24:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:25:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:26:22+11:00",
"value": 0
}, {
"time": "2016-12-17T16:27:22+11:00",
"value": 0
}]
},
"pages": {
"list": [],
"cardinality": 0
},
"sources": {
"direct": 0,
"site": {
"list": [],
"visitors": 0,
"cardinality": 0
},
"social": {
"list": [],
"visitors": 0,
"cardinality": 0
},
"organic": {
"list": [],
"visitors": 0,
"cardinality": 0
},
"internal": {
"list": [],
"visitors": 0,
"cardinality": 0
}
},
"engagement": {
"timeOnSite": {
"breakdown": {
"0": 0,
"10": 0,
"30": 0,
"60": 0,
"300": 0,
"900": 0,
"1800": 0
},
"average": 0
},
"visitDepth": {
"breakdown": {
"1": 0,
"2": 0,
"5": 0,
"15": 0,
"30": 0,
"60": 0
},
"detailedBreakdown": {},
"average": 0
}
}
},
"concurrents": 0
}

View File

@@ -125,6 +125,7 @@ WEBHOOK_INTEGRATIONS = [
function='zerver.views.webhooks.github_webhook.api_github_webhook' function='zerver.views.webhooks.github_webhook.api_github_webhook'
), ),
WebhookIntegration('gitlab', display_name='GitLab'), WebhookIntegration('gitlab', display_name='GitLab'),
WebhookIntegration('gosquared', display_name='GoSquared'),
WebhookIntegration('helloworld', display_name='Hello World'), WebhookIntegration('helloworld', display_name='Hello World'),
WebhookIntegration('heroku', display_name='Heroku'), WebhookIntegration('heroku', display_name='Heroku'),
WebhookIntegration('ifttt', function='zerver.views.webhooks.ifttt.api_iftt_app_webhook', display_name='IFTTT'), WebhookIntegration('ifttt', function='zerver.views.webhooks.ifttt.api_iftt_app_webhook', display_name='IFTTT'),

View File

@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
from zerver.lib.test_classes import WebhookTestCase
class GoSquaredHookTests(WebhookTestCase):
STREAM_NAME = 'gosquared'
URL_TEMPLATE = "/api/v1/external/gosquared?stream={stream}&api_key={api_key}"
FIXTURE_DIR_NAME = 'gosquared'
# Note: Include a test function per each distinct message condition your integration supports
def test_traffic_message(self):
# type: () -> None
expected_subject = u"GoSquared"
expected_message = u"requestb.in has 0 visitors online."
# use fixture named helloworld_hello
self.send_and_test_stream_message('traffic_spike', expected_subject, expected_message,
content_type="application/x-www-form-urlencoded")

View File

@@ -0,0 +1,29 @@
from __future__ import absolute_import
from django.utils.translation import ugettext as _
from zerver.lib.actions import check_send_message
from zerver.lib.response import json_success, json_error
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
from zerver.models import Client, UserProfile
from django.http import HttpRequest, HttpResponse
from typing import Dict, Any
BODY_TEMPLATE = '{website_name} has {user_num} visitors online.'
@api_key_only_webhook_view('GoSquared')
@has_request_variables
def api_gosquared_webhook(request, user_profile, client,
payload=REQ(argument_type='body'),
stream=REQ(default='gosquared'),
topic=REQ(default='GoSquared')):
# type: (HttpRequest, UserProfile, Client, Dict[str, Dict[str, Any]]) -> HttpResponse
try:
domain_name = payload['siteDetails']['domain']
user_num = payload['concurrents']
body = BODY_TEMPLATE.format(website_name=domain_name, user_num=user_num)
except KeyError as e:
return json_error(_("Missing key {} in JSON").format(str(e)))
check_send_message(user_profile, client, 'stream', [stream], topic, body)
return json_success()