8000 Make PhoneNumbers.update_instance consistent with PhoneNumber.update by ryanhorn · Pull Request #105 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

Make PhoneNumbers.update_instance consistent with PhoneNumber.update #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ nose
simplejson
unittest2
coverage
nosexcover
nosexcover
six
13 changes: 13 additions & 0 deletions tests/test_phone_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ def setUp(self):
self.uri = "/base"
self.auth = ("AC123", "token")

def test_update_rename_status_callback_url(self):
mock = Mock()
mock.uri = "/base"
instance = PhoneNumber(mock, "SID")
instance.update(status_callback_url="http://www.example.com")
mock.update.assert_called_with("SID", status_callback="http://www.example.com")

def test_update_instance_rename_status_callback_url(self):
resource = PhoneNumbers(self.uri, self.auth)
resource.update_instance = Mock()
resource.update("SID", status_callback_url="http://www.example.com")
resource.update_instance.assert_called_with("SID", {"status_callback": "http://www.example.com"})

def test_application_sid(self):
resource = PhoneNumbers(self.uri, self.auth)
resource.update_instance = Mock()
Expand Down
23 changes: 13 additions & 10 deletions twilio/rest/resources/phone_numbers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re

from twilio import TwilioException
from twilio.rest.resources.util import transform_params
from twilio.rest.resources.util import change_dict_key, transform_params
from twilio.rest.resources import InstanceResource, ListResource


Expand Down Expand Up @@ -79,14 +79,14 @@ def transfer(self, account_sid):
a = self.parent.transfer(self.name, account_sid)
self.load(a.__dict__)

def update(self, status_callback_url=None, **kwargs):
def update(self, **kwargs):
"""
Update this phone number instance.
"""
kwargs["StatusCallback"] = kwargs.get("status_callback",
status_callback_url)
kwargs_copy = dict(kwargs)
change_dict_key(kwargs_copy, from_key="status_callback_url", to_key="status_callback")

a = self.parent.update(self.name, **kwargs)
a = self.parent.update(self.name, **kwargs_copy)
self.load(a.__dict__)

def delete(self):
Expand Down Expand Up @@ -170,9 +170,12 @@ def update(self, sid, **kwargs):
"""
Update this phone number instance
"""
if "application_sid" in kwargs:
kwargs_copy = dict(kwargs)
change_dict_key(kwargs_copy, from_key="status_callback_url", to_key="status_callback")

if "application_sid" in kwargs_copy:
for sid_type in ["voice_application_sid", "sms_application_sid"]:
if sid_type not in kwargs:
kwargs[sid_type] = kwargs["application_sid"]
del kwargs["application_sid"]
return self.update_instance(sid, kwargs)
if sid_type not in kwargs_copy:
kwargs_copy[sid_type] = kwargs_copy["application_sid"]
del kwargs_copy["application_sid"]
return self.update_instance(sid, kwargs_copy)
14 changes: 14 additions & 0 deletions twilio/rest/resources/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,17 @@ def inner_func(*args, **kwargs):
inner_func.__doc__ = myfunc.__doc__
inner_func.__repr__ = myfunc.__repr__
return inner_func

def change_dict_key(d, from_key, to_key):
"""
Changes a dictionary's key from from_key to to_key. No-op if the key does not exist.

:param d: Dictionary with key to change
:param from_key: Old key
:param to_key: New key
:return: None
"""
try:
d[to_key] = d.pop(from_key)
except KeyError:
pass
0