Skip to content

Commit 372e02e

Browse files
authored
Changed interface of list functions for sip routing async client to r… (Azure#29461)
* Changed interface of list functions for sip routing async client to return AsyncPagedIterable. * Fixing comments from review. * Fixing whitespace. * Changed synchronous client to use ItemPaged return instead of Iterable. * Updated changelog.
1 parent 3a0ec18 commit 372e02e

File tree

7 files changed

+179
-101
lines changed

7 files changed

+179
-101
lines changed

sdk/communication/azure-communication-phonenumbers/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Adds missing API version `2022-12-01` to the list of supported API versions.
1414

1515
### Other Changes
16+
- Changed list_routes and list_trunks functions on SIP routing client to return (Async)ItemPaged object.
1617

1718
## 1.1.0b3 (2023-01-10)
1819
- Users can now manage SIP configuration for Direct routing.

sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/_sip_routing_client.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from urllib.parse import urlparse
99

1010
from azure.core.tracing.decorator import distributed_trace
11+
from azure.core.paging import ItemPaged
1112

1213
from._models import SipTrunk, SipTrunkRoute
1314
from ._generated.models import (
@@ -152,35 +153,54 @@ def delete_trunk(
152153
def list_trunks(
153154
self,
154155
**kwargs # type: Any
155-
): # type: (...) -> Iterable[SipTrunk]
156-
"""Retrieves an iterable of currently configured SIP trunks.
156+
): # type: (...) -> ItemPaged[SipTrunk]
157+
"""Retrieves the currently configured SIP trunks.
157158
158159
:returns: Current SIP trunks configuration.
159-
:rtype: Iterable[~azure.communication.siprouting.models.SipTrunk]
160+
:rtype: ItemPaged[~azure.communication.siprouting.models.SipTrunk]
160161
:raises: ~azure.core.exceptions.HttpResponseError
161162
"""
162-
return self._list_trunks_(**kwargs)
163+
def extract_data(config):
164+
list_of_elem = [SipTrunk(
165+
fqdn=k,
166+
sip_signaling_port=v.sip_signaling_port) for k,v in config.trunks.items()]
167+
return None, list_of_elem
168+
169+
# pylint: disable=unused-argument
170+
def get_next(nextLink=None):
171+
return self._rest_service.sip_routing.get(
172+
**kwargs
173+
)
174+
175+
return ItemPaged(get_next, extract_data)
163176

164177
@distributed_trace
165178
def list_routes(
166179
self,
167180
**kwargs # type: Any
168-
): # type: (...) -> Iterable[SipTrunkRoute]
169-
"""Retrieves an iterable of currently configured SIP routes.
181+
): # type: (...) -> ItemPaged[SipTrunkRoute]
182+
"""Retrieves the currently configured SIP routes.
170183
171184
:returns: Current SIP routes configuration.
172-
:rtype: Iterable[~azure.communication.siprouting.models.SipTrunkRoute]
185+
:rtype: ItemPaged[~azure.communication.siprouting.models.SipTrunkRoute]
173186
:raises: ~azure.core.exceptions.HttpResponseError
174187
"""
175-
config = self._rest_service.sip_routing.get(
176-
**kwargs
177-
)
178-
return [SipTrunkRoute(
179-
description=x.description,
180-
name=x.name,
181-
number_pattern=x.number_pattern,
182-
trunks=x.trunks
183-
) for x in config.routes]
188+
189+
def extract_data(config):
190+
list_of_elem = [SipTrunkRoute(
191+
description=x.description,
192+
name=x.name,
193+
number_pattern=x.number_pattern,
194+
trunks=x.trunks) for x in config.routes]
195+
return None, list_of_elem
196+
197+
# pylint: disable=unused-argument
198+
def get_next(nextLink=None):
199+
return self._rest_service.sip_routing.get(
200+
**kwargs
201+
)
202+
203+
return ItemPaged(get_next, extract_data)
184204

185205
@distributed_trace
186206
def set_trunks(

sdk/communication/azure-communication-phonenumbers/azure/communication/phonenumbers/siprouting/aio/_sip_routing_client_async.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from typing import TYPE_CHECKING # pylint: disable=unused-import
88
from urllib.parse import urlparse
99

10+
from azure.core.async_paging import AsyncItemPaged, AsyncList
11+
12+
from azure.core.tracing.decorator import distributed_trace
1013
from azure.core.tracing.decorator_async import distributed_trace_async
1114

1215
from .._models import SipTrunk, SipTrunkRoute
@@ -23,7 +26,7 @@
2326
from ..._version import SDK_MONIKER
2427

2528
if TYPE_CHECKING:
26-
from typing import Optional, Iterable, List, Any
29+
from typing import List, Any
2730
from azure.core.credentials_async import AsyncTokenCredential
2831

2932

@@ -152,39 +155,59 @@ async def delete_trunk(
152155
body=SipConfiguration(trunks={trunk_fqdn:None}),
153156
**kwargs)
154157

155-
@distributed_trace_async
156-
async def list_trunks(
158+
@distributed_trace
159+
def list_trunks(
157160
self,
158161
**kwargs # type: Any
159-
): # type: (...) -> Iterable[SipTrunk]
160-
"""Retrieves an iterable of currently configured SIP trunks.
162+
): # type: (...) -> AsyncItemPaged[SipTrunk]
163+
"""Retrieves list of currently configured SIP trunks.
161164
162165
:returns: Current SIP trunks configuration.
163-
:rtype: Iterable[~azure.communication.siprouting.models.SipTrunk]
166+
:rtype: AsyncItemPaged[~azure.communication.siprouting.models.SipTrunk]
164167
:raises: ~azure.core.exceptions.HttpResponseError
165168
"""
166-
return await self._list_trunks_(**kwargs)
167169

168-
@distributed_trace_async
169-
async def list_routes(
170+
async def extract_data(config):
171+
list_of_elem = [SipTrunk(
172+
fqdn=k,
173+
sip_signaling_port=v.sip_signaling_port) for k,v in config.trunks.items()]
174+
return None, AsyncList(list_of_elem)
175+
176+
# pylint: disable=unused-argument
177+
async def get_next(nextLink=None):
178+
return await self._rest_service.sip_routing.get(
179+
**kwargs
180+
)
181+
182+
return AsyncItemPaged(get_next, extract_data)
183+
184+
@distributed_trace
185+
def list_routes(
170186
self,
171187
**kwargs # type: Any
172-
): # type: (...) -> Iterable[SipTrunkRoute]
173-
"""Retrieves an iterable of currently configured SIP routes.
188+
): # type: (...) -> AsyncItemPaged[SipTrunkRoute]
189+
"""Retrieves list of currently configured SIP routes.
174190
175191
:returns: Current SIP routes configuration.
176-
:rtype: Iterable[~azure.communication.siprouting.models.SipTrunkRoute]
192+
:rtype: AsyncItemPaged[~azure.communication.siprouting.models.SipTrunkRoute]
177193
:raises: ~azure.core.exceptions.HttpResponseError
178194
"""
179-
config = await self._rest_service.sip_routing.get(
180-
**kwargs
181-
)
182-
return [SipTrunkRoute(
183-
description=x.description,
184-
name=x.name,
185-
number_pattern=x.number_pattern,
186-
trunks=x.trunks
187-
) for x in config.routes]
195+
196+
async def extract_data(config):
197+
list_of_elem = [SipTrunkRoute(
198+
description=x.description,
199+
name=x.name,
200+
number_pattern=x.number_pattern,
201+
trunks=x.trunks) for x in config.routes]
202+
return None, AsyncList(list_of_elem)
203+
204+
# pylint: disable=unused-argument
205+
async def get_next(nextLink=None):
206+
return await self._rest_service.sip_routing.get(
207+
**kwargs
208+
)
209+
210+
return AsyncItemPaged(get_next, extract_data)
188211

189212
@distributed_trace_async
190213
async def set_trunks(

sdk/communication/azure-communication-phonenumbers/samples/siprouting/get_sip_routes_sample_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
async def get_sip_routes_sample():
2727
async with client:
28-
sip_routes = await client.list_routes()
28+
sip_routes = client.list_routes()
2929

30-
for route in sip_routes:
30+
async for route in sip_routes:
3131
print(route.name)
3232
print(route.description)
3333
print(route.number_pattern)

sdk/communication/azure-communication-phonenumbers/samples/siprouting/get_sip_trunks_sample_async.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525

2626
async def get_sip_trunks_sample():
2727
async with client:
28-
sip_trunks = await client.list_trunks()
28+
sip_trunks = client.list_trunks()
2929

30-
for trunk in sip_trunks:
31-
print(trunk.fqdn)
32-
print(trunk.sip_signaling_port)
30+
async for trunk in sip_trunks:
31+
print(trunk.fqdn)
32+
print(trunk.sip_signaling_port)
3333

3434
if __name__ == "__main__":
3535
asyncio.run(get_sip_trunks_sample())

sdk/communication/azure-communication-phonenumbers/test/test_sip_routing_client_e2e.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,45 +30,45 @@ def setup_method(self):
3030
@recorded_by_proxy
3131
def test_get_trunks(self, **kwargs):
3232
trunks = self._sip_routing_client.list_trunks()
33-
assert trunks is not None, "No trunks were returned."
34-
assert_trunks_are_equal(trunks,[self.first_trunk,self.second_trunk])
33+
trunks_list = self._get_as_list(trunks)
34+
assert_trunks_are_equal(trunks_list,[self.first_trunk,self.second_trunk])
3535

3636
@recorded_by_proxy
3737
def test_get_trunks_from_managed_identity(self, **kwargs):
3838
client = self._get_sip_client_managed_identity()
3939
trunks = client.list_trunks()
40-
assert trunks is not None, "No trunks were returned."
41-
assert_trunks_are_equal(trunks,[self.first_trunk,self.second_trunk])
40+
trunks_list = self._get_as_list(trunks)
41+
assert_trunks_are_equal(trunks_list,[self.first_trunk,self.second_trunk])
4242

4343
@recorded_by_proxy
4444
def test_get_routes(self, **kwargs):
4545
self._sip_routing_client.set_routes([self.first_route])
4646
routes = self._sip_routing_client.list_routes()
47-
assert routes is not None, "No routes were returned."
48-
assert_routes_are_equal(routes,[self.first_route])
47+
routes_list = self._get_as_list(routes)
48+
assert_routes_are_equal(routes_list,[self.first_route])
4949

5050
@recorded_by_proxy
5151
def test_get_routes_from_managed_identity(self, **kwargs):
5252
client = self._get_sip_client_managed_identity()
5353
client.set_routes([self.first_route])
5454
routes = client.list_routes()
55-
assert routes is not None, "No routes were returned."
56-
assert_routes_are_equal(routes,[self.first_route])
55+
routes_list = self._get_as_list(routes)
56+
assert_routes_are_equal(routes_list,[self.first_route])
5757

5858
@recorded_by_proxy
5959
def test_set_trunks(self, **kwargs):
6060
self._sip_routing_client.set_trunks([self.additional_trunk])
6161
result_trunks = self._sip_routing_client.list_trunks()
62-
assert result_trunks is not None, "No trunks were returned."
63-
assert_trunks_are_equal(result_trunks,[self.additional_trunk])
62+
trunks_list = self._get_as_list(result_trunks)
63+
assert_trunks_are_equal(trunks_list,[self.additional_trunk])
6464

6565
@recorded_by_proxy
6666
def test_set_trunks_from_managed_identity(self, **kwargs):
6767
client = self._get_sip_client_managed_identity()
6868
client.set_trunks([self.additional_trunk])
6969
result_trunks = client.list_trunks()
70-
assert result_trunks is not None, "No trunks were returned."
71-
assert_trunks_are_equal(result_trunks,[self.additional_trunk])
70+
trunks_list = self._get_as_list(result_trunks)
71+
assert_trunks_are_equal(trunks_list,[self.additional_trunk])
7272

7373
@recorded_by_proxy
7474
def test_set_trunks_empty_list(self, **kwargs):
@@ -86,8 +86,8 @@ def test_set_routes(self, **kwargs):
8686
self._sip_routing_client.set_routes([self.first_route])
8787
self._sip_routing_client.set_routes(new_routes)
8888
result_routes = self._sip_routing_client.list_routes()
89-
assert result_routes is not None, "No routes were returned."
90-
assert_routes_are_equal(result_routes,new_routes)
89+
routes_list = self._get_as_list(result_routes)
90+
assert_routes_are_equal(routes_list,new_routes)
9191

9292
@recorded_by_proxy
9393
def test_set_routes_from_managed_identity(self, **kwargs):
@@ -96,36 +96,40 @@ def test_set_routes_from_managed_identity(self, **kwargs):
9696
client.set_routes([self.first_route])
9797
client.set_routes(new_routes)
9898
result_routes = client.list_routes()
99-
assert result_routes is not None, "No routes were returned."
100-
assert_routes_are_equal(result_routes,new_routes)
99+
routes_list = self._get_as_list(result_routes)
100+
assert_routes_are_equal(routes_list,new_routes)
101101

102102
@recorded_by_proxy
103103
def test_delete_trunk(self, **kwargs):
104104
trunk_to_delete = self.second_trunk.fqdn
105105
self._sip_routing_client.delete_trunk(trunk_to_delete)
106106
new_trunks = self._sip_routing_client.list_trunks()
107-
assert_trunks_are_equal(new_trunks,[self.first_trunk])
107+
trunks_list = self._get_as_list(new_trunks)
108+
assert_trunks_are_equal(trunks_list,[self.first_trunk])
108109

109110
@recorded_by_proxy
110111
def test_delete_trunk_from_managed_identity(self, **kwargs):
111112
trunk_to_delete = self.second_trunk.fqdn
112113
client = self._get_sip_client_managed_identity()
113114
client.delete_trunk(trunk_to_delete)
114115
new_trunks = client.list_trunks()
115-
assert_trunks_are_equal(new_trunks,[self.first_trunk])
116+
trunks_list = self._get_as_list(new_trunks)
117+
assert_trunks_are_equal(trunks_list,[self.first_trunk])
116118

117119
@recorded_by_proxy
118120
def test_add_trunk(self, **kwargs):
119121
self._sip_routing_client.set_trunk(self.additional_trunk)
120122
new_trunks = self._sip_routing_client.list_trunks()
121-
assert_trunks_are_equal(new_trunks,[self.first_trunk,self.second_trunk,self.additional_trunk])
123+
trunks_list = self._get_as_list(new_trunks)
124+
assert_trunks_are_equal(trunks_list,[self.first_trunk,self.second_trunk,self.additional_trunk])
122125

123126
@recorded_by_proxy
124127
def test_add_trunk_from_managed_identity(self, **kwargs):
125128
client = self._get_sip_client_managed_identity()
126129
client.set_trunk(self.additional_trunk)
127130
new_trunks = client.list_trunks()
128-
assert_trunks_are_equal(new_trunks,[self.first_trunk,self.second_trunk,self.additional_trunk])
131+
trunks_list = self._get_as_list(new_trunks)
132+
assert_trunks_are_equal(trunks_list,[self.first_trunk,self.second_trunk,self.additional_trunk])
129133

130134
@recorded_by_proxy
131135
def test_get_trunk(self, **kwargs):
@@ -150,17 +154,26 @@ def test_set_trunk(self, **kwargs):
150154
modified_trunk = SipTrunk(fqdn=self.second_trunk.fqdn,sip_signaling_port=7777)
151155
self._sip_routing_client.set_trunk(modified_trunk)
152156
new_trunks = self._sip_routing_client.list_trunks()
153-
assert_trunks_are_equal(new_trunks,[self.first_trunk,modified_trunk])
157+
trunks_list = self._get_as_list(new_trunks)
158+
assert_trunks_are_equal(trunks_list,[self.first_trunk,modified_trunk])
154159

155160
@recorded_by_proxy
156161
def test_set_trunk_from_managed_identity(self, **kwargs):
157162
modified_trunk = SipTrunk(fqdn=self.second_trunk.fqdn,sip_signaling_port=7777)
158163
client = self._get_sip_client_managed_identity()
159164
client.set_trunk(modified_trunk)
160165
new_trunks = client.list_trunks()
161-
assert_trunks_are_equal(new_trunks,[self.first_trunk,modified_trunk])
166+
trunks_list = self._get_as_list(new_trunks)
167+
assert_trunks_are_equal(trunks_list,[self.first_trunk,modified_trunk])
162168

163169
def _get_sip_client_managed_identity(self):
164170
endpoint, *_ = parse_connection_str(self.connection_str)
165171
credential = create_token_credential()
166172
return SipRoutingClient(endpoint, credential)
173+
174+
def _get_as_list(self,iter):
175+
assert iter is not None, "No iterable was returned."
176+
items = []
177+
for item in iter:
178+
items.append(item)
179+
return items

0 commit comments

Comments
 (0)