8000 Merge pull request #105 from ryanhorn/phone_numbers_update_consistency · Stackdriver/twilio-python@092372d · 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 092372d

Browse files
committed
Merge pull request twilio#105 from ryanhorn/phone_numbers_update_consistency
Make PhoneNumbers.update_instance consistent with PhoneNumber.update
2 parents d446c40 + ac9805d commit 092372d

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

tests/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ nose
44
simplejson
55
unittest2
66
coverage
7-
nosexcover
7+
nosexcover
8+
six

tests/test_phone_numbers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ def setUp(self):
2121
self.uri = "/base"
2222
self.auth = ("AC123", "token")
2323

24+
def test_update_rename_status_callback_url(self):
25+
mock = Mock()
26+
mock.uri = "/base"
27+
instance = PhoneNumber(mock, "SID")
28+
instance.update(status_callback_url="http://www.example.com")
29+
mock.update.assert_called_with("SID", status_callback="http://www.example.com")
30+
31+
def test_update_instance_rename_status_callback_url(self):
32+
resource = PhoneNumbers(self.uri, self.auth)
33+
resource.update_instance = Mock()
34+
resource.update("SID", status_callback_url="http://www.example.com")
35+
resource.update_instance.assert_called_with("SID", {"status_callback": "http://www.example.com"})
36+
2437
def test_application_sid(self):
2538
resource = PhoneNumbers(self.uri, self.auth)
2639
resource.update_instance = Mock()

twilio/rest/resources/phone_numbers.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22

33
from twilio import TwilioException
4-
from twilio.rest.resources.util import transform_params
4+
from twilio.rest.resources.util import change_dict_key, transform_params
55
from twilio.rest.resources import InstanceResource, ListResource
66

77

@@ -79,14 +79,14 @@ def transfer(self, account_sid):
7979
a = self.parent.transfer(self.name, account_sid)
8080
self.load(a.__dict__)
8181

82-
def update(self, status_callback_url=None, **kwargs):
82+
def update(self, **kwargs):
8383
"""
8484
Update this phone number instance.
8585
"""
86-
kwargs["StatusCallback"] = kwargs.get("status_callback",
87-
status_callback_url)
86+
kwargs_copy = dict(kwargs)
87+
change_dict_key(kwargs_copy, from_key="status_callback_url", to_key="status_callback")
8888

89-
a = self.parent.update(self.name, **kwargs)
89+
a = self.parent.update(self.name, **kwargs_copy)
9090
self.load(a.__dict__)
9191

9292
def delete(self):
@@ -170,9 +170,12 @@ def update(self, sid, **kwargs):
170170
"""
171171
Update this phone number instance
172172
"""
173-
if "application_sid" in kwargs:
173+
kwargs_copy = dict(kwargs)
174+
change_dict_key(kwargs_copy, from_key="status_callback_url", to_key="status_callback")
175+
176+
if "application_sid" in kwargs_copy:
174177
for sid_type in ["voice_application_sid", "sms_application_sid"]:
175-
if sid_type not in kwargs:
176-
kwargs[sid_type] = kwargs["application_sid"]
177-
del kwargs["application_sid"]
178-
return self.update_instance(sid, kwargs)
178+
if sid_type not in kwargs_copy:
179+
kwargs_copy[sid_type] = kwargs_copy["application_sid"]
180+
del kwargs_copy["application_sid"]
181+
return self.update_instance(sid, kwargs_copy)

twilio/rest/resources/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,17 @@ def inner_func(*args, **kwargs):
9090
inner_func.__doc__ = myfunc.__doc__
9191
inner_func.__repr__ = myfunc.__repr__
92< 831A code>92
return inner_func
93+
94+
def change_dict_key(d, from_key, to_key):
95+
"""
96+
Changes a dictionary's key from from_key to to_key. No-op if the key does not exist.
97+
98+
:param d: Dictionary with key to change
99+
:param from_key: Old key
100+
:param to_key: New key
101+
:return: None
102+
"""
103+
try:
104+
d[to_key] = d.pop(from_key)
105+
except KeyError:
106+
pass

0 commit comments

Comments
 (0)
0