Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Release History

## 1.10.1 (Unreleased)
## 1.11.0 (Unreleased)

### Features

- Add `raise_for_status` method onto `HttpResponse`. Calling `response.raise_for_status()` on a response with an error code
will raise an `HttpResponseError`. Calling it on a good response will do nothing #16399


## 1.10.0 (2021-01-11)
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core/azure/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
# regenerated.
# --------------------------------------------------------------------------

VERSION = "1.10.1"
VERSION = "1.11.0"
9 changes: 9 additions & 0 deletions sdk/core/azure-core/azure/core/pipeline/transport/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

from six.moves.http_client import HTTPConnection, HTTPResponse as _HTTPResponse

from azure.core.exceptions import HttpResponseError
from azure.core.pipeline import (
ABC,
AbstractContextManager,
Expand Down Expand Up @@ -583,6 +584,14 @@ def _get_raw_parts(self, http_response_type=None):
requests = self.request.multipart_mixed_info[0] # type: List[HttpRequest]
return self._decode_parts(message, http_response_type, requests)

def raise_for_status(self):
# type () -> None
"""Raises an HttpResponseError if the response has an error status code.
If response is good, does nothing.
"""
if self.status_code >= 400:
raise HttpResponseError(response=self)


class HttpResponse(_HttpResponseBase): # pylint: disable=abstract-method
def stream_download(self, pipeline):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from azure.core.pipeline.transport import HttpRequest, AsyncHttpResponse, AsyncHttpTransport, AioHttpTransport
from azure.core.pipeline.policies import HeadersPolicy
from azure.core.pipeline import AsyncPipeline
from azure.core.exceptions import HttpResponseError

import pytest

Expand Down Expand Up @@ -706,6 +707,17 @@ async def test_multipart_receive_with_combination_changeset_first():
assert parts[1].status_code == 202
assert parts[2].status_code == 404

def test_raise_for_status_bad_response():
response = MockResponse(request=None, body=None, content_type=None)
response.status_code = 400
with pytest.raises(HttpResponseError):
response.raise_for_status()

def test_raise_for_status_good_response():
response = MockResponse(request=None, body=None, content_type=None)
response.status_code = 200
response.raise_for_status()


@pytest.mark.asyncio
async def test_multipart_receive_with_combination_changeset_middle():
Expand Down
14 changes: 13 additions & 1 deletion sdk/core/azure-core/tests/test_basic_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from collections import OrderedDict
import time
import sys
import json

try:
from unittest import mock
Expand All @@ -17,6 +18,7 @@
from azure.core.pipeline.transport._base import HttpClientTransportResponse, HttpTransport, _deserialize_response, _urljoin
from azure.core.pipeline.policies import HeadersPolicy
from azure.core.pipeline import Pipeline
from azure.core.exceptions import HttpResponseError
import logging
import pytest

Expand All @@ -30,7 +32,6 @@ def __init__(self, request, body, content_type):
def body(self):
return self._body


@pytest.mark.skipif(sys.version_info < (3, 6), reason="Multipart serialization not supported on 2.7 + dict order not deterministic on 3.5")
def test_http_request_serialization():
# Method + Url
Expand Down Expand Up @@ -671,6 +672,17 @@ def on_response(self, request, response):
assert res1.status_code == 404
assert res1.headers['x-ms-fun'] == 'true'

def test_raise_for_status_bad_response():
response = MockResponse(request=None, body=None, content_type=None)
response.status_code = 400
with pytest.raises(HttpResponseError):
response.raise_for_status()

def test_raise_for_status_good_response():
response = MockResponse(request=None, body=None, content_type=None)
response.status_code = 200
response.raise_for_status()


def test_multipart_receive_with_one_changeset():

Expand Down