8000 Add normalize_next_gen_date decorator · apjfan/twilio-python@ac570c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac570c6

Browse files
author
Mitch Friedman
committed
Add normalize_next_gen_date decorator
The next gen resources use a a different date standard than the older ones so they require a different normalize date decorator. This commit also adds a decorator to the iter method of the next gen resource
1 parent bab926d commit ac570c6

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

tests/test_core.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from twilio.rest.resources import convert_case
1010
from twilio.rest.resources import convert_boolean
1111
from twilio.rest.resources import normalize_dates
12+
from twilio.rest.resources import normalize_next_gen_dates
1213

1314

1415
def test_date():
@@ -72,6 +73,24 @@ def foo(on=None, before=None, after=None):
7273
assert_equal(d["before"], "2009-10-10")
7374

7475

76+
def test_normalize_next_gen_dates():
77+
78+
@normalize_next_gen_dates
79+
def foo(on=None, before=None, after=None):
80+
return {
81+
"on": on,
82+
"before": before,
83+
"after": after,
84+
}
85+
86+
d = foo(on="2015-05-29T14:38:08.225180",
87+
before=datetime(2015, 05, 29, 14, 38, 8, 225180),
88+
after=date(2015, 05, 29))
89+
90+
assert_equal(d["on"], "2015-05-29T14:38:08.225180")
91+
assert_equal(d["before"], "2015-05-29T14:38:08.225180")
92+
assert_equal(d["after"], "2015-05-29")
93+
7594
def test_convert_case():
7695
assert_equal(convert_case("from_"), "From")
7796
assert_equal(convert_case("to"), "To")

twilio/rest/resources/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .util import (
22
transform_params, format_name, parse_date, convert_boolean, convert_case,
3-
convert_keys, normalize_dates, UNSET_TIMEOUT
3+
convert_keys, normalize_dates, UNSET_TIMEOUT, normalize_next_gen_dates,
44
)
55
from .base import (
66
Response, Resource, InstanceResource, ListResource,

twilio/rest/resources/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
transform_params,
2424
UNSET_TIMEOUT,
2525
normalize_dates,
26+
normalize_next_gen_dates,
2627
)
2728

2829
logger = logging.getLogger('twilio')
@@ -454,6 +455,7 @@ class NextGenListResource(ListResource):
454455
def __init__(self, *args, **kwargs):
455456
super(NextGenListResource, self).__init__(*args, **kwargs)
456457

458+
@normalize_next_gen_dates
457459
def iter(self, **kwargs):
458460
""" Return all instance resources using an iterator
459461

twilio/rest/resources/util.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ def parse_date(d):
4848
return d
4949

5050

51+
def parse_next_gen_date(d):
52+
"""
53+
Return a string representation of datetime that the Twilio API understands
54+
Format is YYYY-MM-DDTHH:MM:SS.mmmmmm (ISO 8601)
55+
"""
56+
if isinstance(d, datetime.datetime):
57+
return d.isoformat()
58+
elif isinstance(d, datetime.date):
59+
return str(d)
60+
elif isinstance(d, str):
61+
return d
62+
63+
5164
def parse_rfc2822_date(s):
5265
"""
5366
Parses an RFC 2822 date string and returns a time zone naive datetime
@@ -126,6 +139,18 @@ def inner_func(*args, **kwargs):
126139
return inner_func
127140

128141

142+
def normalize_next_gen_dates(myfunc):
143+
def inner_func(*args, **kwargs):
144+
for k, v in iteritems(kwargs):
145+
res = [True for s in ["after", "before", "on"] if s in k]
146+
if len(res):
147+
kwargs[k] = parse_next_gen_date(v)
148+
return myfunc(*args, **kwargs)
149+
inner_func.__doc__ = myfunc.__doc__
150+
inner_func.__repr__ = myfunc.__repr__
151+
return inner_func
152+
153+
129154
def change_dict_key(d, from_key, to_key):
130155
"""
131156
Changes a dictionary's key from from_key to to_key.

0 commit comments

Comments
 (0)
0