Skip to content

Commit

Permalink
feat: add stream_href method
Browse files Browse the repository at this point in the history
  • Loading branch information
jkeifer committed Nov 17, 2023
1 parent 5683121 commit 44b85d8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
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

- `stream_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 @@ -22,6 +22,7 @@
download_item,
download_item_collection,
read_href,
stream_href,
)
from .client import Client
from .config import Config
Expand Down Expand Up @@ -64,4 +65,5 @@
"download_item",
"download_item_collection",
"read_href",
"stream_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 stream_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 stream_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_stream_href(data_path: Path) -> None:
text = b""
async for chunk in stac_asset.stream_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

0 comments on commit 44b85d8

Please sign in to comment.