10000 Merge pull request #21 from twilio/monitor · stomatocode/twilio-python@248f18f · GitHub
[go: up one dir, main page]

Skip to content

Commit 248f18f

Browse files
committed
Merge pull request twilio#21 from twilio/monitor
Implement monitor
2 parents d8a83cd + 4b91b2a commit 248f18f

File tree

12 files changed

+379
-0
lines changed

12 files changed

+379
-0
lines changed

tests/monitor/__init__.py

Whitespace-only changes.

tests/monitor/test_alerts.py

Lines changed: 35 additions & 0 deletions
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+
resp.status_code = 200
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)

tests/monitor/test_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from mock import patch
2+
3+
from tests.tools import create_mock_json
4+
from twilio.rest.monitor import TwilioMonitorClient
5+
6+
7+
@patch("twilio.rest.resources.base.make_twilio_request")
8+
def test_events(mock):
9+
client = TwilioMonitorClient("ACCOUNT_SID", "AUTH_TOKEN")
10+
resp = create_mock_json("tests/resources/monitor/events_instance.json")
11+
mock.return_value = resp
12+
client.events.get("AEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13+
uri = "https://monitor.twilio.com/v1/Events/AEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
14+
mock.assert_called_with("GET", uri, auth=("ACCOUNT_SID", "AUTH_TOKEN"), use_json_extension=False)

tests/monitor/test_events.py

Lines changed: 35 additions & 0 deletions
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.events import Events
7+
8+
9+
AUTH = ("AC123", "token")
10+
BASE_URI = "https://monitor.twilio.com/v1"
11+
EVENT_SID = "AEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12+
13+
14+
class EventTest(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/events_instance.json')
18+
resp.status_code = 200
19+
request.return_value = resp
20+
21+
uri = "{0}/Events/{1}".format(BASE_URI, EVENT_SID)
22+
list_resource = Events(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/events_list.json')
29+
resp.status_code = 200
30+
request.return_value = resp
31+
32+
uri = "{0}/Events".format(BASE_URI)
33+
list_resource = Events(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
+
"response_headers": "Date=Tue%2C+17+Mar+2015+22%3A40%3A19+GMT&Content-Length=36&Server=nginx%2F1.6.2",
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
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"account_sid": "ACbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
3+
"actor_sid": "ACbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
4+
"actor_type": "account",
5+
"description": null,
6+
"event_date": "2014-10-03T16:48:25Z",
7+
"event_type": "account.updated",
8+
"resource_sid": "AC384c618f68e746e62d8ccfe591239e62",
9+
"resource_type": "account",
10+
"sid": "AE9ef65da5ad5243fd8336334cf98721ea",
11+
"source": "api",
12+
"source_ip_address": "10.86.6.250",
13+
"event_data": {
14+
"friendly_name": {
15+
"previous": "SubAccount Created at 2014-10-03 09:48 am",
16+
"updated": "Mr. Friendly"
17+
}
18+
},
19+
"links": {
20+
"actor": "https://api.twilio.com/2010-04-01/Accounts/ACbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
21+
"resource": "https://api.twilio.com/2010-04-01/Accounts/AC384c618f68e746e62d8ccfe591239e62"
22+
},
23+
"url": "https://monitor.twilio.com/v1/Events/AE9ef65da5ad5243fd8336334cf98721ea"
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"events": [
3+
{
4+
"account_sid": "ACbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
5+
"actor_sid": "ACbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
6+
"actor_type": "account",
7+
"description": null,
8+
"event_date": "2014-10-03T16:48:25Z",
9+
"event_type": "account.updated",
10+
"resource_sid": "AC384c618f68e746e62d8ccfe591239e62",
11+
"resource_type": "account",
12+
"sid": "AE9ef65da5ad5243fd8336334cf98721ea",
13+
"source": "api",
14+
"source_ip_address": "10.86.6.250",
15+
"event_data": {
16+
"friendly_name": {
17+
"previous": "SubAccount Created at 2014-10-03 09:48 am",
18+
"updated": "Mr. Friendly"
19+
}
20+
},
21+
"links": {
22+
"actor": "https://api.twilio.com/2010-04-01/Accounts/ACbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
23+
"resource": "https://api.twilio.com/2010-04-01/Accounts/AC384c618f68e746e62d8ccfe591239e62"
24+
},
25+
"url": "https://monitor.twilio.com/v1/Events/AE9ef65da5ad5243fd8336334cf98721ea"
26+
}
27+
28+
],
29+
"meta": {
30+
"key": "events",
31+
"next_page_url": null,
32+
"url": "https://monitor.twilio.com/v1/Events?PageSize=50&Page=0",
33+
"previous_page_url": null,
34+
"first_page_url": "https://monitor.twilio.com/v1/Events?PageSize=50&Page=0",
35+
"page_size": 50,
36+
"page": 0
37+
}
38+
}

twilio/rest/monitor.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from twilio.rest.base import TwilioClient
2+
from twilio.rest.resources import UNSET_TIMEOUT
3+
from twilio.rest.resources.monitor.alerts import Alerts
4+
from twilio.rest.resources.monitor.events import Events
5+
6+
7+
class TwilioMonitorClient(TwilioClient):
8+
"""
9+
A client for accessing the Twilio Monitor API.
10+
11+
The Twilio Monitor API provides information about events. For more
12+
information, see the
13+
`Monitor API documentation <https://www.twilio.com/docs/XXX>`_.
14+
15+
:param str account: Your Account Sid from `your dashboard
16+
<https://www.twilio.com/user/account>`_
17+
:param str token: Your Auth Token from `your dashboard
18+
<https://www.twilio.com/user/account>`_
19+
:param float timeout: The socket and read timeout for requests to Twilio
20+
"""
21+
22+
def __init__(self, account=None, token=None,
23+
base="https://monitor.twilio.com", version="v1",
24+
timeout=UNSET_TIMEOUT):
25+
26+
super(TwilioMonitorClient, self).__init__(account, token, base,
27+
version, timeout)
28+
29+
self.version_uri = "%s/%s" % (base, version)
30+
self.events = Events(self.version_uri, self.auth, timeout)
31+
self.alerts = Alerts(self.version_uri, self.auth, timeout)

twilio/rest/resources/monitor/__init__.py

Whitespace-only changes.
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)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from twilio.rest.resources.base import (
2+
NextGenInstanceResource,
3+
NextGenListResource,
4+
)
5+
6+
7+
class Event(NextGenInstanceResource):
8+
"""An Event resource representing a state change in the Twilio API.
9+
10+
See the `Monitor API reference
11+
<https://www.twilio.com/docs/api/rest/monitor>_`
12+
for more information.
13+
14+
.. attribute:: sid
15+
16+
The unique ID for this event.
17+
18+
.. attribute:: account_sid
19+
20+
The unique ID of the Account that owns this Event.
21+
22+
.. attribute:: actor_sid
23+
24+
The unique ID of the actor of this event.
25+
26+
.. attribute:: actor_type
27+
28+
The type of actor of this event.
29+
30+
.. attribute:: description
31+
32+
A description of the event.
33+
34+
.. attribute:: event_date
35+
36+
The time this event was sent, in UTC ISO 8601 format.
37+
38+
.. attribute:: event_type
39+
40+
The type of this event.
41+
42+
.. attribute:: resource_sid
43+
44+
The unique ID of the object this event is most relevant to.
45+
46+
.. attribute:: resource_type
47+
48+
The type of object this event is most relevant to.
49+
50+
.. attribute:: source
51+
52+
The source of this event.
53+
54+
.. attribute:: source_ip_address
55+
56+
The source's IP address for this event.
57+
58+
.. attribute:: event_data
59+
60+
Data about this specific Event.
61+
"""
62+
pass
63+
64+
65+
class Events(NextGenListResource):
66+
name = "Events"
67+
instance = Event
68+
69+
def list(self, **kwargs):
70+
"""
71+
Returns a page of :class:`Event` resources as a list. For paging
72+
information see :class:`NextGenListResource`
73+
74+
:param actor_sid: (Optional) Sid of the event actor.
75+
:param start_date: (Optional) Filter events
76+
by a start date.
77+
:param end_date: (Optional) Filter events by an end date.
78+
:param resource_sid: (Optional) Sid of the event resource.
79+
:param event_type: (Optional) The type of event to filter by.
80+
:param source_ip_address: (Optional) The IP address of the event's
81+
source.
82+
"""
83+
return super(Events, self).list(**kwargs)
84+
85+
def get_instances(self, params):
86+
"""
87+
Query the list resource for a list of InstanceResources.
88+
Raises a :exc:`~twilio.TwilioRestException` if requesting a page of
89+
results that does not exist.
90+
91+
:param dict params: List of URL parameters to be included in request
92+
:param str page_token: Token of the page of results to retrieve
93+
:param int page_size: The number of results to be returned.
94+
:returns: -- the list of resources
95+
"""
96+
return super(Events, self).get_instances(params)

0 commit comments

Comments
 (0)
0