add sort param support in CDX API class (#156)

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 https://github.com/akamhy/waybackpy/issues/155
This commit is contained in:
Akash Mahanty 2022-02-17 12:17:23 +05:30 committed by GitHub
parent f63c6adf79
commit 3a44a710d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 0 deletions

View File

@ -6,6 +6,7 @@ from waybackpy.cdx_utils import (
check_collapses, check_collapses,
check_filters, check_filters,
check_match_type, check_match_type,
check_sort,
full_url, full_url,
get_response, get_response,
get_total_pages, get_total_pages,
@ -101,3 +102,12 @@ def test_check_match_type() -> None:
with pytest.raises(WaybackError): with pytest.raises(WaybackError):
check_match_type("not a valid type", "url") 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")

View File

@ -16,6 +16,7 @@ from .cdx_utils import (
check_collapses, check_collapses,
check_filters, check_filters,
check_match_type, check_match_type,
check_sort,
full_url, full_url,
get_response, get_response,
get_total_pages, get_total_pages,
@ -44,6 +45,7 @@ class WaybackMachineCDXServerAPI:
end_timestamp: Optional[str] = None, end_timestamp: Optional[str] = None,
filters: Optional[List[str]] = None, filters: Optional[List[str]] = None,
match_type: Optional[str] = None, match_type: Optional[str] = None,
sort: Optional[str] = None,
gzip: Optional[str] = None, gzip: Optional[str] = None,
collapses: Optional[List[str]] = None, collapses: Optional[List[str]] = None,
limit: Optional[str] = None, limit: Optional[str] = None,
@ -57,6 +59,8 @@ class WaybackMachineCDXServerAPI:
check_filters(self.filters) check_filters(self.filters)
self.match_type = None if match_type is None else str(match_type).strip() self.match_type = None if match_type is None else str(match_type).strip()
check_match_type(self.match_type, self.url) 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.gzip = gzip
self.collapses = [] if collapses is None else collapses self.collapses = [] if collapses is None else collapses
check_collapses(self.collapses) check_collapses(self.collapses)
@ -165,6 +169,9 @@ class WaybackMachineCDXServerAPI:
if self.match_type: if self.match_type:
payload["matchType"] = self.match_type payload["matchType"] = self.match_type
if self.sort:
payload["sort"] = self.sort
if self.filters and len(self.filters) > 0: if self.filters and len(self.filters) > 0:
for i, _filter in enumerate(self.filters): for i, _filter in enumerate(self.filters):
payload["filter" + str(i)] = _filter payload["filter" + str(i)] = _filter

View File

@ -151,3 +151,24 @@ def check_match_type(match_type: Optional[str], url: str) -> bool:
raise WaybackError(exc_message) raise WaybackError(exc_message)
return True 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