I am trying to delete a phone number and the 911 address associated with it. If I disssasiciate the number on one call, then delete the number on a separate call it works. For some reason when I combine them into a single API call it doesn't work. I have tried putting sleep statements in between and it is inconsistent, it works a couple of times and then fails. I have tried to put it into a while loop and same thing, it works a couple of times then fails several times. When I rerun the same call the second time it works 100% of the time. It seems like it is caching the disassociate method. What am I missing to make this work 100% of time?
class ProviderTwilio:
def __init__(self):
# instantiate env variables
gen_env = MyEnv()
# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = gen_env.get_key('TWILIO_ACCOUNT_SID')
auth_token = gen_env.get_key('TWILIO_AUTH_TOKEN')
self.client = Client(account_sid, auth_token)
def delete_phone_number(self, active_phone_number, customer):
timeout_min = 1 / 2 # 30 seconds
timeout_timer = time.time() + 60 * timeout_min
while True:
time.sleep(5)
LOG.info(f"timer is {time.time()}")
if time.time() > timeout_timer:
return jsonify({"msg": f"Could not complete task."}), 400
break
try:
# 10 number phone digit
phone_number = self.get_phone_sid(active_phone_number)
LOG.info(f"phone number exists")
except IndexError:
return jsonify({"msg": f"Phone number provided does not exist"}), 404
break
# disassociate phone number
LOG.info(f"dissociating phone number {phone_number}")
self.disassociate_ems_address(phone_number)
# if it disassociates the number correctly then the rest works.
# If customer has a 911 Address delete it.
if self.search_ems_address(customer):
ad_sid = self.search_ems_address(customer)
try:
LOG.info(f"deleting 911 for customer {customer}")
self.delete_ems_address(ad_sid)
except TwilioRestException:
continue
LOG.info(f"deleting phone number {phone_number}")
self.client.incoming_phone_numbers(phone_number).delete()
return jsonify({"msg": f"Phone Number {active_phone_number} and 911 address was deleted successfully"}), 200
break