Skip to content

Commit

Permalink
feat!: support multiple clients per item
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Aug 7, 2023
1 parent 0e7d9a2 commit e363d5c
Show file tree
Hide file tree
Showing 18 changed files with 1,486 additions and 389 deletions.
6 changes: 2 additions & 4 deletions src/stac_asset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from .config import Config
from .earthdata_client import EarthdataClient
from .errors import (
AssetDownloadError,
AssetOverwriteError,
CannotIncludeAndExclude,
DownloadError,
Expand All @@ -24,7 +23,7 @@
from .functions import (
download_item,
download_item_collection,
guess_client,
guess_client_class,
)
from .http_client import HttpClient
from .planetary_computer_client import PlanetaryComputerClient
Expand All @@ -33,7 +32,6 @@

__all__ = [
"DownloadWarning",
"AssetDownloadError",
"AssetOverwriteError",
"CannotIncludeAndExclude",
"Client",
Expand All @@ -47,5 +45,5 @@
"S3Client",
"download_item",
"download_item_collection",
"guess_client",
"guess_client_class",
]
30 changes: 19 additions & 11 deletions src/stac_asset/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def cli() -> None:
@cli.command()
@click.argument("href", required=False)
@click.argument("directory", required=False)
@click.option(
"-a",
"--alternate-assets",
help="Alternate asset hrefs to prefer, if available",
multiple=True,
)
@click.option("-i", "--include", help="Asset keys to include", multiple=True)
@click.option(
"-x",
Expand Down Expand Up @@ -57,6 +63,7 @@ def cli() -> None:
def download(
href: Optional[str],
directory: Optional[str],
alternate_assets: List[str],
include: List[str],
exclude: List[str],
file_name: Optional[str],
Expand All @@ -83,21 +90,22 @@ def download(
$ stac-asset download -i asset-key-to-include item.json
"""
if href is None or href == "-":
input_dict = json.load(sys.stdin)
else:
input_dict = json.loads(asyncio.run(read_file(href)))
if directory is None:
directory = os.getcwd()

config = Config(
alternate_assets=alternate_assets,
include=include,
exclude=exclude,
file_name=file_name,
s3_requester_pays=s3_requester_pays,
warn=warn,
)

if href is None or href == "-":
input_dict = json.load(sys.stdin)
else:
input_dict = json.loads(asyncio.run(read_file(href, config)))
if directory is None:
directory = os.getcwd()

type_ = input_dict.get("type")
if type_ is None:
print("ERROR: missing 'type' field on input dictionary", file=sys.stderr)
Expand Down Expand Up @@ -131,10 +139,10 @@ def download(
json.dump(output.to_dict(transform_hrefs=False), sys.stdout)


async def read_file(
href: str,
) -> bytes:
async with await functions.guess_client(href) as client:
async def read_file(href: str, config: Config) -> bytes:
async with await functions.guess_client_class_from_href(href).from_config(
config
) as client:
data = b""
async for chunk in client.open_href(href):
data += chunk
Expand Down
Loading

0 comments on commit e363d5c

Please sign in to comment.