8000 Add Alerts resources · codemunkee/twilio-python@4b91b2a · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b91b2a

Browse files
committed
Add Alerts resources
1 parent ddc0af3 commit 4b91b2a

File tree

6 files changed

+151
-3
lines changed

6 files changed

+151
-3
lines changed

tests/monitor/test_alerts.py

Lines changed: 35 additions & 0 deletions
+
resp.status_code = 200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import unittest
2+
3+
from mock import patch
4+
5+
from tests.tools import create_mock_json
6+
from twilio.rest.resources.monitor.alerts import Alerts
7+
8+
9+
AUTH = ("AC123", "token")
10+
BASE_URI = "https://monitor.twilio.com/v1"
11+
EVENT_SID = "NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12+
13+
14+
class AlertTest(unittest.TestCase):
15+
@patch('twilio.rest.resources.base.make_twilio_request')
16+
def test_get(self, request):
17+
resp = create_mock_json('tests/resources/monitor/alerts_instance.json')
18
19+
request.return_value = resp
20+
21+
uri = "{0}/Alerts/{1}".format(BASE_URI, EVENT_SID)
22+
list_resource = Alerts(BASE_URI, AUTH)
23+
list_resource.get(EVENT_SID)
24+
request.assert_called_with("GET", uri, auth=AUTH, use_json_extension=False)
25+
26+
@patch('twilio.rest.resources.base.make_twilio_request')
27+
def test_list(self, request):
28+
resp = create_mock_json('tests/resources/monitor/alerts_list.json')
29+
resp.status_code = 200
30+
request.return_value = resp
31+
32+
uri = "{0}/Alerts".format(BASE_URI)
33+
list_resource = Alerts(BASE_URI, AUTH)
34+
list_resource.list()
35+
request.assert_called_with("GET", uri, params={}, auth=AUTH, use_json_extension=False)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"url": "https://monitor.twilio.com/v1/Alerts/NO70839f875fe15a2c74612eee1c02d2f3",
3+
"response_body": "",
4+
"alert_text": "LogLevel=ERROR&sourceComponent=12000&Msg&httpResponse=404&ErrorCode=11200&url=https%3A%2F%2F674d331e.ngrok.com%2Fdie",
5+
"more_info": "https://www.twilio.com/docs/errors/11200",
6+
"error_code": "11200",
7+
"log_level": "error",
8+
"resource_sid": "CA6f58741ed5bc70b2476902a8e9e628cc",
9+
"account_sid": "AC70839f875fe15a2c74612eee1c02d2f3",
10+
"sid": "NO6f58741ed5bc73fc74612eee1c02d2f3",
11+
"request_method": "GET",
12+
"request_url": "https://674d331e.ngrok.com/die",
13+
"request_variables": "AccountSid=AC70839f875fe15a2c74612eee1c02d2f3&ToZip=94111&FromState=CA&Called=%2B14154187474&FromCountry=US&CallerCountry=US&CalledZip=94111&Direction=inbound",
14+
"response_headers": "Date=Tue%2C+17+Mar+2015+22%3A40%3A19+GMT&Content-Length=36&Server=nginx%2F1.6.2",
15+
"date_created": "2015-03-17T22:40:19Z",
16+
"api_version": "2008-08-01",
17+
"date_updated": "2015-03-17T22:40:21Z",
18+
"date_generated": "2015-03-17T22:40:19Z"
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"meta": {
3+
"key": "alerts",
4+
"end": 1,
5+
"first_page_uri": "/v1/Alerts?PageSize=2&Page=0",
6+
"next_page_uri": "/v1/Alerts?PageSize=2&Page=1&PageToken=PANO7bd5bf2efbdf0500dc321b3a772e9229",
7+
"page": 0,
8+
"page_size": 2,
9+
"previous_page_uri": null,
10+
"start": 0,
11+
"uri": "/v1/Alerts?PageSize=2&Page=0"
12+
},
13+
"alerts": [
14+
{
15+
"uri": "https://monitor.twilio.com/v1/Alerts/NOf9a8decd348f123d17b716658e7b3932",
16+
"date_generated": "2015-03-02T20:33:18Z",
17+
"date_updated": "2015-03-02T20:33:18Z",
18+
"api_version": null,
19+
"date_created": "2015-03-02T20:33:18Z",
20+
"request_url": null,
21+
"sid": "NOf9a8decd348f123d17b716658e7b3932",
22+
"account_sid": "AC23d17b716658e7b326b77cdb692b437e",
23+
"call_sid": null,
24+
"log_level": "notice",
25+
"error_code": null,
26+
"more_info": null,
27+
"alert_text": "The balance of test@example.com's Account fell below $10.00 and according to the auto-recharge settings, we charged the payment method ...",
28+
"request_method": null
29+
},
30+
{
31+
"uri": "https://monitor.twilio.com/v1/Alerts/NO7bd5bf2efbdf0500dc321b3a772e9229",
32+
"date_generated": "2015-03-02T20:33:18Z",
33+
"date_updated": "2015-03-02T20:33:18Z",
34+
"api_version": null,
35+
"date_created": "2015-03-02T20:33:18Z",
36+
"request_url": null,
37+
"sid": "NO7bd5bf2efbdf0500dc321b3a772e9229",
38+
"account_sid": "AC23d17b716658e7b326b77cdb692b437e",
39+
"call_sid": null,
40+
"log_level": "notice",
41+
"error_code": null,
42+
"more_info": null,
43+
"alert_text": "The balance of test@example.com's Account fell below $10.00 and according to the auto-recharge settings, we charged the payment method ...",
44+
"request_method": null
45+
}
46+
]
47+
}

