Skip to content

Preserve relative paths on link generation #199

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

Merged
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
20 changes: 9 additions & 11 deletions stac_fastapi/pgstac/stac_fastapi/pgstac/models/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def collection_link(self, rel: str = Relations.collection.value) -> Dict:
return dict(
rel=rel,
type=MimeTypes.json.value,
href=self.resolve(f"/collections/{self.collection_id}"),
href=self.resolve(f"collections/{self.collection_id}"),
)


Expand All @@ -189,7 +189,7 @@ def link_items(self) -> Dict:
return dict(
rel="items",
type=MimeTypes.geojson.value,
href=self.resolve(f"/collections/{self.collection_id}/items"),
href=self.resolve(f"collections/{self.collection_id}/items"),
)


Expand All @@ -204,9 +204,7 @@ def link_self(self) -> Dict:
return dict(
rel=Relations.self.value,
type=MimeTypes.geojson.value,
href=self.resolve(
f"/collections/{self.collection_id}/items/{self.item_id}"
),
href=self.resolve(f"collections/{self.collection_id}/items/{self.item_id}"),
)

def link_parent(self) -> Dict:
Expand All @@ -224,7 +222,7 @@ def link_tiles(self) -> Dict:
type=MimeTypes.json.value,
title="tiles",
href=self.resolve(
f"/collections/{self.collection_id}/items/{self.item_id}/tiles",
f"collections/{self.collection_id}/items/{self.item_id}/tiles",
),
)

Expand All @@ -241,15 +239,15 @@ def __post_init__(self):
"""Post init handler."""
self.item_uri = urljoin(
self.base_url,
f"/collections/{self.collection_id}/items/{self.item_id}",
f"collections/{self.collection_id}/items/{self.item_id}",
)

