Skip to content

Commit

Permalink
feat: add client override to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Nov 4, 2024
1 parent 7f62102 commit 5bad9e4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/stac_asset/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def cli() -> None:
@cli.command()
@click.argument("href", required=False)
@click.argument("directory", required=False)
@click.option(
"-c",
"--client",
help="Set the client to use for all downloads. If not "
"provided, the client will be guessed from the asset href.",
)
@click.option(
"-p",
"--path-template",
Expand Down Expand Up @@ -164,6 +170,7 @@ def cli() -> None:
def download(
href: str | None,
directory: str | None,
client: str | None,
path_template: str | None,
alternate_assets: list[str],
include: list[str],
Expand Down Expand Up @@ -213,6 +220,7 @@ def download(
download_async(
href,
directory,
client,
path_template,
alternate_assets,
include,
Expand All @@ -237,6 +245,7 @@ def download(
async def download_async(
href: str | None,
directory: str | None,
client: str | None,
path_template: str | None,
alternate_assets: list[str],
include: list[str],
Expand Down Expand Up @@ -275,6 +284,7 @@ async def download_async(
warn=not fail_fast,
fail_fast=fail_fast,
overwrite=overwrite,
client_override=client,
)

input_dict = await read_as_dict(href, config)
Expand Down
30 changes: 28 additions & 2 deletions src/stac_asset/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,21 @@ async def get_client(self, href: str) -> Client:
Returns:
Client: An instance of that client.
"""
# TODO allow dynamic registration of new clients, e.g. via a plugin mechanism

from .earthdata_client import EarthdataClient
from .filesystem_client import FilesystemClient
from .http_client import HttpClient
from .planetary_computer_client import PlanetaryComputerClient
from .s3_client import S3Client

url = URL(href)
if not url.host:
client_class: type[Client] = FilesystemClient
if self.config.client_override:
client_class: type[Client] = _get_client_class_by_name(
self.config.client_override
)
elif not url.host:
client_class = FilesystemClient
elif url.scheme == "s3":
client_class = S3Client
elif url.host.endswith("blob.core.windows.net"):
Expand All @@ -241,3 +247,23 @@ async def close_all(self) -> None:
async with self.lock:
for client in self.clients.values():
await client.close()


def _get_client_class_by_name(name: str) -> type[Client]:
from .earthdata_client import EarthdataClient
from .filesystem_client import FilesystemClient
from .http_client import HttpClient
from .planetary_computer_client import PlanetaryComputerClient
from .s3_client import S3Client

client_classes: list[type[Client]] = [
EarthdataClient,
FilesystemClient,
HttpClient,
PlanetaryComputerClient,
S3Client,
]
for client_class in client_classes:
if client_class.name == name:
return client_class
raise ValueError(f"no client with name: {name}")
6 changes: 6 additions & 0 deletions src/stac_asset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class Config:
overwrite: bool = False
"""Download files even if they already exist locally."""

client_override: str | None = None
"""Use the same client for all asset requests.
If not set, each asset's client will be guessed from its href.
"""

http_client_timeout: float | None = DEFAULT_HTTP_CLIENT_TIMEOUT
"""Total number of seconds for the whole request."""

Expand Down

0 comments on commit 5bad9e4

Please sign in to comment.