Skip to content
Open
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
21 changes: 21 additions & 0 deletions openrouteservice/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ def __init__(
)

self._req = None
def validate_key(self, profile="driving-car", coordinates=None) -> bool:
"""
Validate the configured API key by performing a lightweight request.

Returns True if the key appears valid.
Raises openrouteservice.exceptions.ApiError for invalid keys or other API errors.
"""
if coordinates is None:
# Two close points (small/cheap request)
coordinates = ((8.681495, 49.41461), (8.686507, 49.41943))

# Minimal directions request; invalid keys will raise ApiError from request()
self.request(
url=f"/v2/directions/{profile}",
post_json={
"coordinates": coordinates,
"instructions": False,
"geometry": False,
},
)
return True

def request(
self,
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/client/test_validate_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest
from unittest.mock import patch

import openrouteservice
from openrouteservice.exceptions import ApiError


def test_validate_key_success():
client = openrouteservice.Client(key="valid_key")

with patch.object(client, "request", return_value={"ok": True}) as mock_request:
result = client.validate_key()
assert result is True
mock_request.assert_called_once()


def test_validate_key_invalid_key_raises():
client = openrouteservice.Client(key="invalid_key")

with patch.object(client, "request", side_effect=ApiError(403, "Forbidden")):
with pytest.raises(ApiError):
client.validate_key()