Ejemplo n.º 1
0
 async def test_list_acquired_phone_numbers_from_managed_identity(self):
     endpoint, access_key = parse_connection_str(self.connection_str)
     credential = create_token_credential()
     phone_number_client = PhoneNumbersClient(endpoint, credential)
     async with self.phone_number_client:
         phone_numbers = phone_number_client.list_acquired_phone_numbers()
         items = []
         async for item in phone_numbers:
             items.append(item)
     assert len(items) > 0
 async def test_list_purchased_phone_numbers_from_managed_identity(self):
     endpoint, access_key = parse_connection_str(self.connection_str)
     credential = create_token_credential()
     phone_number_client = PhoneNumbersClient(
         endpoint,
         credential,
         http_logging_policy=get_http_logging_policy())
     async with phone_number_client:
         phone_numbers = phone_number_client.list_purchased_phone_numbers()
         items = []
         async for item in phone_numbers:
             items.append(item)
     assert len(items) > 0
 async def test_purchase_phone_numbers_from_managed_identity(self):
     endpoint, access_key = parse_connection_str(self.connection_str)
     credential = create_token_credential()
     phone_number_client = PhoneNumbersClient(
         endpoint,
         credential,
         http_logging_policy=get_http_logging_policy())
     capabilities = PhoneNumberCapabilities(
         calling=PhoneNumberCapabilityType.INBOUND,
         sms=PhoneNumberCapabilityType.INBOUND_OUTBOUND)
     async with phone_number_client:
         search_poller = await phone_number_client.begin_search_available_phone_numbers(
             self.country_code,
             PhoneNumberType.TOLL_FREE,
             PhoneNumberAssignmentType.APPLICATION,
             capabilities,
             polling=True)
         phone_number_to_buy = await search_poller.result()
         purchase_poller = await phone_number_client.begin_purchase_phone_numbers(
             phone_number_to_buy.search_id, polling=True)
         await purchase_poller.result()
         release_poller = await phone_number_client.begin_release_phone_number(
             phone_number_to_buy.phone_numbers[0])
     assert release_poller.status(
     ) == PhoneNumberOperationStatus.SUCCEEDED.value
 def _get_managed_identity_phone_number_client(self):
     endpoint, access_key = parse_connection_str(self.connection_str)
     credential = async_create_token_credential()
     return PhoneNumbersClient(
         endpoint,
         credential,
         http_logging_policy=get_http_logging_policy(),
         headers_policy=get_header_policy())
 async def test_get_purchased_phone_number_from_managed_identity(self):
     endpoint, access_key = parse_connection_str(self.connection_str)
     credential = create_token_credential()
     phone_number_client = PhoneNumbersClient(endpoint, credential)
     async with phone_number_client:
         phone_number = await phone_number_client.get_purchased_phone_number(
             self.phone_number)
     assert phone_number.phone_number == self.phone_number
 async def test_update_phone_number_capabilities_from_managed_identity(
         self):
     endpoint, access_key = parse_connection_str(self.connection_str)
     credential = create_token_credential()
     phone_number_client = PhoneNumbersClient(endpoint, credential)
     async with phone_number_client:
         poller = await phone_number_client.begin_update_phone_number_capabilities(
             self.phone_number,
             PhoneNumberCapabilityType.INBOUND_OUTBOUND,
             PhoneNumberCapabilityType.INBOUND,
             polling=True)
     assert poller.result()
 async def test_search_available_phone_numbers_from_managed_identity(self):
     endpoint, access_key = parse_connection_str(self.connection_str)
     credential = create_token_credential()
     phone_number_client = PhoneNumbersClient(endpoint, credential)
     capabilities = PhoneNumberCapabilities(
         calling=PhoneNumberCapabilityType.INBOUND,
         sms=PhoneNumberCapabilityType.INBOUND_OUTBOUND)
     async with phone_number_client:
         poller = await phone_number_client.begin_search_available_phone_numbers(
             self.country_code,
             PhoneNumberType.TOLL_FREE,
             PhoneNumberAssignmentType.APPLICATION,
             capabilities,
             polling=True)
     assert poller.result()
Ejemplo n.º 8
0
 def setUp(self):
     super(PhoneNumbersClientTestAsync, self).setUp()
     if self.is_playback():
         self.phone_number = "sanitized"
         self.country_code = "US"
     else:
         self.phone_number = os.getenv("AZURE_COMMUNICATION_SERVICE_PHONE_NUMBER")
         self.country_code = os.getenv("AZURE_COMMUNICATION_SERVICE_COUNTRY_CODE", "US")
     self.phone_number_client = PhoneNumbersClient.from_connection_string(self.connection_str)
     self.recording_processors.extend([
         BodyReplacerProcessor(
             keys=["id", "token", "phoneNumber", "searchId"]
         ),
         PhoneNumberUriReplacer(),
         ResponseReplacerProcessor()])
 async def test_update_phone_number_capabilities_from_managed_identity(self):
     endpoint, access_key = parse_connection_str(self.connection_str)
     credential = async_create_token_credential()
     phone_number_client = PhoneNumbersClient(
         endpoint, 
         credential, 
         http_logging_policy=get_http_logging_policy()
     )
     async with phone_number_client:
         current_phone_number = await phone_number_client.get_purchased_phone_number(self.phone_number)
         calling_capabilities = PhoneNumberCapabilityType.INBOUND if current_phone_number.capabilities.calling == PhoneNumberCapabilityType.OUTBOUND else PhoneNumberCapabilityType.OUTBOUND
         sms_capabilities = PhoneNumberCapabilityType.INBOUND_OUTBOUND if current_phone_number.capabilities.sms == PhoneNumberCapabilityType.OUTBOUND else PhoneNumberCapabilityType.OUTBOUND
         poller = await phone_number_client.begin_update_phone_number_capabilities(
             self.phone_number,
             sms_capabilities,
             calling_capabilities,
             polling = True
         )
         assert await poller.result()
         assert poller.status() == PhoneNumberOperationStatus.SUCCEEDED.value
DESCRIPTION:
    This sample demonstrates how to get the information from an acquired phone number using your connection string
USAGE:
    python get_purchased_phone_number_sample.py
    Set the environment variables with your own values before running the sample:
    1) COMMUNICATION_SAMPLES_CONNECTION_STRING - The connection string including your endpoint and 
        access key of your Azure Communication Service
    2) AZURE_PHONE_NUMBER - The phone number you want to get its information
"""

import asyncio
import os
from azure.communication.phonenumbers.aio import (PhoneNumbersClient)

connection_str = os.getenv('COMMUNICATION_SAMPLES_CONNECTION_STRING')
phone_number = os.getenv("AZURE_PHONE_NUMBER")  # e.g. "+18001234567"
phone_numbers_client = PhoneNumbersClient.from_connection_string(
    connection_str)


async def get_purchased_phone_number_information():
    async with phone_numbers_client:
        purchased_phone_number_information = await phone_numbers_client.get_purchased_phone_number(
            phone_number)
    print('Phone number: ' + purchased_phone_number_information.phone_number)
    print('Country code: ' + purchased_phone_number_information.country_code)


if __name__ == '__main__':
    asyncio.run(get_purchased_phone_number_information())