Skip to content

Commit 599c15a

Browse files
committed
allow LRO pipelines to handle rest responses
1 parent 0304a0c commit 599c15a

File tree

9 files changed

+383
-5
lines changed

9 files changed

+383
-5
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,19 @@ 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(request, _return_pipeline_response=True)
128+
# if I am a azure.core.pipeline.transport.HttpResponse
129+
request = self._client.get(status_link)
130+
131+
# can't use send_request in this case, because send_request is still provisional
123132
return await self._client._pipeline.run( # pylint: disable=protected-access
124133
request, stream=False, **self._operation_config
125134
)

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

Lines changed: 12 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,19 @@ 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(request, _return_pipeline_response=True)
587+
# if I am a azure.core.pipeline.transport.HttpResponse
588+
request = self._client.get(status_link)
589+
590+
# can't use send_request in this case, because send_request is still provisional
581591
return self._client._pipeline.run( # pylint: disable=protected-access
582592
request, stream=False, **self._operation_config
583593
)

sdk/core/azure-core/azure/core/rest/_aiohttp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(
5252
def __getstate__(self):
5353
state = self.__dict__.copy()
5454
# Remove the unpicklable entries.
55-
state['internal_response'] = None # aiohttp response are not pickable (see headers comments)
55+
state['_internal_response'] = None # aiohttp response are not pickable (see headers comments)
5656
state['headers'] = CIMultiDict(self.headers) # MultiDictProxy is not pickable
5757
return state
5858

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 HttpResponseError
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):
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)
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(HttpResponseError):
90+
await (await lro_poller(HttpRequest("PUT", "/polling/put/bad-operation-location"))).result()
91+
92+
@pytest.mark.asyncio
93+
async def test_put_location_polling_fail(lro_poller):
94+
with pytest.raises(HttpResponseError):
95+
await (await lro_poller(HttpRequest("PUT", "/polling/bad-location"))).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(HttpResponseError):
105+
await (await lro_poller(HttpRequest("PUT", "/polling/bad-operation-location"))).result()
106+
107+
@pytest.mark.asyncio
108+
async def test_patch_location_polling_fail(lro_poller):
109+
with pytest.raises(HttpResponseError):
110+
await (await lro_poller(HttpRequest("PUT", "/polling/bad-location"))).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'
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 HttpResponseError
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):
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)
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(HttpResponseError):
83+
lro_poller(HttpRequest("PUT", "/polling/put/bad-operation-location")).result()
84+
85+
def test_put_location_polling_fail(lro_poller):
86+
with pytest.raises(HttpResponseError):
87+
lro_poller(HttpRequest("PUT", "/polling/bad-location")).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(HttpResponseError):
95+
lro_poller(HttpRequest("PUT", "/polling/bad-operation-location")).result()
96+
97+
def test_patch_location_polling_fail(lro_poller):
98+
with pytest.raises(HttpResponseError):
99+
lro_poller(HttpRequest("PUT", "/polling/bad-location")).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'

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
basic_api,
1111
encoding_api,
1212
errors_api,
13+
polling_api,
1314
streams_api,
1415
urlencoded_api,
1516
multipart_api,
16-
xml_api
17+
xml_api,
1718
)
1819

1920
app = Flask(__name__)
2021
app.register_blueprint(basic_api, url_prefix="/basic")
2122
app.register_blueprint(encoding_api, url_prefix="/encoding")
2223
app.register_blueprint(errors_api, url_prefix="/errors")
24+
app.register_blueprint(polling_api, url_prefix="/polling")
2325
app.register_blueprint(streams_api, url_prefix="/streams")
2426
app.register_blueprint(urlencoded_api, url_prefix="/urlencoded")
2527
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
@@ -12,12 +12,14 @@
1212
from .streams import streams_api
1313
from .urlencoded import urlencoded_api
1414
from .xml_route import xml_api
15+
from .polling import polling_api
1516

1617
__all__ = [
1718
"basic_api",
1819
"encoding_api",
1920
"errors_api",
2021
"multipart_api",
22+
"polling_api",
2123
"streams_api",
2224
"urlencoded_api",
2325
"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)