Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add stream_href method #123

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Added

- `open_href` ([#123](https://github.com/stac-utils/stac-asset/pull/123))

## [0.2.3] - 2023-10-20

### Added
Expand Down
2 changes: 2 additions & 0 deletions src/stac_asset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
download_collection,
download_item,
download_item_collection,
open_href,
read_href,
)
from .client import Client
Expand Down Expand Up @@ -63,5 +64,6 @@
"download_collection",
"download_item",
"download_item_collection",
"open_href",
"read_href",
]
32 changes: 25 additions & 7 deletions src/stac_asset/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path
from types import TracebackType
from typing import (
AsyncIterator,
List,
Optional,
Set,
Expand Down Expand Up @@ -456,27 +457,44 @@ async def asset_exists(
return True


async def read_href(
async def open_href(
href: str, config: Optional[Config] = None, clients: Optional[List[Client]] = None
) -> bytes:
"""Reads an href and returns its bytes.
) -> AsyncIterator[bytes]:
"""Opens an href and yields byte chunks.

Args:
href: The href to read
config: The download configuration to use
clients: Any pre-configured clients to use

Returns:
Yields:
bytes: The bytes from the href
"""
if config is None:
config = Config()
clients_ = Clients(config, clients=clients)
async with await clients_.get_client(href) as client:
data = b""
async for chunk in client.open_href(href):
data += chunk
return data
yield chunk


async def read_href(
href: str, config: Optional[Config] = None, clients: Optional[List[Client]] = None
) -> bytes:
"""Reads an href and returns its bytes.

Args:
href: The href to read
config: The download configuration to use
clients: Any pre-configured clients to use

Returns:
bytes: The bytes from the href
"""
data = b""
async for chunk in open_href(href, config=config, clients=clients):
data += chunk
return data


def make_asset_hrefs_relative(
Expand Down
7 changes: 7 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ async def test_assert_asset_exists(item: Item) -> None:
await stac_asset.assert_asset_exists(Asset(href="not-a-file"))


async def test_open_href(data_path: Path) -> None:
text = b""
async for chunk in stac_asset.open_href(str(data_path / "item.json")):
text += chunk
Item.from_dict(json.loads(text))


async def test_read_href(data_path: Path) -> None:
text = await stac_asset.read_href(str(data_path / "item.json"))
Item.from_dict(json.loads(text))
Expand Down