Skip to content

Commit

Permalink
wrap httpx.TimeoutException (#12)
Browse files Browse the repository at this point in the history
This allows to catch it via the generic WRCError or with the new WRCTimeoutError
  • Loading branch information
eifinger authored Sep 9, 2023
1 parent 0ff55e1 commit 0db5bc1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/pywaze/route_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class WRCError(Exception):
"""Waze Route Calculator Error."""


class WRCTimeoutError(WRCError):
"""Waze Route Calculator Timeout Error."""


class WazeRouteCalculator:
"""Calculate actual route time and distance with Waze API."""

Expand Down Expand Up @@ -85,9 +89,12 @@ async def address_to_coords(self, address: str) -> dict[str, Any]:
"lon": base_coords["lon"],
}

response: httpx.Response = await self.client.get(
self.WAZE_URL + get_cord, params=url_options, headers=self.HEADERS
)
try:
response: httpx.Response = await self.client.get(
self.WAZE_URL + get_cord, params=url_options, headers=self.HEADERS
)
except httpx.TimeoutException as e:
raise WRCTimeoutError("Timeout getting coords for %s" % address) from e
for response_json in response.json():
if response_json.get("city"):
lat = response_json["location"]["lat"]
Expand Down Expand Up @@ -145,9 +152,12 @@ async def get_route(
if avoid_subscription_roads is False:
url_options["subscription"] = "*"

response: httpx.Response = await self.client.get(
self.WAZE_URL + routing_server, params=url_options, headers=self.HEADERS
)
try:
response: httpx.Response = await self.client.get(
self.WAZE_URL + routing_server, params=url_options, headers=self.HEADERS
)
except httpx.TimeoutException as e:
raise WRCTimeoutError("Timeout getting route") from e
response_json = self._check_response(response)
if response_json:
if "error" in response_json:
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import Any
from httpx import Response
import httpx
import pytest
from respx import MockRouter

Expand All @@ -22,6 +23,14 @@ def get_route_mock(get_route_response: dict[str, Any], respx_mock: MockRouter):
)


@pytest.fixture
def timeout_mock(respx_mock: MockRouter):
"""Throw a httpx.TimeoutException when calculating routes."""
yield respx_mock.get("https://www.waze.com/row-RoutingManager/routingRequest").mock(
side_effect=httpx.TimeoutException("Timeout")
)


@pytest.fixture()
def wiesbaden_to_coords_mock(respx_mock: MockRouter):
"""Return the provided json response when converting this address to coordinates."""
Expand Down
11 changes: 11 additions & 0 deletions tests/test_route_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,14 @@ async def test_calc_all_routes_info(
route_time, route_distance = list(results.values())[0]
assert route_time == expected_route_time
assert route_distance == expected_route_distance


async def test_calc_route_info_timeout(timeout_mock):
"""Test calc_route_info with timeout."""

async with route_calculator.WazeRouteCalculator() as client:
with pytest.raises(route_calculator.WRCTimeoutError):
await client.calc_route_info(
"50.00332659227126,8.262322651915843",
"50.08414976707619,8.247836017342934",
)

0 comments on commit 0db5bc1

Please sign in to comment.