twilio/rest/monitor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from twilio.rest.base import TwilioClient
22
from twilio.rest.resources import UNSET_TIMEOUT
3+
from twilio.rest.resources.monitor.alerts import Alerts
34
from twilio.rest.resources.monitor.events import Events
45

56

@@ -22,7 +23,9 @@ def __init__(self, account=None, token=None,
2223
base="https://monitor.twilio.com", version="v1",
2324
timeout=UNSET_TIMEOUT):
2425

25-
super(TwilioMonitorClient, self).__init__(account, token, base, version, timeout)
26+
super(TwilioMonitorClient, self).__init__(account, token, base,
27+
version, timeout)
2628

2729
self.version_uri = "%s/%s" % (base, version)
2830
self.events = Events(self.version_uri, self.auth, timeout)
31+
self.alerts = Alerts(self.version_uri, self.auth, timeout)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from twilio.rest.resources import NextGenInstanceResource, NextGenListResource
2+
3+
4+
class Alert(NextGenInstanceResource):
5+
6+
def delete(self):
7+
"""
8+
Delete this alert
9+
"""
10+
return self.delete_instance()
11+
12+
13+
class Alerts(NextGenListResource):
14+
15+
name = "Alerts"
16+
instance = Alert
17+
18+
def list(self, before=None, after=None, **kwargs):
19+
"""
20+
Returns a page of :class:`Alert` resources as a list.
21+
For paging information see :class:`ListResource`.
22+
23+
**NOTE**: Due to the potentially voluminous amount of data in an
24+
alert, the full HTTP request and response data is only returned
25+
in the Alert instance resource representation.
26+
27+
:param date after: Only list alerts logged after this datetime
28+
:param date before: Only list alerts logger before this datetime
29+
:param log_level: If 'error', only shows errors. If 'warning',
30+
only show warnings
31+
"""
32+
kwargs["MessageDate<"] = before
33+
kwargs["MessageDate>"] = after
34+
return self.get_instances(kwargs)
35+
36+
def delete(self, sid):
37+
"""
38+
Delete a given Alert
39+
"""
40+
return self.delete_instance(sid)

twilio/rest/resources/monitor/events.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from twilio.rest.resources.base import NextGenInstanceResource, NextGenListResource
1+
from twilio.rest.resources.base import (
2+
NextGenInstanceResource,
3+
NextGenListResource,
4+
)
25

36

47
class Event(NextGenInstanceResource):
@@ -74,7 +77,8 @@ def list(self, **kwargs):
7477
:param end_date: (Optional) Filter events by an end date.
7578
:param resource_sid: (Optional) Sid of the event resource.
7679
:param event_type: (Optional) The type of event to filter by.
77-
:param source_ip_address: (Optional) The IP address of the event's source.
80+
:param source_ip_address: (Optional) The IP address of the event's
81+
source.
7882
"""
7983
return super(Events, self).list(**kwargs)
8084

0 commit comments

Comments
 (0)
0