Skip to content

Commit

Permalink
Use timeout settings from ClientSession (#62)
Browse files Browse the repository at this point in the history
* use timeout settings from clientsession

* use response within context

* remove unused import

* Remove test

* Formatting

---------

Co-authored-by: Jan-Philipp Benecke <[email protected]>
  • Loading branch information
mib1185 and jpbede authored Feb 17, 2024
1 parent bcac535 commit cf680fe
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 35 deletions.
12 changes: 3 additions & 9 deletions aiotankerkoenig/aiotankerkoenig.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Tankerkoenig API client."""
from __future__ import annotations

import asyncio
from dataclasses import dataclass
from importlib import metadata
import socket
Expand Down Expand Up @@ -34,7 +33,6 @@ class Tankerkoenig:

api_key: str
session: ClientSession | None = None
request_timeout: int = 10

_close_session: bool = False

Expand All @@ -61,12 +59,10 @@ async def _request(self, path: str, params: dict[str, Any]) -> str:
)

try:
async with asyncio.timeout(self.request_timeout):
response = await self.session.get(
url,
headers=headers,
)
async with self.session.get(url, headers=headers) as response:
response.raise_for_status()
content_type = response.headers.get("Content-Type", "")
text = await response.text()
except TimeoutError as exception:
msg = "Timeout occurred while connecting to tankerkoenig.de API"
raise TankerkoenigConnectionTimeoutError(
Expand All @@ -81,8 +77,6 @@ async def _request(self, path: str, params: dict[str, Any]) -> str:
msg = "Error occurred while communicating with the tankerkoenig.de API"
raise TankerkoenigConnectionError(msg) from exception

content_type = response.headers.get("Content-Type", "")
text = await response.text()
if "application/json" not in content_type:
msg = "Unexpected content type from tankerkoenig.de API"
raise TankerkoenigError(
Expand Down
27 changes: 1 addition & 26 deletions tests/test_aiotankerkoenig.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""Test the tankerkoenig API client."""
import asyncio
from typing import Any

import aiohttp
from aioresponses import CallbackResult, aioresponses
from aioresponses import aioresponses
import pytest
from syrupy import SnapshotAssertion

Expand All @@ -12,7 +10,6 @@
Sort,
Tankerkoenig,
TankerkoenigConnectionError,
TankerkoenigConnectionTimeoutError,
TankerkoenigError,
TankerkoenigInvalidKeyError,
TankerkoenigRateLimitError,
Expand All @@ -22,28 +19,6 @@
TANKERKOENIG_ENDPOINT = "https://creativecommons.tankerkoenig.de"


async def test_timeout(
responses: aioresponses,
) -> None:
"""Test request timeout."""

async def response_handler(_: str, **_kwargs: Any) -> CallbackResult:
"""Response handler for this test."""
await asyncio.sleep(2)
return CallbackResult(body="Good morning!")

responses.get(
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1",
callback=response_handler,
)
async with Tankerkoenig(
api_key="abc123",
request_timeout=1,
) as tankerkoenig_client:
with pytest.raises(TankerkoenigConnectionTimeoutError):
await tankerkoenig_client.station_details(station_id="1")


async def test_unexpected_server_response(
responses: aioresponses,
tankerkoenig_client: Tankerkoenig,
Expand Down

0 comments on commit cf680fe

Please sign in to comment.