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
17 changes: 15 additions & 2 deletions src/flexmeasures_client/response_handling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import logging
from typing import TYPE_CHECKING

from aiohttp import ContentTypeError
Expand All @@ -11,6 +12,8 @@
if TYPE_CHECKING: # Only imports the below statements during type checking
from flexmeasures_client.client import FlexMeasuresClient

logger = logging.getLogger(__name__)


async def check_response(
self: FlexMeasuresClient, response, polling_step: int, reauth_once: bool, url: URL
Expand Down Expand Up @@ -92,6 +95,16 @@ def check_content_type(response):


def check_for_status(status, expected_status):
"""Check if status is expected"""
if status != expected_status:
"""Check if status is an acceptable 2xx status code.

Accepts any 2xx status code as success. Logs at INFO level if the status
differs from the expected status but is still a 2xx success code.
Raises ValueError for non-2xx status codes.
"""
if 200 <= status < 300:
if status != expected_status:
logger.info(
f"Received HTTP status {status} instead of expected {expected_status}."
)
else:
raise ValueError(f"Request failed with status code {status}")
34 changes: 34 additions & 0 deletions tests/client/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,37 @@ def test_check_for_status_failure():
"""check_for_status raises ValueError on wrong status."""
with pytest.raises(ValueError, match="Request failed with status code 400"):
check_for_status(400, 200)


def test_check_for_status_exact_match(caplog):
"""check_for_status does not raise and does not log when status matches expected."""
import logging

with caplog.at_level(logging.INFO, logger="flexmeasures_client.response_handling"):
check_for_status(200, 200)
assert caplog.records == []


def test_check_for_status_different_2xx_logs_info(caplog):
"""check_for_status logs at INFO level when status is 2xx but differs from expected."""
import logging

with caplog.at_level(logging.INFO, logger="flexmeasures_client.response_handling"):
check_for_status(202, 200)
assert len(caplog.records) == 1
assert caplog.records[0].levelno == logging.INFO
assert "202" in caplog.records[0].message
assert "200" in caplog.records[0].message


@pytest.mark.parametrize("status", [200, 201, 202, 204, 299])
def test_check_for_status_various_2xx_pass(status):
"""check_for_status does not raise for any 2xx status code, even when it differs from expected."""
check_for_status(status, 200)


@pytest.mark.parametrize("status", [199, 300, 400, 404, 500, 503])
def test_check_for_status_non_2xx_raises(status):
"""check_for_status raises ValueError for non-2xx status codes."""
with pytest.raises(ValueError, match=f"Request failed with status code {status}"):
check_for_status(status, 200)
Loading