From 031c8efbe6cb09aa42e01d82d59ff4ef7a9d0b9a Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Thu, 21 Oct 2021 16:56:53 +0200 Subject: [PATCH] Implement pagination for package list The simplest way I can think of. Currently we only call the request function once, so we can only get the first page. Before calling it multiple times, we should check if there are more pages by checking if the request URL is an empty string. Contributes to issue CURA-8556. --- plugins/Marketplace/PackageList.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/Marketplace/PackageList.py b/plugins/Marketplace/PackageList.py index 37755498f8..b93cc3e083 100644 --- a/plugins/Marketplace/PackageList.py +++ b/plugins/Marketplace/PackageList.py @@ -33,12 +33,13 @@ class PackageList(ListModel): self._is_loading = True self._scope = JsonDecoratorScope(UltimakerCloudScope(CuraApplication.getInstance())) + self._request_url = f"{Marketplace.PACKAGES_URL}?limit={self.ITEMS_PER_PAGE}" self.addRoleName(self.PackageRole, "package") - self.requestFirst() + self.request() - def requestFirst(self) -> None: + def request(self) -> None: """ Make a request for the first paginated page of packages. @@ -48,7 +49,7 @@ class PackageList(ListModel): http = HttpRequestManager.getInstance() http.get( - Marketplace.PACKAGES_URL, + self._request_url, scope = self._scope, callback = self._parseResponse, error_callback = self._onError @@ -78,13 +79,15 @@ class PackageList(ListModel): :param reply: A reply containing information about a number of packages. """ response_data = HttpRequestManager.readJSON(reply) - if "data" not in response_data: + if "data" not in response_data or "links" not in response_data: return # TODO: Handle invalid response. for package_data in response_data["data"]: package = PackageModel(package_data, parent = self) self.appendItem({"package": package}) # Add it to this list model. + self._request_url = response_data["links"].get("next", "") # Use empty string to signify that there is no next page. + def _onError(self, reply: "QNetworkReply", error: Optional["QNetworkReply.NetworkError"]) -> None: """ Handles networking and server errors when requesting the list of packages.