本文整理匯總了Python中twilio.base.exceptions.TwilioRestException方法的典型用法代碼示例。如果您正苦於以下問題:Python exceptions.TwilioRestException方法的具體用法?Python exceptions.TwilioRestException怎麽用?Python exceptions.TwilioRestException使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類twilio.base.exceptions
的用法示例。
在下文中一共展示了exceptions.TwilioRestException方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_exception_is_logged_when_raised
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_exception_is_logged_when_raised(client, mocker, backend):
with override_settings(PHONE_VERIFICATION=backend):
mock_send_verification = mocker.patch(
"phone_verify.services.PhoneVerificationService.send_verification"
)
mock_logger = mocker.patch("phone_verify.services.logger")
backend_cls = _get_backend_cls(backend)
if (
backend_cls == "nexmo.NexmoBackend"
or backend_cls == "nexmo.NexmoSandboxBackend"
):
exc = ClientError()
mock_send_verification.side_effect = exc
elif (
backend_cls == "twilio.TwilioBackend"
or backend_cls == "twilio.TwilioSandboxBackend"
):
exc = TwilioRestException(status=mocker.Mock(), uri=mocker.Mock())
mock_send_verification.side_effect = exc
send_security_code_and_generate_session_token(phone_number="+13478379634")
mock_logger.error.assert_called_once_with(
f"Error in sending verification code to +13478379634: {exc}"
)
示例2: post_receive
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def post_receive(self, alert):
if alert.repeat:
return
message = "%s: %s alert for %s - %s is %s" % (
alert.environment, alert.severity.capitalize(),
','.join(alert.service), alert.resource, alert.event
)
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
for twilio_to in TWILIO_TO_NUMBER.split(','):
LOG.debug('Twilio SMS: Send message from {}, to {}'.format(TWILIO_FROM_NUMBER, twilio_to))
try:
message = client.messages.create(body=message, to=twilio_to, from_=TWILIO_FROM_NUMBER)
except TwilioRestException as e:
LOG.error('Twilio SMS: ERROR - {}'.format(str(e)))
else:
LOG.info("Twilio SMS: Message ID: %s", message.sid)
示例3: _send_text
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def _send_text(self, recipient_id, text):
from twilio.base.exceptions import TwilioRestException
message = None
try:
while not message and self.send_retry < self.max_retry:
message = self.messages.create(body=text,
to=recipient_id,
from_=self.twilio_number)
self.send_retry += 1
except TwilioRestException as e:
logger.error("Something went wrong " + repr(e.msg))
finally:
self.send_retry = 0
if not message and self.send_retry == self.max_retry:
logger.error("Failed to send message. Max number of "
"retires exceeded.")
return message
示例4: form_valid
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def form_valid(self, form):
try:
with transaction.atomic():
return super().form_valid(form)
except TwilioRestException as e:
if e.status >= 500:
logger.exception(str(e))
msg = TWILIO_ERRORS.get('default')
else:
msg = TWILIO_ERRORS.get(e.code)
if msg:
form.add_error('phone', msg)
return self.form_invalid(form)
else:
raise
示例5: test_update_invalid_phone
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_update_invalid_phone(self, send_sms):
self.user.email = None
self.user.phone = '+12345678990'
self.user.phone_verified = True
self.user.save()
send_sms.side_effect = TwilioRestException(
status=400,
uri='http://localhost:8000',
msg=('Unable to create record: The "To" number +15555555555 is '
'not a valid phone number.'),
method='POST',
code=21211
)
data = {'phone': '+15555555555', 'username': 'imagine72'}
response = self.request(method='PUT', post_data=data, user=self.user)
assert response.status_code == 400
assert TWILIO_ERRORS[21211] in response.content['phone']
assert VerificationDevice.objects.filter(
unverified_phone='+15555555555').exists() is False
self.user.refresh_from_db()
assert self.user.username != 'imagine72'
assert self.user.phone == '+12345678990'
assert self.user.phone_verified is True
示例6: test_update_twilio_error_400
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_update_twilio_error_400(self, send_sms):
self.user.email = None
self.user.phone = '+12345678990'
self.user.phone_verified = True
self.user.save()
send_sms.side_effect = TwilioRestException(
status=400,
uri='http://localhost:8000',
msg=('Account not active'),
method='POST',
code=20005
)
data = {'phone': '+15555555555', 'username': 'imagine72'}
with pytest.raises(TwilioRestException):
self.request(method='PUT', post_data=data, user=self.user)
assert VerificationDevice.objects.filter(
unverified_phone='+15555555555').exists() is False
self.user.refresh_from_db()
assert self.user.username != 'imagine72'
assert self.user.phone == '+12345678990'
assert self.user.phone_verified is True
示例7: test_update_twilio_error_500
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_update_twilio_error_500(self, send_sms):
self.user.email = None
self.user.phone = '+12345678990'
self.user.phone_verified = True
self.user.save()
send_sms.side_effect = TwilioRestException(
status=500,
uri='http://localhost:8000',
msg=('Account not active'),
method='POST',
code=20005
)
data = {'phone': '+15555555555', 'username': 'imagine72'}
response = self.request(method='PUT', post_data=data, user=self.user)
assert response.status_code == 400
assert TWILIO_ERRORS['default'] in response.content['phone']
assert VerificationDevice.objects.filter(
unverified_phone='+15555555555').exists() is False
self.user.refresh_from_db()
assert self.user.username != 'imagine72'
assert self.user.phone == '+12345678990'
assert self.user.phone_verified is True
示例8: test_signup_invalid_phone
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_signup_invalid_phone(self, send_sms):
send_sms.side_effect = TwilioRestException(
status=400,
uri='http://localhost:8000',
msg=('Unable to create record: The "To" number +15555555555 is '
'not a valid phone number.'),
method='POST',
code=21211
)
data = {
'username': 'sherlock',
'email': '',
'phone': '+15555555555',
'password': '221B@bakerstreet',
'full_name': 'Sherlock Holmes'
}
response = self.request(method='POST', post_data=data)
assert response.status_code == 400
assert TWILIO_ERRORS[21211] in response.content['phone']
assert VerificationDevice.objects.filter(
unverified_phone='+15555555555').exists() is False
assert User.objects.count() == 0
示例9: test_signup_twilio_error_400
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_signup_twilio_error_400(self, send_sms):
send_sms.side_effect = TwilioRestException(
status=400,
uri='http://localhost:8000',
msg=('Account not active'),
method='POST',
code=20005
)
data = {
'username': 'sherlock',
'email': '',
'phone': '+15555555555',
'password': '221B@bakerstreet',
'full_name': 'Sherlock Holmes'
}
with pytest.raises(TwilioRestException):
self.request(method='POST', post_data=data)
assert VerificationDevice.objects.filter(
unverified_phone='+15555555555').exists() is False
assert User.objects.count() == 0
示例10: test_sign_up_with_invalid_phone_number
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_sign_up_with_invalid_phone_number(self, send_sms):
send_sms.side_effect = TwilioRestException(
status=400,
uri='http://localhost:8000',
msg=('Unable to create record: The "To" number +15555555555 is '
'not a valid phone number.'),
method='POST',
code=21211
)
data = {
'username': 'sherlock',
'phone': '+15555555555',
'password': '221B@bakerstreet',
'full_name': 'Sherlock Holmes',
'language': 'en'
}
response = self.request(method='POST', post_data=data)
assert response.status_code == 200
assert TWILIO_ERRORS[21211] in response.content
assert User.objects.count() == 0
assert VerificationDevice.objects.count() == 0
assert len(mail.outbox) == 0
示例11: test_twilio_error_400
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_twilio_error_400(self, send_sms):
send_sms.side_effect = TwilioRestException(
status=400,
uri='http://localhost:8000',
msg=('Account not active'),
method='POST',
code=20005
)
data = {
'username': 'sherlock',
'phone': '+15555555555',
'password': '221B@bakerstreet',
'full_name': 'Sherlock Holmes',
'language': 'en'
}
with pytest.raises(TwilioRestException):
self.request(method='POST', post_data=data)
assert User.objects.count() == 0
assert VerificationDevice.objects.count() == 0
assert len(mail.outbox) == 0
示例12: test_twilio_error_500
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_twilio_error_500(self, send_sms):
send_sms.side_effect = TwilioRestException(
status=500,
uri='http://localhost:8000',
msg=('Account not active'),
method='POST',
code=20005
)
data = {
'username': 'sherlock',
'phone': '+15555555555',
'password': '221B@bakerstreet',
'full_name': 'Sherlock Holmes',
'language': 'en'
}
response = self.request(method='POST', post_data=data)
assert response.status_code == 200
assert TWILIO_ERRORS['default'] in response.content
self.request(method='POST', post_data=data)
assert User.objects.count() == 0
assert VerificationDevice.objects.count() == 0
assert len(mail.outbox) == 0
示例13: test_update_profile_with_invalid_phone
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_update_profile_with_invalid_phone(self, send_sms):
send_sms.side_effect = TwilioRestException(
status=400,
uri='http://localhost:8000',
msg=('Unable to create record: The "To" number +15555555555 is '
'not a valid phone number.'),
method='POST',
code=21211
)
user = UserFactory.create(password='221B@bakerstreet')
post_data = {
'username': 'new_name',
'email': user.email,
'phone': '+919327768250',
'language': 'en',
'measurement': 'metric',
'full_name': 'Sherlock Holmes',
'password': '221B@bakerstreet'
}
response = self.request(method='POST', post_data=post_data, user=user)
assert response.status_code == 200
assert TWILIO_ERRORS[21211] in response.content
assert VerificationDevice.objects.count() == 0
user.refresh_from_db()
assert user.username != 'new_name'
示例14: test_send_token_with_invalid_phone
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def test_send_token_with_invalid_phone(self, send_sms):
send_sms.side_effect = TwilioRestException(
status=400,
uri='http://localhost:8000',
msg=('Unable to create record: The "To" number +15555555555 is '
'not a valid phone number.'),
method='POST',
code=21211
)
VerificationDevice.objects.create(user=self.user,
unverified_phone=self.user.phone)
data = {
'phone': '+919327768250',
}
response = self.request(method='POST', post_data=data)
assert response.status_code == 200
assert TWILIO_ERRORS[21211] in response.content
示例15: update
# 需要導入模塊: from twilio.base import exceptions [as 別名]
# 或者: from twilio.base.exceptions import TwilioRestException [as 別名]
def update(self, request, *args, **kwargs):
try:
with transaction.atomic():
return super().update(request, *args, **kwargs)
except TwilioRestException as e:
if e.status >= 500:
logger.exception(str(e))
msg = messages.TWILIO_ERRORS.get('default')
else:
msg = messages.TWILIO_ERRORS.get(e.code)
if msg:
return Response(status=400, data={'phone': msg})
else:
raise