8000 Usage records support · Stackdriver/twilio-python@3ccebf9 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 1, 2018. It is now read-only.

Commit 3ccebf9

Browse files
committed
Usage records support
1 parent e1a934d commit 3ccebf9

File tree

4 files changed

+144
-0
lines changed
  • twilio/rest/resources
  • 4 files changed

    +144
    -0
    lines changed
    Lines changed: 65 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,65 @@
    1+
    {
    2+
    "page": 0,
    3+
    "num_pages": 1,
    4+
    "page_size": 50,
    5+
    "total": 2,
    6+
    "start": 0,
    7+
    "end": 1,
    8+
    "uri": "\/2010-04-01\/Accounts\/AC4bf2dafbed59a5733d2c1c1c69a83a28\/Usage/Records.json?Page=0&PageSize=50",
    9+
    "previous_page_uri": null,
    10+
    "next_page_uri": "\/2010-04-01\/Accounts\/AC4bf2dafbed59a5733d2c1c1c69a83a28\/Calls.json?Page=1&PageSize=50",
    11+
    "last_page_uri": "\/2010-04-01\/Accounts\/AC4bf2dafbed59a5733d2c1c1c69a83a28\/Calls.json?Page=10&PageSize=50",
    12+
    "first_page_uri": "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records.json?Page=0&PageSize=50",
    13+
    "usage_records": [
    14+
    {
    15+
    "category": "calleridlookups",
    16+
    "subresource_uris": [
    17+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/AllTime.json?Category=calleridlookups",
    18+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/Today.json?Category=calleridlookups",
    19+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/Yesterday.json?Category=calleridlookups",
    20+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/ThisMonth.json?Category=calleridlookups",
    21+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/LastMonth.json?Category=calleridlookups",
    22+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/ThisYear.json?Category=calleridlookups",
    23+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/LastYear.json?Category=calleridlookups",
    24+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/Daily.json?Category=calleridlookups",
    25+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/Monthly.json?Category=calleridlookups",
    26+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/Yearly.json?Category=calleridlookups"
    27+
    ],
    28+
    "description": "Caller ID lookups",
    29+
    "end_date": "2012-09-29",
    30+
    "usage_unit": "Lookups",
    31+
    "price": "0",
    32+
    "uri": "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records.json?Category=calleridlookups&StartDate=2012-09-22&EndDate=2012-09-29",
    33+
    "number": "0",
    34+
    "account_sid": "ACed70abd024d3f57a4027b5dc2ca88d5b",
    35+
    "trigger_uri": "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Triggers.json?UsageCategory=calleridlookups",
    36+
    "usage": "0",
    37+
    "start_date": "2012-09-22"
    38+
    },
    39+
    {
    40+
    "category": "recordings",
    41+
    "subresource_uris": [
    42+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/AllTime.json?Category=recordings",
    43+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/Today.json?Category=recordings",
    44+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/Yesterday.json?Category=recordings",
    45+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/ThisMonth.json?Category=recordings",
    46+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/LastMonth.json?Category=recordings",
    47+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/ThisYear.json?Category=recordings",
    48+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/LastYear.json?Category=recordings",
    49+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/Daily.json?Category=recordings",
    50+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/Monthly.json?Category=recordings",
    51+
    "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records/Yearly.json?Category=recordings"
    52+
    ],
    53+
    "description": "Call recordings",
    54+
    "end_date": "2012-09-29",
    55+
    "usage_unit": "Minutes",
    56+
    "price": "0",
    57+
    "uri": "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Records.json?Category=recordings&StartDate=2012-09-22&EndDate=2012-09-29",
    58+
    "number": "0",
    59+
    "account_sid": "ACed70abd024d3f57a4027b5dc2ca88d5b",
    60+
    "trigger_uri": "/2010-04-01/Accounts/ACed70abd024d3f57a4027b5dc2ca88d5b/Usage/Triggers.json?UsageCategory=recordings",
    61+
    "usage": "0",
    62+
    "start_date": "2012-09-22"
    63+
    }
    64+
    ]
    65+
    }

    tests/test_usage_records.py

    Lines changed: 43 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,43 @@
    1+
    from mock import patch
    2+
    from nose.tools import raises
    3+
    from tools import create_mock_json
    4+
    from twilio.rest.resources import UsageRecords
    5+
    6+
    BASE_URI = "https://api.twilio.com/2010-04-01/Accounts/AC123"
    7+
    ACCOUNT_SID = "AC123"
    8+
    AUTH = (ACCOUNT_SID, "token")
    9+
    10+
    list_resource = UsageRecords(BASE_URI, AUTH)
    11+
    12+
    13+
    @patch("twilio.rest.resources.base.make_twilio_request")
    14+
    def test_paging(request):
    15+
    resp = create_mock_json("tests/resources/usage_records_list.json")
    16+
    request.return_value = resp
    17+
    18+
    uri = "%s/Usage/Records" % BASE_URI
    19+
    list_resource.list(
    20+
    start_date="2012-10-12",
    21+
    end_date="2012-10-13",
    22+
    category="sms")
    23+
    24+
    request.assert_called_with("GET", uri, params={
    25+
    "StartDate": "2012-10-12",
    26+
    "EndDate": "2012-10-13",
    27+
    "Category": "sms"
    28+
    }, auth=AUTH)
    29+
    30+
    31+
    @raises(AttributeError)
    32+
    def test_create():
    33+
    list_resource.create
    34+
    35+
    36+
    @raises(AttributeError)
    37+
    def test_delete():
    38+
    list_resource.delete
    39+
    40+
    41+
    @raises(AttributeError)
    42+
    def test_get():
    43+
    list_resource.get('abc')

    twilio/rest/resources/__init__.py

    Lines changed: 2 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -47,3 +47,5 @@
    4747
    from twilio.rest.resources.accounts import Account, Accounts
    4848

    4949
    from twilio.rest.resources.usage_triggers import UsageTriggers
    50+
    51+
    from twilio.rest.resources.usage_records import UsageRecords
    Lines changed: 34 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,34 @@
    1+
    from twilio.rest.resources import InstanceResource, ListResource
    2+
    3+
    4+
    class UsageRecord(InstanceResource):
    5+
    """ A usage record resource """
    6+
    7+
    8+
    class UsageRecords(ListResource):
    9+
    name = "Usage/Records"
    10+
    instance = UsageRecord
    11+
    key = "usage_records"
    12+
    13+
    def list(self, **kwargs):
    14+
    """
    15+
    Returns a page of :class:`UsageRecord` resources as a list. For paging
    16+
    information see :class:`ListResource`
    17+
    18+
    :param category: Only include usage of this usage category.
    19+
    :param start_date: Only include usage that has occurred on or after
    20+
    this date. Format is YYYY-MM-DD. All dates are in
    21+
    GMT.
    22+
    :param end_date: Only include usage that has occurred on or before this
    23+
    date. Format is YYYY-MM-DD. All dates are in GMT.
    24+
    """
    25+
    return self.get_instances(kwargs)
    26+
    27+
    def get(self, *args, **kwargs):
    28+
    raise AttributeError('Unsupported in UsageRecords')
    29+
    30+
    def load_instance(self, data):
    31+
    instance = self.instance(self, "Resource")
    32+
    instance.load(data)
    33+
    instance.load_subresources()
    34+
    return instance

    0 commit comments

    Comments
     (0)
    0