Skip to content

Commit

Permalink
feat: add read_href method
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Sep 18, 2023
1 parent 69f757d commit da9b5a9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 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

- `read_href` and `blocking.read_href` ([#107](https://github.com/stac-utils/stac-asset/pull/107))

## [0.2.1] - 2023-09-05

### Fixed
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,
read_href,
)
from .client import Client
from .config import Config
Expand Down Expand Up @@ -62,4 +63,5 @@
"download_collection",
"download_item",
"download_item_collection",
"read_href",
]
23 changes: 23 additions & 0 deletions src/stac_asset/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,29 @@ async def asset_exists(
return True


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
"""
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


def make_asset_hrefs_relative(
stac_object: Union[Item, Collection]
) -> Union[Item, Collection]:
Expand Down
16 changes: 16 additions & 0 deletions src/stac_asset/blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,19 @@ def asset_exists(
bool: Whether the asset exists or not
"""
return asyncio.run(_functions.asset_exists(asset, config, clients))


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
"""
return asyncio.run(_functions.read_href(href, config, clients))
10 changes: 8 additions & 2 deletions tests/test_blocking.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from pathlib import Path

import stac_asset.blocking
Expand Down Expand Up @@ -32,9 +33,14 @@ def test_download_asset(tmp_path: Path, item: Item) -> None:
assert asset.href == str(tmp_path / "image.jpg")


def test_assert_asset_exists(tmp_path: Path, item: Item) -> None:
def test_assert_asset_exists(item: Item) -> None:
stac_asset.blocking.assert_asset_exists(item.assets["data"])


def test_asset_exists(tmp_path: Path, item: Item) -> None:
def test_asset_exists(item: Item) -> None:
assert stac_asset.blocking.asset_exists(item.assets["data"])


def test_read_href(data_path: Path) -> None:
text = stac_asset.blocking.read_href(str(data_path / "item.json"))
Item.from_dict(json.loads(text))
8 changes: 7 additions & 1 deletion tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os.path
from asyncio import Queue
from pathlib import Path
Expand Down Expand Up @@ -204,7 +205,12 @@ async def test_asset_exists(item: Item) -> None:
assert not await stac_asset.asset_exists(Asset(href="not-a-file"))


async def test_asert_asset_exists(item: Item) -> None:
async def test_assert_asset_exists(item: Item) -> None:
await stac_asset.assert_asset_exists(item.assets["data"])
with pytest.raises(ValueError):
await stac_asset.assert_asset_exists(Asset(href="not-a-file"))


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))

0 comments on commit da9b5a9

Please sign in to comment.