Skip to content

Commit 8502e1b

Browse files
why-not-try-calmersuricactus
authored andcommitted
Add support for pagination on the list_projects command
1 parent f398010 commit 8502e1b

2 files changed

Lines changed: 37 additions & 15 deletions

File tree

src/qfieldcloud_sdk/cli.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import platform
66
import sys
77
from enum import Enum
8+
from typing import Any, Dict, List
89

910
import click
1011

@@ -159,20 +160,33 @@ def logout(ctx):
159160

160161

161162
@cli.command()
163+
@click.option(
164+
"-off",
165+
"--offset",
166+
default=None,
167+
is_flag=False,
168+
help="Offsets the given number of projects in the paginated JSON response",
169+
)
170+
@click.option(
171+
"-l",
172+
"--limit",
173+
default=None,
174+
is_flag=False,
175+
help="Limits the number of projects to return in the paginated JSON response",
176+
)
162177
@click.option(
163178
"--include-public/--no-public",
164179
default=False,
180+
is_flag=True,
165181
help="Includes the public project in the list. Default: False",
166182
)
167183
@click.pass_context
168-
def list_projects(ctx, include_public):
184+
def list_projects(ctx, **opts):
169185
"""List QFieldCloud projects."""
170186

171187
log("Listing projects…")
172188

173-
projects = ctx.obj["client"].list_projects(
174-
include_public=include_public,
175-
)
189+
projects: List[Dict[str, Any]] = ctx.obj["client"].list_projects(**opts)
176190

177191
if ctx.obj["format_json"]:
178192
print_json(projects)

src/qfieldcloud_sdk/sdk.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def __init__(
7171
"""Prepares a new client.
7272
7373
If the `url` is not provided, uses `QFIELDCLOUD_URL` from the environment.
74-
If the `token` is not provided, uses `QFIELDCLOUD_TOKEN` from the environment."""
74+
If the `token` is not provided, uses `QFIELDCLOUD_TOKEN` from the environment.
75+
"""
7576
self.url = url or os.environ.get("QFIELDCLOUD_URL", None)
7677
self.token = token or os.environ.get("QFIELDCLOUD_TOKEN", None)
7778
self.verify_ssl = verify_ssl
@@ -117,16 +118,23 @@ def logout(self) -> None:
117118
return resp.json()
118119

119120
def list_projects(
120-
self, username: Optional[str] = None, include_public: Optional[bool] = False
121-
) -> Dict:
122-
"""Lists the project of a given user. If the user is omitted, it fallbacks to the currently logged in user"""
123-
resp = self._request(
124-
"GET",
125-
"projects",
126-
params={
127-
"include-public": "1" if include_public else "0",
128-
},
129-
)
121+
self,
122+
username: Optional[str] = None,
123+
include_public: Optional[bool] = False,
124+
limit: Optional[int] = None,
125+
offset: Optional[int] = None,
126+
) -> List[Dict[str, Any]]:
127+
"""Returns a paginated lists the project of a given user. If the user is omitted, it fallbacks to the currently logged in user"""
128+
params = {
129+
"include-public": int(include_public),
130+
}
131+
132+
if limit:
133+
params["limit"] = limit
134+
if offset:
135+
params["offset"] = offset
136+
137+
resp = self._request("GET", "projects", params=params)
130138

131139
return resp.json()
132140

0 commit comments

Comments
 (0)