8000 Implement monitor · randy3465/twilio-python@4eb0b7d · GitHub
[go: up one dir, main page]

Skip to content

Commit 4eb0b7d

Browse files
committed
Implement monitor
1 parent f0cfb8d commit 4eb0b7d

File tree

8 files changed

+226
-0
lines changed

8 files changed

+226
-0
lines changed

tests/monitor/__init__.py

Whitespace-only changes.

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3+
"actor_sid": "ACbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
4+
"actor_type": "account",
5+
"actor_url": "https://api.twilio.com/2010-04-01/Accounts/ACbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
6+
"description": null,
7+
"event_date": "2014-10-03T16:48:25Z",
8+
"event_type": "account.updated",
9+
"resource_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
10+
"resource_type": "account",
11+
"resource_url": "https://api.twilio.com/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12+
"sid": "AEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13+
"url": "https://monitor.twilio.com/v1/Events/AEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
14+
"source": "api",
15+
"source_ip_address": "10.86.6.250",
16+
"event_data": {
17+
"friendly_name": {
18+
"previous": "SubAccount Created at 2014-10-03 09:48am",
19+
"updated": "Mr. Friendly"
20+
}
21+
}
22+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"meta": {
3+
"key": "events",
4+
"page": 0,
5+
"page_size": 50,
6+
"first_page_url": "https://monitor.twilio.com/v1/Events?PageSize=50&Page=0",
7+
"previous_page_url": null,
8+
"url": "https://monitor.twilio.com/v1/Events?PageSize=50&Page=0",
9+
"next_page_url": "https://monitor.twilio.com/v1/Events?PageSize=50&Page=1&PageToken=ENeyJTaWQiOiAiRVZhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYSIsICJUaW1lc3RhbXAiOiAiMTQyMzI2OTE2MSIsICJXb3Jrc3BhY2VTaWQiOiAiV0FhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYSJ9"
10+
},
11+
"events": [
12+
{
13+
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
14+
"actor_sid": "ACbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
15+
"actor_type": "account",
16+
"actor_url": "https://api.twilio.com/2010-04-01/Accounts/ACbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
17+
"description": null,
18+
"event_date": "2014-10-03T16:48:25Z",
19+
"event_type": "account.updated",
20+
"resource_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
21+
"resource_type": "account",
22+
"resource_url": "https://api.twilio.com/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
23+
"sid": "AEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
24+
"url": "https://monitor.twilio.com/v1/Events/AEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
25+
"source": "api",
26+
"source_ip_address": "10.86.6.250",
27+
"event_data": {
28+
"friendly_name": {
29+
"previous": "SubAccount Created at 2014-10-03 09:48am",
30+
"updated": "Mr. Friendly"
31+
}
32+
}
33+
}
34+
]
35+
}

twilio/rest/monitor.py

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

twilio/rest/resources/monitor/__init__.py

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

0 commit comments

Comments
 (0)
0