File: phone_number_orders_sample_async.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (132 lines) | stat: -rw-r--r-- 6,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: phone_number_orders_sample_async.py
DESCRIPTION:
    This sample demonstrates how to list, acquire and cancel phone number orders via a connection string,
    reservation id, phone plan id and and area code.
USAGE:
    python phone_number_orders_sample.py
    Set the environment variables with your own values before running the sample:
    1) AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING - The endpoint of your Azure Communication Service
    2) AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_RELEASE_ID - The release id you want to get info from
    3) AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_RESERVATION_ID - The reservation id you want to get info from
    4) AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_AREA_CODE_FOR_RESERVATION - The area code to create reservation
    5) AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_PHONE_PLAN_ID - The phone plan id
    6) AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_RESERVATION_ID_TO_PURCHASE - The reservation id you want to purchase
    7) AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_RESERVATION_ID_TO_CANCEL - The reservation id you want to cancel
"""

import os
import asyncio
from azure.communication.administration.aio import PhoneNumberAdministrationClient
from azure.communication.administration import CreateSearchOptions

connection_str = os.getenv('AZURE_COMMUNICATION_SERVICE_CONNECTION_STRING')
phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str)
release_id = os.getenv('AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_RELEASE_ID', "release-id")
reservation_id = os.getenv('AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_RESERVATION_ID', "reservation-id")
area_code_for_reservation = os.getenv('AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_AREA_CODE_FOR_RESERVATION', "area-code")
phone_plan_id = os.getenv('AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_PHONE_PLAN_ID', "phone-plan-id")
reservation_id_to_purchase = os.getenv('AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_RESERVATION_ID_TO_PURCHASE',
                                       "reservation-id")
reservation_id_to_cancel = os.getenv('AZURE_COMMUNICATION_SERVICE_PHONENUMBERS_RESERVATION_ID_TO_CANCEL',
                                     "reservation-id")


async def get_release_by_id():
    # [START get_release_by_id]
    phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str)
    async with phone_number_administration_client:
        phone_number_release_response = await phone_number_administration_client.get_release_by_id(
            release_id=release_id
        )
        print('phone_number_release_response:')
        print(phone_number_release_response)
    # [END get_release_by_id]


async def list_all_releases():
    # [START list_all_releases]
    phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str)
    async with phone_number_administration_client:
        releases_response = phone_number_administration_client.list_all_releases()
        print('releases_response:')
        async for release in releases_response:
            print(release)
    # [END list_all_releases]


async def get_reservation_by_id():
    # [START get_reservation_by_id]
    phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str)
    async with phone_number_administration_client:
        phone_number_reservation_response = await phone_number_administration_client.get_reservation_by_id(
            reservation_id=reservation_id
        )
        print('phone_number_reservation_response:')
        print(phone_number_reservation_response)
    await phone_number_administration_client.close()
    # [END get_reservation_by_id]


async def begin_reserve_phone_numbers():
    # [START begin_reserve_phone_numbers]
    phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str)
    reservationOptions = CreateSearchOptions(
        area_code=area_code_for_reservation,
        description="testreservation20200014",
        display_name="testreservation20200014",
        phone_plan_ids=[phone_plan_id],
        quantity=1
    )
    async with phone_number_administration_client:
        reserve_phone_numbers_poller = await phone_number_administration_client.begin_reserve_phone_numbers(
            area_code=area_code_for_reservation,
            description="testreservation20200014",
            display_name="testreservation20200014",
            phone_plan_ids=[phone_plan_id],
            quantity=1
        )
        print('reserve phone numbers status:')
        print(reserve_phone_numbers_poller.status())
    # [END begin_reserve_phone_numbers]


async def cancel_reservation():
    # [START cancel_reservation]
    phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str)
    async with phone_number_administration_client:
        await phone_number_administration_client.cancel_reservation(
            reservation_id=reservation_id_to_cancel
        )
    # [END cancel_reservation]


async def begin_purchase_reservation():
    # [START begin_purchase_reservation]
    phone_number_administration_client = PhoneNumberAdministrationClient.from_connection_string(connection_str)
    async with phone_number_administration_client:
        await phone_number_administration_client.begin_purchase_reservation(
            reservation_id=reservation_id_to_purchase
        )
    # [END begin_purchase_reservation]


async def main():
    await get_release_by_id()
    await list_all_releases()
    await get_reservation_by_id()
    await begin_reserve_phone_numbers()
    await cancel_reservation()
    # await begin_purchase_reservation() #currently throws error if purchase an already purchased number

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())