def link_tiles(self) -> Dict:
"""Create tiles link."""
return dict(
href=urljoin(
self.base_url,
f"/titiler/tiles/{{z}}/{{x}}/{{y}}.png?url={self.item_uri}",
f"titiler/tiles/{{z}}/{{x}}/{{y}}.png?url={self.item_uri}",
),
rel=Relations.item.value,
title="tiles",
Expand All @@ -260,7 +258,7 @@ def link_tiles(self) -> Dict:
def link_viewer(self) -> Dict:
"""Create viewer link."""
return dict(
href=urljoin(self.base_url, f"/titiler/viewer?url={self.item_uri}"),
href=urljoin(self.base_url, f"titiler/viewer?url={self.item_uri}"),
rel=Relations.alternate.value,
type=MimeTypes.html.value,
title="viewer",
Expand All @@ -269,7 +267,7 @@ def link_viewer(self) -> Dict:
def link_tilejson(self) -> Dict:
"""Create tilejson link."""
return dict(
href=urljoin(self.base_url, f"/titiler/tilejson.json?url={self.item_uri}"),
href=urljoin(self.base_url, f"titiler/tilejson.json?url={self.item_uri}"),
rel=Relations.alternate.value,
type=MimeTypes.json.value,
title="tilejson",
Expand All @@ -280,7 +278,7 @@ def link_wmts(self) -> Dict:
return dict(
href=urljoin(
self.base_url,
f"/titiler/WMTSCapabilities.xml?url={self.item_uri}",
f"titiler/WMTSCapabilities.xml?url={self.item_uri}",
),
rel=Relations.alternate.value,
type=MimeTypes.xml.value,
Expand Down
21 changes: 21 additions & 0 deletions stac_fastapi/pgstac/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from shapely.geometry import Polygon
from stac_pydantic import Collection, Item
from stac_pydantic.shared import DATETIME_RFC339
from starlette.requests import Request

from stac_fastapi.pgstac.models.links import CollectionLinks


@pytest.mark.asyncio
Expand Down Expand Up @@ -907,3 +910,21 @@ async def test_search_invalid_query_field(app_client):
body = {"query": {"gsd": {"lt": 100}, "invalid-field": {"eq": 50}}}
resp = await app_client.post("/search", json=body)
assert resp.status_code == 400


@pytest.mark.asyncio
async def test_relative_link_construction():
req = Request(
scope={
"type": "http",
"scheme": "http",
"method": "PUT",
"root_path": "http://test/stac",
"path": "/",
"raw_path": b"/tab/abc",
"query_string": b"",
"headers": {},
}
)
links = CollectionLinks(collection_id="naip", request=req)
assert links.link_items()["href"] == "http://test/stac/collections/naip/items"
26 changes: 17 additions & 9 deletions stac_fastapi/types/stac_fastapi/types/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ async def delete_collection(
class LandingPageMixin:
"""Create a STAC landing page (GET /)."""

conformance_classes: List[str] = attr.ib()
stac_version: str = attr.ib(default=STAC_VERSION)
landing_page_id: str = attr.ib(default="stac-fastapi")
title: str = attr.ib(default="stac-fastapi")
Expand All @@ -237,10 +238,7 @@ def _landing_page(self, base_url: str) -> stac_types.LandingPage:
title=self.title,
description=self.description,
stac_version=self.stac_version,
conformsTo=[
"https://stacspec.org/STAC-api.html",
"http://docs.opengeospatial.org/is/17-069r3/17-069r3.html#ats_geojson",
],
conformsTo=self.conformance_classes,
links=[
{
"rel": Relations.self.value,
Expand All @@ -262,7 +260,7 @@ def _landing_page(self, base_url: str) -> stac_types.LandingPage:
"rel": Relations.conformance.value,
"type": MimeTypes.json,
"title": "STAC/WFS3 conformance classes implemented by this server",
"href": base_url,
"href": urljoin(base_url, "conformance"),
},
{
"rel": Relations.search.value,
Expand All @@ -285,6 +283,12 @@ class BaseCoreClient(LandingPageMixin, abc.ABC):
"""

extensions: List[ApiExtension] = attr.ib(default=attr.Factory(list))
conformance_classes: List[str] = attr.ib(
factory=lambda: [
"https://stacspec.org/STAC-api.html",
"http://docs.opengeospatial.org/is/17-069r3/17-069r3.html#ats_geojson",
]
)

def extension_is_enabled(self, extension: Type[ApiExtension]) -> bool:
"""Check if an api extension is enabled."""
Expand Down Expand Up @@ -312,7 +316,6 @@ def landing_page(self, **kwargs) -> stac_types.LandingPage:
)
return landing_page

@abc.abstractmethod
def conformance(self, **kwargs) -> stac_types.Conformance:
"""Conformance classes.

Expand All @@ -321,7 +324,7 @@ def conformance(self, **kwargs) -> stac_types.Conformance:
Returns:
Conformance classes which the server conforms to.
"""
...
stac_types.Conformance(conformsTo=self.conformance_classes)

@abc.abstractmethod
def post_search(
Expand Down Expand Up @@ -430,6 +433,12 @@ class AsyncBaseCoreClient(LandingPageMixin, abc.ABC):
"""

extensions: List[ApiExtension] = attr.ib(default=attr.Factory(list))
conformance_classes: List[str] = attr.ib(
factory=lambda: [
"https://stacspec.org/STAC-api.html",
"http://docs.opengeospatial.org/is/17-069r3/17-069r3.html#ats_geojson",
]
)

def extension_is_enabled(self, extension: Type[ApiExtension]) -> bool:
"""Check if an api extension is enabled."""
Expand Down Expand Up @@ -457,7 +466,6 @@ async def landing_page(self, **kwargs) -> stac_types.LandingPage:
)
return landing_page

@abc.abstractmethod
async def conformance(self, **kwargs) -> stac_types.Conformance:
"""Conformance classes.

Expand All @@ -466,7 +474,7 @@ async def conformance(self, **kwargs) -> stac_types.Conformance:
Returns:
Conformance classes which the server conforms to.
"""
...
stac_types.Conformance(conformsTo=self.conformance_classes)

@abc.abstractmethod
async def post_search(
Expand Down