|
| 1 | +import os |
1 | 2 | from uuid import UUID |
2 | 3 |
|
| 4 | +from loguru import logger |
| 5 | + |
3 | 6 | from _tilebox.grpc.aio.channel import open_channel |
4 | 7 | from _tilebox.grpc.aio.error import with_pythonic_errors |
5 | 8 | from _tilebox.grpc.error import NotFoundError |
6 | 9 | from tilebox.datasets.aio.dataset import DatasetClient |
| 10 | +from tilebox.datasets.client import _TILEBOX_API_KEY_ENV_VAR, _TILEBOX_API_URL |
7 | 11 | from tilebox.datasets.client import Client as BaseClient |
8 | | -from tilebox.datasets.client import token_from_env |
9 | 12 | from tilebox.datasets.data.datasets import DatasetKind, FieldDict |
10 | 13 | from tilebox.datasets.datasets.v1.collections_pb2_grpc import CollectionServiceStub |
11 | 14 | from tilebox.datasets.datasets.v1.data_access_pb2_grpc import DataAccessServiceStub |
|
16 | 19 |
|
17 | 20 |
|
18 | 21 | class Client: |
19 | | - def __init__(self, *, url: str = "https://api.tilebox.com", token: str | None = None) -> None: |
| 22 | + def __init__( |
| 23 | + self, *, url: str = _TILEBOX_API_URL, token: str | None = None, warn_if_unauthenticated: bool = True |
| 24 | + ) -> None: |
20 | 25 | """ |
21 | 26 | Create a Tilebox datasets client. |
22 | 27 |
|
23 | 28 | Args: |
24 | 29 | url: Tilebox API Url. Defaults to "https://api.tilebox.com". |
25 | 30 | token: The API Key to authenticate with. If not set the `TILEBOX_API_KEY` environment variable will be used. |
| 31 | + If no token is provided or found, anonymous open data access will be used. |
| 32 | + warn_if_unauthenticated: Whether to warn if no API key is provided and the client is used with the default |
| 33 | + Tilebox API URL. Defaults to True. |
26 | 34 | """ |
27 | | - channel = open_channel(url, token_from_env(url, token)) |
| 35 | + if token is None: |
| 36 | + token = os.environ.get(_TILEBOX_API_KEY_ENV_VAR, None) |
| 37 | + |
| 38 | + if token is None and url == _TILEBOX_API_URL and warn_if_unauthenticated: |
| 39 | + logger.opt(colors=True).info( |
| 40 | + "<yellow>" |
| 41 | + "No Tilebox API key detected. Using anonymous Open Data access without authentication. " |
| 42 | + "For higher throughput and rate limits, sign up for a free Tilebox at https://console.tilebox.com." |
| 43 | + "</yellow>" |
| 44 | + ) |
| 45 | + |
| 46 | + channel = open_channel(url, token) |
28 | 47 | dataset_service_stub = with_pythonic_errors(DatasetServiceStub(channel)) |
29 | 48 | collection_service_stub = with_pythonic_errors(CollectionServiceStub(channel)) |
30 | 49 | data_access_service_stub = with_pythonic_errors(DataAccessServiceStub(channel)) |
|
0 commit comments