Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[storage.blob] Remove requests as dependency for storage.blob #25017

Merged
merged 21 commits into from
Jul 29, 2022
1 change: 1 addition & 0 deletions sdk/storage/azure-storage-blob/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
### Features Added

### Bugs Fixed
- Removed forced `requests` import for sync calls. (25017)
- Adjusted type hints for `upload_blob` and `StorageStreamDownloader.readall`.

## 12.13.0 (2022-07-07)
Original file line number Diff line number Diff line change
@@ -11,8 +11,7 @@
from io import BytesIO
from typing import Generic, Iterator, TypeVar

import requests
from azure.core.exceptions import HttpResponseError, ServiceResponseError
from azure.core.exceptions import DecodeError, HttpResponseError, ServiceResponseError, IncompleteReadError
from azure.core.tracing.common import with_current_context

from ._shared.request_handlers import validate_and_format_range_headers
@@ -213,7 +212,7 @@ def _download_chunk(self, chunk_start, chunk_end):
try:
chunk_data = process_content(response, offset[0], offset[1], self.encryption_options)
retry_active = False
except (requests.exceptions.ChunkedEncodingError, requests.exceptions.ConnectionError) as error:
jalauzon-msft marked this conversation as resolved.
Show resolved Hide resolved
except (IncompleteReadError, HttpResponseError, DecodeError) as error:
retry_total -= 1
if retry_total <= 0:
raise ServiceResponseError(error, error=error)
@@ -474,7 +473,7 @@ def _initial_request(self):
self._encryption_options
)
retry_active = False
except (requests.exceptions.ChunkedEncodingError, requests.exceptions.ConnectionError) as error:
except (IncompleteReadError, HttpResponseError, DecodeError) as error:
retry_total -= 1
if retry_total <= 0:
raise ServiceResponseError(error, error=error)
35 changes: 30 additions & 5 deletions sdk/storage/azure-storage-blob/tests/test_retry.py
Original file line number Diff line number Diff line change
@@ -3,14 +3,15 @@
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import unittest
from unittest import mock
import pytest

from azure.core.exceptions import (
HttpResponseError,
ResourceExistsError,
AzureError,
ClientAuthenticationError
ClientAuthenticationError,
ServiceResponseError
)
from azure.core.pipeline.transport import(
RequestsTransport
@@ -24,6 +25,9 @@
LinearRetry,
ExponentialRetry,
)
from requests import Response
from requests.exceptions import ContentDecodingError, ChunkedEncodingError
from azure.core.exceptions import DecodeError

from settings.testcase import BlobPreparer
from devtools_testutils.storage import StorageTestCase
@@ -434,6 +438,27 @@ def test_invalid_account_key(self, storage_account_name, storage_account_key):
# No retry should be performed since the signing error is fatal
self.assertEqual(retry_counter.count, 0)


# ------------------------------------------------------------------------------

@pytest.mark.live_test_only
jalauzon-msft marked this conversation as resolved.
Show resolved Hide resolved
@BlobPreparer()
def test_streaming_retry(self, storage_account_name, storage_account_key):
"""Test that retry mechanisms are working when streaming data."""
container_name = self.get_resource_name('utcontainer')
service = self._create_storage_service(
BlobServiceClient, storage_account_name, storage_account_key)
container = service.get_container_client(container_name)
container.create_container()
assert container.exists()
blob_name = "myblob"
container.upload_blob(blob_name, b"abcde")

for error in (ContentDecodingError(), ChunkedEncodingError(), ChunkedEncodingError("IncompleteRead")):
iterator_mock = mock.MagicMock()
iterator_mock.__next__.side_effect = error
iter_content_mock = mock.Mock()
iter_content_mock.return_value = iterator_mock
with mock.patch.object(Response, "iter_content", iter_content_mock), pytest.raises(ServiceResponseError):
blob = container.get_blob_client(blob=blob_name)
blob.download_blob()
assert iterator_mock.__next__.call_count == 3

# ------------------------------------------------------------------------------