Skip to content

Commit

Permalink
have LRO support rest requests and responses (Azure#20483)
Browse files Browse the repository at this point in the history
* have LRO support rest requests and responses

* pass operation config to send_request

* fix polling location fail tests
  • Loading branch information
iscai-msft authored Sep 1, 2021
1 parent b0c7721 commit 791acc1
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 3 deletions.
13 changes: 12 additions & 1 deletion sdk/core/azure-core/azure/core/polling/async_base_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,21 @@ async def request_status(self, status_link): # pylint:disable=invalid-overridde
"""
if self._path_format_arguments:
status_link = self._client.format_url(status_link, **self._path_format_arguments)
request = self._client.get(status_link)
# Re-inject 'x-ms-client-request-id' while polling
if "request_id" not in self._operation_config:
self._operation_config["request_id"] = self._get_request_id()
if hasattr(self._initial_response.http_response, "content"):
# if I am a azure.core.rest.HttpResponse
# want to keep making azure.core.rest calls
from azure.core.rest import HttpRequest as RestHttpRequest
request = RestHttpRequest("GET", status_link)
return await self._client.send_request(
request, _return_pipeline_response=True, **self._operation_config
)
# if I am a azure.core.pipeline.transport.HttpResponse
request = self._client.get(status_link)

# can't use send_request in this case, because send_request is still provisional
return await self._client._pipeline.run( # pylint: disable=protected-access
request, stream=False, **self._operation_config
)
Expand Down
16 changes: 14 additions & 2 deletions sdk/core/azure-core/azure/core/polling/base_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def _is_empty(response):
:rtype: bool
"""
return not bool(response.body())
content = response.content if hasattr(response, "content") else response.body() # type: ignore
return not bool(content)


class LongRunningOperation(ABC):
Expand Down Expand Up @@ -574,10 +575,21 @@ def request_status(self, status_link):
"""
if self._path_format_arguments:
status_link = self._client.format_url(status_link, **self._path_format_arguments)
request = self._client.get(status_link)
# Re-inject 'x-ms-client-request-id' while polling
if "request_id" not in self._operation_config:
self._operation_config["request_id"] = self._get_request_id()
if hasattr(self._initial_response.http_response, "content"):
# if I am a azure.core.rest.HttpResponse
# want to keep making azure.core.rest calls
from azure.core.rest import HttpRequest as RestHttpRequest
request = RestHttpRequest("GET", status_link)
return self._client.send_request(
request, _return_pipeline_response=True, **self._operation_config
)
# if I am a azure.core.pipeline.transport.HttpResponse
request = self._client.get(status_link)

# can't use send_request in this case, because send_request is still provisional
return self._client._pipeline.run( # pylint: disable=protected-access
request, stream=False, **self._operation_config
)
Expand Down
120 changes: 120 additions & 0 deletions sdk/core/azure-core/tests/async_tests/test_rest_polling_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#--------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#--------------------------------------------------------------------------
import pytest
from azure.core.exceptions import ServiceRequestError
from azure.core.rest import HttpRequest
from azure.core.polling import AsyncLROPoller
from azure.core.polling.async_base_polling import AsyncLROBasePolling

@pytest.fixture
def deserialization_callback():
def _callback(response):
return response.http_response.json()
return _callback

@pytest.fixture
def lro_poller(client, deserialization_callback):
async def _callback(request, **kwargs):
initial_response = await client.send_request(
request=request,
_return_pipeline_response=True
)
return AsyncLROPoller(
client._client,
initial_response,
deserialization_callback,
AsyncLROBasePolling(0, **kwargs)
)
return _callback

@pytest.mark.asyncio
async def test_post_with_location_and_operation_location_headers(lro_poller):
poller = await lro_poller(HttpRequest("POST", "/polling/post/location-and-operation-location"))
result = await poller.result()
assert result == {'location_result': True}

@pytest.mark.asyncio
async def test_post_with_location_and_operation_location_headers_no_body(lro_poller):
poller = await lro_poller(HttpRequest("POST", "/polling/post/location-and-operation-location-no-body"))
result = await poller.result()
assert result is None

@pytest.mark.asyncio
async def test_post_resource_location(lro_poller):
poller = await lro_poller(HttpRequest("POST", "/polling/post/resource-location"))
result = await poller.result()
assert result == {'location_result': True}

@pytest.mark.asyncio
async def test_put_no_polling(lro_poller):
result = await (await lro_poller(HttpRequest("PUT", "/polling/no-polling"))).result()
assert result['properties']['provisioningState'] == 'Succeeded'

@pytest.mark.asyncio
async def test_put_location(lro_poller):
result = await (await lro_poller(HttpRequest("PUT", "/polling/location"))).result()
assert result['location_result']

@pytest.mark.asyncio
async def test_put_initial_response_body_invalid(lro_poller):
# initial body is invalid
result = await (await lro_poller(HttpRequest("PUT", "/polling/initial-body-invalid"))).result()
assert result['location_result']

@pytest.mark.asyncio
async def test_put_operation_location_polling_fail(lro_poller):
with pytest.raises(ServiceRequestError):
await (await lro_poller(HttpRequest("PUT", "/polling/bad-operation-location"), retry_total=0)).result()

@pytest.mark.asyncio
async def test_put_location_polling_fail(lro_poller):
with pytest.raises(ServiceRequestError):
await (await lro_poller(HttpRequest("PUT", "/polling/bad-location"), retry_total=0)).result()

@pytest.mark.asyncio
async def test_patch_location(lro_poller):
result = await (await lro_poller(HttpRequest("PATCH", "/polling/location"))).result()
assert result['location_result']

@pytest.mark.asyncio
async def test_patch_operation_location_polling_fail(lro_poller):
with pytest.raises(ServiceRequestError):
await (await lro_poller(HttpRequest("PUT", "/polling/bad-operation-location"), retry_total=0)).result()

@pytest.mark.asyncio
async def test_patch_location_polling_fail(lro_poller):
with pytest.raises(ServiceRequestError):
await (await lro_poller(HttpRequest("PUT", "/polling/bad-location"), retry_total=0)).result()

@pytest.mark.asyncio
async def test_delete_operation_location(lro_poller):
result = await (await lro_poller(HttpRequest("DELETE", "/polling/operation-location"))).result()
assert result['status'] == 'Succeeded'

@pytest.mark.asyncio
async def test_request_id(lro_poller):
result = await (await lro_poller(HttpRequest("POST", "/polling/request-id"), request_id="123456789")).result()
assert result['status'] == "Succeeded"
106 changes: 106 additions & 0 deletions sdk/core/azure-core/tests/test_rest_polling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#--------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#--------------------------------------------------------------------------
import pytest
from azure.core.exceptions import ServiceRequestError
from azure.core.rest import HttpRequest
from azure.core.polling import LROPoller
from azure.core.polling.base_polling import LROBasePolling

@pytest.fixture
def deserialization_callback():
def _callback(response):
return response.http_response.json()
return _callback

@pytest.fixture
def lro_poller(client, deserialization_callback):
def _callback(request, **kwargs):
initial_response = client.send_request(
request=request,
_return_pipeline_response=True
)
return LROPoller(
client._client,
initial_response,
deserialization_callback,
LROBasePolling(0, **kwargs),
)
return _callback

def test_post_with_location_and_operation_location_headers(lro_poller):
poller = lro_poller(HttpRequest("POST", "/polling/post/location-and-operation-location"))
result = poller.result()
assert result == {'location_result': True}

def test_post_with_location_and_operation_location_headers_no_body(lro_poller):
poller = lro_poller(HttpRequest("POST", "/polling/post/location-and-operation-location-no-body"))
result = poller.result()
assert result is None

def test_post_resource_location(lro_poller):
poller = lro_poller(HttpRequest("POST", "/polling/post/resource-location"))
result = poller.result()
assert result == {'location_result': True}

def test_put_no_polling(lro_poller):
result = lro_poller(HttpRequest("PUT", "/polling/no-polling")).result()
assert result['properties']['provisioningState'] == 'Succeeded'

def test_put_location(lro_poller):
result = lro_poller(HttpRequest("PUT", "/polling/location")).result()
assert result['location_result']

def test_put_initial_response_body_invalid(lro_poller):
# initial body is invalid
result = lro_poller(HttpRequest("PUT", "/polling/initial-body-invalid")).result()
assert result['location_result']

def test_put_operation_location_polling_fail(lro_poller):
with pytest.raises(ServiceRequestError):
lro_poller(HttpRequest("PUT", "/polling/bad-operation-location"), retry_total=0).result()

def test_put_location_polling_fail(lro_poller):
with pytest.raises(ServiceRequestError):
lro_poller(HttpRequest("PUT", "/polling/bad-location"), retry_total=0).result()

def test_patch_location(lro_poller):
result = lro_poller(HttpRequest("PATCH", "/polling/location")).result()
assert result['location_result']

def test_patch_operation_location_polling_fail(lro_poller):
with pytest.raises(ServiceRequestError):
lro_poller(HttpRequest("PUT", "/polling/bad-operation-location"), retry_total=0).result()

def test_patch_location_polling_fail(lro_poller):
with pytest.raises(ServiceRequestError):
lro_poller(HttpRequest("PUT", "/polling/bad-location"), retry_total=0).result()

def test_delete_operation_location(lro_poller):
result = lro_poller(HttpRequest("DELETE", "/polling/operation-location")).result()
assert result['status'] == 'Succeeded'

def test_request_id(lro_poller):
result = lro_poller(HttpRequest("POST", "/polling/request-id"), request_id="123456789").result()
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
basic_api,
encoding_api,
errors_api,
polling_api,
streams_api,
urlencoded_api,
multipart_api,
Expand All @@ -21,6 +22,7 @@
app.register_blueprint(basic_api, url_prefix="/basic")
app.register_blueprint(encoding_api, url_prefix="/encoding")
app.register_blueprint(errors_api, url_prefix="/errors")
app.register_blueprint(polling_api, url_prefix="/polling")
app.register_blueprint(streams_api, url_prefix="/streams")
app.register_blueprint(urlencoded_api, url_prefix="/urlencoded")
app.register_blueprint(multipart_api, url_prefix="/multipart")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .encoding import encoding_api
from .errors import errors_api
from .multipart import multipart_api
from .polling import polling_api
from .streams import streams_api
from .urlencoded import urlencoded_api
from .xml_route import xml_api
Expand All @@ -19,6 +20,7 @@
"encoding_api",
"errors_api",
"multipart_api",
"polling_api",
"streams_api",
"urlencoded_api",
"xml_api",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ def get_dict(*keys, **extras):

return out_d

def get_base_url(request):
return "http://" + request.host

__all__ = ["assert_with_message",
"get_dict",
"jsonify"]
Loading

0 comments on commit 791acc1

Please sign in to comment.