Skip to content

Commit 791acc1

Browse files
authored
have LRO support rest requests and responses (Azure#20483)
* have LRO support rest requests and responses * pass operation config to send_request * fix polling location fail tests
1 parent b0c7721 commit 791acc1

File tree

8 files changed

+412
-3
lines changed

8 files changed

+412
-3
lines changed

sdk/core/azure-core/azure/core/polling/async_base_polling.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,21 @@ async def request_status(self, status_link): # pylint:disable=invalid-overridde
116116
"""
117117
if self._path_format_arguments:
118118
status_link = self._client.format_url(status_link, **self._path_format_arguments)
119-
request = self._client.get(status_link)
120119
# Re-inject 'x-ms-client-request-id' while polling
121120
if "request_id" not in self._operation_config:
122121
self._operation_config["request_id"] = self._get_request_id()
122+
if hasattr(self._initial_response.http_response, "content"):
123+
# if I am a azure.core.rest.HttpResponse
124+
# want to keep making azure.core.rest calls
125+
from azure.core.rest import HttpRequest as RestHttpRequest
126+
request = RestHttpRequest("GET", status_link)
127+
return await self._client.send_request(
128+
request, _return_pipeline_response=True, **self._operation_config
129+
)
130+
# if I am a azure.core.pipeline.transport.HttpResponse
131+
request = self._client.get(status_link)
132+
133+
# can't use send_request in this case, because send_request is still provisional
123134
return await self._client._pipeline.run( # pylint: disable=protected-access
124135
request, stream=False, **self._operation_config
125136
)

sdk/core/azure-core/azure/core/polling/base_polling.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def _is_empty(response):
121121
122122
:rtype: bool
123123
"""
124-
return not bool(response.body())
124+
content = response.content if hasattr(response, "content") else response.body() # type: ignore
125+
return not bool(content)
125126

126127

127128
class LongRunningOperation(ABC):
@@ -574,10 +575,21 @@ def request_status(self, status_link):
574575
"""
575576
if self._path_format_arguments:
576577
status_link = self._client.format_url(status_link, **self._path_format_arguments)
577-
request = self._client.get(status_link)
578578
# Re-inject 'x-ms-client-request-id' while polling
579579
if "request_id" not in self._operation_config:
580580
self._operation_config["request_id"] = self._get_request_id()
581+
if hasattr(self._initial_response.http_response, "content"):
582+
# if I am a azure.core.rest.HttpResponse
583+
# want to keep making azure.core.rest calls
584+
from azure.core.rest import HttpRequest as RestHttpRequest
585+
request = RestHttpRequest("GET", status_link)
586+
return self._client.send_request(
587+
request, _return_pipeline_response=True, **self._operation_config
588+
)
589+
# if I am a azure.core.pipeline.transport.HttpResponse
590+
request = self._client.get(status_link)
591+
592+
# can't use send_request in this case, because send_request is still provisional
581593
return self._client._pipeline.run( # pylint: disable=protected-access
582594
request, stream=False, **self._operation_config
583595
)
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#--------------------------------------------------------------------------
2+
#
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
#
5+
# The MIT License (MIT)
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the ""Software""), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
#
25+
#--------------------------------------------------------------------------
26+
import pytest
27+
from azure.core.exceptions import ServiceRequestError
28+
from azure.core.rest import HttpRequest
29+
from azure.core.polling import AsyncLROPoller
30+
from azure.core.polling.async_base_polling import AsyncLROBasePolling
31+
32+
@pytest.fixture
33+
def deserialization_callback():
34+
def _callback(response):
35+
return response.http_response.json()
36+
return _callback
37+
38+
@pytest.fixture
39+
def lro_poller(client, deserialization_callback):
40+
async def _callback(request, **kwargs):
41+
initial_response = await client.send_request(
42+
request=request,
43+
_return_pipeline_response=True
44+
)
45+
return AsyncLROPoller(
46+
client._client,
47+
initial_response,
48+
deserialization_callback,
49+
AsyncLROBasePolling(0, **kwargs)
50+
)
51+
return _callback
52+
53+
@pytest.mark.asyncio
54+
async def test_post_with_location_and_operation_location_headers(lro_poller):
55+
poller = await lro_poller(HttpRequest("POST", "/polling/post/location-and-operation-location"))
56+
result = await poller.result()
57+
assert result == {'location_result': True}
58+
59+
@pytest.mark.asyncio
60+
async def test_post_with_location_and_operation_location_headers_no_body(lro_poller):
61+
poller = await lro_poller(HttpRequest("POST", "/polling/post/location-and-operation-location-no-body"))
62+
result = await poller.result()
63+
assert result is None
64+
65+
@pytest.mark.asyncio
66+
async def test_post_resource_location(lro_poller):
67+
poller = await lro_poller(HttpRequest("POST", "/polling/post/resource-location"))
68+
result = await poller.result()
69+
assert result == {'location_result': True}
70+
71+
@pytest.mark.asyncio
72+
async def test_put_no_polling(lro_poller):
73+
result = await (await lro_poller(HttpRequest("PUT", "/polling/no-polling"))).result()
74+
assert result['properties']['provisioningState'] == 'Succeeded'
75+
76+
@pytest.mark.asyncio
77+
async def test_put_location(lro_poller):
78+
result = await (await lro_poller(HttpRequest("PUT", "/polling/location"))).result()
79+
assert result['location_result']
80+
81+
@pytest.mark.asyncio
82+
async def test_put_initial_response_body_invalid(lro_poller):
83+
# initial body is invalid
84+
result = await (await lro_poller(HttpRequest("PUT", "/polling/initial-body-invalid"))).result()
85+
assert result['location_result']
86+
87+
@pytest.mark.asyncio
88+
async def test_put_operation_location_polling_fail(lro_poller):
89+
with pytest.raises(ServiceRequestError):
90+
await (await lro_poller(HttpRequest("PUT", "/polling/bad-operation-location"), retry_total=0)).result()
91+
92+
@pytest.mark.asyncio
93+
async def test_put_location_polling_fail(lro_poller):
94+
with pytest.raises(ServiceRequestError):
95+
await (await lro_poller(HttpRequest("PUT", "/polling/bad-location"), retry_total=0)).result()
96+
97+
@pytest.mark.asyncio
98+
async def test_patch_location(lro_poller):
99+
result = await (await lro_poller(HttpRequest("PATCH", "/polling/location"))).result()
100+
assert result['location_result']
101+
102+
@pytest.mark.asyncio
103+
async def test_patch_operation_location_polling_fail(lro_poller):
104+
with pytest.raises(ServiceRequestError):
105+
await (await lro_poller(HttpRequest("PUT", "/polling/bad-operation-location"), retry_total=0)).result()
106+
107+
@pytest.mark.asyncio
108+
async def test_patch_location_polling_fail(lro_poller):
109+
with pytest.raises(ServiceRequestError):
110+
await (await lro_poller(HttpRequest("PUT", "/polling/bad-location"), retry_total=0)).result()
111+
112+
@pytest.mark.asyncio
113+
async def test_delete_operation_location(lro_poller):
114+
result = await (await lro_poller(HttpRequest("DELETE", "/polling/operation-location"))).result()
115+
assert result['status'] == 'Succeeded'
116+
117+
@pytest.mark.asyncio
118+
async def test_request_id(lro_poller):
119+
result = await (await lro_poller(HttpRequest("POST", "/polling/request-id"), request_id="123456789")).result()
120+
assert result['status'] == "Succeeded"
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#--------------------------------------------------------------------------
2+
#
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
#
5+
# The MIT License (MIT)
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the ""Software""), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
#
25+
#--------------------------------------------------------------------------
26+
import pytest
27+
from azure.core.exceptions import ServiceRequestError
28+
from azure.core.rest import HttpRequest
29+
from azure.core.polling import LROPoller
30+
from azure.core.polling.base_polling import LROBasePolling
31+
32+
@pytest.fixture
33+
def deserialization_callback():
34+
def _callback(response):
35+
return response.http_response.json()
36+
return _callback
37+
38+
@pytest.fixture
39+
def lro_poller(client, deserialization_callback):
40+
def _callback(request, **kwargs):
41+
initial_response = client.send_request(
42+
request=request,
43+
_return_pipeline_response=True
44+
)
45+
return LROPoller(
46+
client._client,
47+
initial_response,
48+
deserialization_callback,
49+
LROBasePolling(0, **kwargs),
50+
)
51+
return _callback
52+
53+
def test_post_with_location_and_operation_location_headers(lro_poller):
54+
poller = lro_poller(HttpRequest("POST", "/polling/post/location-and-operation-location"))
55+
result = poller.result()
56+
assert result == {'location_result': True}
57+
58+
def test_post_with_location_and_operation_location_headers_no_body(lro_poller):
59+
poller = lro_poller(HttpRequest("POST", "/polling/post/location-and-operation-location-no-body"))
60+
result = poller.result()
61+
assert result is None
62+
63+
def test_post_resource_location(lro_poller):
64+
poller = lro_poller(HttpRequest("POST", "/polling/post/resource-location"))
65+
result = poller.result()
66+
assert result == {'location_result': True}
67+
68+
def test_put_no_polling(lro_poller):
69+
result = lro_poller(HttpRequest("PUT", "/polling/no-polling")).result()
70+
assert result['properties']['provisioningState'] == 'Succeeded'
71+
72+
def test_put_location(lro_poller):
73+
result = lro_poller(HttpRequest("PUT", "/polling/location")).result()
74+
assert result['location_result']
75+
76+
def test_put_initial_response_body_invalid(lro_poller):
77+
# initial body is invalid
78+
result = lro_poller(HttpRequest("PUT", "/polling/initial-body-invalid")).result()
79+
assert result['location_result']
80+
81+
def test_put_operation_location_polling_fail(lro_poller):
82+
with pytest.raises(ServiceRequestError):
83+
lro_poller(HttpRequest("PUT", "/polling/bad-operation-location"), retry_total=0).result()
84+
85+
def test_put_location_polling_fail(lro_poller):
86+
with pytest.raises(ServiceRequestError):
87+
lro_poller(HttpRequest("PUT", "/polling/bad-location"), retry_total=0).result()
88+
89+
def test_patch_location(lro_poller):
90+
result = lro_poller(HttpRequest("PATCH", "/polling/location")).result()
91+
assert result['location_result']
92+
93+
def test_patch_operation_location_polling_fail(lro_poller):
94+
with pytest.raises(ServiceRequestError):
95+
lro_poller(HttpRequest("PUT", "/polling/bad-operation-location"), retry_total=0).result()
96+
97+
def test_patch_location_polling_fail(lro_poller):
98+
with pytest.raises(ServiceRequestError):
99+
lro_poller(HttpRequest("PUT", "/polling/bad-location"), retry_total=0).result()
100+
101+
def test_delete_operation_location(lro_poller):
102+
result = lro_poller(HttpRequest("DELETE", "/polling/operation-location")).result()
103+
assert result['status'] == 'Succeeded'
104+
105+
def test_request_id(lro_poller):
106+
result = lro_poller(HttpRequest("POST", "/polling/request-id"), request_id="123456789").result()

sdk/core/azure-core/tests/testserver_tests/coretestserver/coretestserver/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
basic_api,
1111
encoding_api,
1212
errors_api,
13+
polling_api,
1314
streams_api,
1415
urlencoded_api,
1516
multipart_api,
@@ -21,6 +22,7 @@
2122
app.register_blueprint(basic_api, url_prefix="/basic")
2223
app.register_blueprint(encoding_api, url_prefix="/encoding")
2324
app.register_blueprint(errors_api, url_prefix="/errors")
25+
app.register_blueprint(polling_api, url_prefix="/polling")
2426
app.register_blueprint(streams_api, url_prefix="/streams")
2527
app.register_blueprint(urlencoded_api, url_prefix="/urlencoded")
2628
app.register_blueprint(multipart_api, url_prefix="/multipart")

sdk/core/azure-core/tests/testserver_tests/coretestserver/coretestserver/test_routes/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .encoding import encoding_api
1010
from .errors import errors_api
1111
from .multipart import multipart_api
12+
from .polling import polling_api
1213
from .streams import streams_api
1314
from .urlencoded import urlencoded_api
1415
from .xml_route import xml_api
@@ -19,6 +20,7 @@
1920
"encoding_api",
2021
"errors_api",
2122
"multipart_api",
23+
"polling_api",
2224
"streams_api",
2325
"urlencoded_api",
2426
"xml_api",

sdk/core/azure-core/tests/testserver_tests/coretestserver/coretestserver/test_routes/helpers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ def get_dict(*keys, **extras):
158158

159159
return out_d
160160

161+
def get_base_url(request):
162+
return "http://" + request.host
163+
161164
__all__ = ["assert_with_message",
162165
"get_dict",
163166
"jsonify"]

0 commit comments

Comments
 (0)