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