Skip to content

Base HERE API

Base classes shared by HERE API clients.

HEREApi

Base class from which all wrappers inherit.

Source code in src/herepy/here_api.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class HEREApi:
    """Base class from which all wrappers inherit."""

    def __init__(self, api_key: str = None, timeout: int = None):
        """
        Initialize the API client.

        Args:
          api_key (str):
            API key taken from HERE Developer Portal.
          timeout (int):
            Timeout limit for requests.

        """
        self.__set_credentials(api_key)
        if timeout:
            self._timeout = timeout
        else:
            self._timeout = 20
        self._async_client: httpx2.AsyncClient | None = None

    async def __aenter__(self):
        """Open a shared async HTTP client for use by async API methods."""
        if self._async_client is not None:
            raise RuntimeError("This API client is already open.")
        self._async_client = httpx2.AsyncClient(timeout=self._timeout)
        return self

    async def __aexit__(self, exc_type, exc_value, traceback):
        """Close the shared async HTTP client."""
        await self.aclose()

    async def aclose(self):
        """Close the shared async HTTP client."""
        if self._async_client is not None:
            client = self._async_client
            self._async_client = None
            await client.aclose()

    def _get_async_client(self) -> httpx2.AsyncClient:
        if self._async_client is None:
            raise RuntimeError("Use this API client with 'async with' before calling async methods.")
        return self._async_client

    def __set_credentials(self, api_key):
        """
        Setter for credentials.

        Args:
          api_key (str):
            API key taken from HERE Developer Portal.

        """
        self._api_key = api_key

__aenter__() async

Open a shared async HTTP client for use by async API methods.

Source code in src/herepy/here_api.py
28
29
30
31
32
33
async def __aenter__(self):
    """Open a shared async HTTP client for use by async API methods."""
    if self._async_client is not None:
        raise RuntimeError("This API client is already open.")
    self._async_client = httpx2.AsyncClient(timeout=self._timeout)
    return self

__aexit__(exc_type, exc_value, traceback) async

Close the shared async HTTP client.

Source code in src/herepy/here_api.py
35
36
37
async def __aexit__(self, exc_type, exc_value, traceback):
    """Close the shared async HTTP client."""
    await self.aclose()

__init__(api_key=None, timeout=None)

Initialize the API client.

Parameters:

Name Type Description Default
api_key str

API key taken from HERE Developer Portal.

None
timeout int

Timeout limit for requests.

None
Source code in src/herepy/here_api.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def __init__(self, api_key: str = None, timeout: int = None):
    """
    Initialize the API client.

    Args:
      api_key (str):
        API key taken from HERE Developer Portal.
      timeout (int):
        Timeout limit for requests.

    """
    self.__set_credentials(api_key)
    if timeout:
        self._timeout = timeout
    else:
        self._timeout = 20
    self._async_client: httpx2.AsyncClient | None = None

__set_credentials(api_key)

Setter for credentials.

Parameters:

Name Type Description Default
api_key str

API key taken from HERE Developer Portal.

required
Source code in src/herepy/here_api.py
51
52
53
54
55
56
57
58
59
60
def __set_credentials(self, api_key):
    """
    Setter for credentials.

    Args:
      api_key (str):
        API key taken from HERE Developer Portal.

    """
    self._api_key = api_key

aclose() async

Close the shared async HTTP client.

Source code in src/herepy/here_api.py
39
40
41
42
43
44
async def aclose(self):
    """Close the shared async HTTP client."""
    if self._async_client is not None:
        client = self._async_client
        self._async_client = None
        await client.aclose()