Skip to content

Commit

Permalink
Extract next link query param creation into EntriesCollection method. (
Browse files Browse the repository at this point in the history
  • Loading branch information
markus1978 committed Mar 16, 2023
1 parent 01101a4 commit d5e7c15
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions optimade/server/entry_collections/entry_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,27 @@ def parse_sort_params(self, sort_params: str) -> Iterable[Tuple[str, int]]:
]

return sort_spec

def get_next_query_params(
self,
params: EntryListingQueryParams,
results: Union[None, List[EntryResource], EntryResource, List[Dict]],
) -> Dict[str, List[str]]:
"""Provides url query pagination parameters that will be used in the next
link.
Arguments:
results: The results produced by find.
params: The parsed request params produced by handle_query_params.
Returns:
A dictionary with the necessary query parameters.
"""
query: Dict[str, List[str]] = dict()
if isinstance(results, list):
query["page_offset"] = [
str(getattr(params, "page_offset", 0) + len(results))
]

return query
4 changes: 2 additions & 2 deletions optimade/server/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ def get_entries(
if more_data_available:
# Deduce the `next` link from the current request
query = urllib.parse.parse_qs(request.url.query)
if isinstance(results, list):
query["page_offset"] = int(query.get("page_offset", [0])[0]) + len(results) # type: ignore[assignment,list-item]
query.update(collection.get_next_query_params(params, results))

urlencoded = urllib.parse.urlencode(query, doseq=True)
base_url = get_base_url(request.url)

Expand Down

0 comments on commit d5e7c15

Please sign in to comment.