Skip to content

Commit

Permalink
fix: don't strip slash from root href
Browse files Browse the repository at this point in the history
Some servers require it (per #373 (comment)).
  • Loading branch information
gadomski committed Jan 17, 2023
1 parent bd5b817 commit e5de97f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pystac_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ def open(
Return:
catalog : A :class:`Client` instance for this Catalog/API
"""
url = url.rstrip("/")
client: Client = cls.from_file(
url,
headers=headers,
Expand Down Expand Up @@ -254,7 +253,7 @@ def get_collection(self, collection_id: str) -> Optional[Collection]:
CollectionClient: A STAC Collection
"""
if self._supports_collections() and self._stac_io:
url = f"{self.get_self_href()}/collections/{collection_id}"
url = self._get_href(f"collections/{collection_id}")
collection = CollectionClient.from_dict(
self._stac_io.read_json(url),
root=self,
Expand Down Expand Up @@ -282,7 +281,7 @@ def get_collections(self) -> Iterator[Collection]:
collection: Union[Collection, CollectionClient]

if self._supports_collections() and self.get_self_href() is not None:
url = f"{self.get_self_href()}/collections"
url = self._get_href("collections")
for page in self._stac_io.get_pages(url): # type: ignore
if "collections" not in page:
raise APIError("Invalid response from /collections")
Expand Down Expand Up @@ -504,3 +503,12 @@ def get_search_link(self) -> Optional[pystac.Link]:
),
None,
)

def _get_href(self, path: str) -> str:
self_href = self.get_self_href()
if self_href is None:
return path
elif self_href.endswith("/"):
return f"{self_href}{path}"
else:
return f"{self_href}/{path}"
9 changes: 9 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ def test_get_collections_single_slash(self, requests_mock: Mocker) -> None:
assert len(history) == 2
assert history[1].url == f"{root_url}collections"

def test_keep_trailing_slash_on_root(self, requests_mock: Mocker) -> None:
pc_root_text = read_data_file("planetary-computer-root.json")
root_url = "http://pystac-client.test/"
requests_mock.get(root_url, status_code=200, text=pc_root_text)
client = Client.open(root_url)
self_href = client.get_self_href()
assert self_href
assert self_href.endswith("/")

def test_custom_request_parameters(self, requests_mock: Mocker) -> None:
pc_root_text = read_data_file("planetary-computer-root.json")
pc_collection_dict = read_data_file(
Expand Down

0 comments on commit e5de97f

Please sign in to comment.