Skip to content

Commit

Permalink
add sort param support in CDX API class (#156)
Browse files Browse the repository at this point in the history
see https://nla.github.io/outbackcdx/api.html#operation/query

sort takes string input which must be one of the follwoing:
- default
- closest
- reverse

This commit shall help in closing issue at #155
  • Loading branch information
akamhy authored Feb 17, 2022
1 parent f63c6ad commit 3a44a71
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/test_cdx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
check_collapses,
check_filters,
check_match_type,
check_sort,
full_url,
get_response,
get_total_pages,
Expand Down Expand Up @@ -101,3 +102,12 @@ def test_check_match_type() -> None:

with pytest.raises(WaybackError):
check_match_type("not a valid type", "url")


def test_check_sort() -> None:
assert check_sort("default")
assert check_sort("closest")
assert check_sort("reverse")

with pytest.raises(WaybackError):
assert check_sort("random crap")
7 changes: 7 additions & 0 deletions waybackpy/cdx_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
check_collapses,
check_filters,
check_match_type,
check_sort,
full_url,
get_response,
get_total_pages,
Expand Down Expand Up @@ -44,6 +45,7 @@ def __init__(
end_timestamp: Optional[str] = None,
filters: Optional[List[str]] = None,
match_type: Optional[str] = None,
sort: Optional[str] = None,
gzip: Optional[str] = None,
collapses: Optional[List[str]] = None,
limit: Optional[str] = None,
Expand All @@ -57,6 +59,8 @@ def __init__(
check_filters(self.filters)
self.match_type = None if match_type is None else str(match_type).strip()
check_match_type(self.match_type, self.url)
self.sort = None if sort is None else str(sort).strip()
check_sort(self.sort)
self.gzip = gzip
self.collapses = [] if collapses is None else collapses
check_collapses(self.collapses)
Expand Down Expand Up @@ -165,6 +169,9 @@ def add_payload(self, payload: Dict[str, str]) -> None:
if self.match_type:
payload["matchType"] = self.match_type

if self.sort:
payload["sort"] = self.sort

if self.filters and len(self.filters) > 0:
for i, _filter in enumerate(self.filters):
payload["filter" + str(i)] = _filter
Expand Down
21 changes: 21 additions & 0 deletions waybackpy/cdx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,24 @@ def check_match_type(match_type: Optional[str], url: str) -> bool:
raise WaybackError(exc_message)

return True


def check_sort(sort: Optional[str]) -> bool:
"""
Check that the sort argument passed by the end-user is valid.
If not valid then raise WaybackError.
"""

legal_sort = ["default", "closest", "reverse"]

if not sort:
return True

if sort not in legal_sort:
exc_message = (
f"{sort} is not an allowed argument for sort.\n"
"Use one from 'default', 'closest' or 'reverse'"
)
raise WaybackError(exc_message)

return True

0 comments on commit 3a44a71

Please sign in to comment.