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

Add timeout option #14

Merged
merged 2 commits into from
Sep 15, 2023
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
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,40 @@

Asynchronous Waze client for calculating routes and travel times.

Based on [WazeRouteCalculator](https://github.com/kovacsbalu/WazeRouteCalculator)

## Installation

```console
pip install pywaze
```

## Usage

```python
#!/usr/bin/env python3

import asyncio
from pywaze import route_calculator


async def get_time(start: str, end: str) -> float:
"""Return the travel time home."""

async with route_calculator.WazeRouteCalculator() as client:
results = await client.calc_all_routes_info(start, end)
route_time, _ = list(results.values())[0]
return route_time


start = "50.00332659227126,8.262322651915843"
end = "50.08414976707619,8.247836017342934"

travel_time = asyncio.run(get_time(start, end))

print(travel_time)
```

---

[<img src="https://raw.githubusercontent.com/eifinger/setup-rye/main/docs/images/bmc-button.svg" width=150 height=40 style="margin: 5px"/>](https://www.buymeacoffee.com/eifinger)
Expand Down
14 changes: 11 additions & 3 deletions src/pywaze/route_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ def __init__(
self,
region="EU",
client: httpx.AsyncClient | None = None,
timeout: int = 10,
):
self.region = region
self.client = client
self.timeout = timeout
if self.client is None:
self.client = httpx.AsyncClient()
self.client = httpx.AsyncClient(timeout=timeout)

def already_coords(self, address: str) -> bool:
"""Already coordinates or address."""
Expand Down Expand Up @@ -91,7 +93,10 @@ async def address_to_coords(self, address: str) -> dict[str, Any]:

try:
response: httpx.Response = await self.client.get(
self.WAZE_URL + get_cord, params=url_options, headers=self.HEADERS
self.WAZE_URL + get_cord,
params=url_options,
headers=self.HEADERS,
timeout=self.timeout,
)
except httpx.TimeoutException as e:
raise WRCTimeoutError("Timeout getting coords for %s" % address) from e
Expand Down Expand Up @@ -154,7 +159,10 @@ async def get_route(

try:
response: httpx.Response = await self.client.get(
self.WAZE_URL + routing_server, params=url_options, headers=self.HEADERS
self.WAZE_URL + routing_server,
params=url_options,
headers=self.HEADERS,
timeout=self.timeout,
)
except httpx.TimeoutException as e:
raise WRCTimeoutError("Timeout getting route") from e
Expand Down