Skip to content

Commit 5c4882d

Browse files
committed
Make use of paginated API by default on list-projects and list-jobs
1 parent 1ac0874 commit 5c4882d

5 files changed

Lines changed: 156 additions & 140 deletions

File tree

qfieldcloud_sdk/cli.py

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import collections
22
import platform
33
from enum import Enum
4-
from typing import Any, Dict, List
4+
from typing import Any, Dict, List, Optional
55

66
import click
77

@@ -33,6 +33,26 @@ def list_commands(self, ctx):
3333
return self.commands
3434

3535

36+
def paginated(command):
37+
command = click.option(
38+
"-o",
39+
"--offset",
40+
type=int,
41+
default=None,
42+
is_flag=False,
43+
help="Offsets the given number of records in the paginated JSON response.",
44+
)(command)
45+
command = click.option(
46+
"-l",
47+
"--limit",
48+
type=int,
49+
default=None,
50+
is_flag=False,
51+
help="Limits the number of records to return in the paginated JSON response.",
52+
)(command)
53+
return command
54+
55+
3656
@click.group(cls=OrderedGroup)
3757
@click.option(
3858
"-U",
@@ -149,35 +169,23 @@ def logout(ctx):
149169

150170

151171
@cli.command()
152-
@click.option(
153-
"-o",
154-
"--offset",
155-
type=int,
156-
default=None,
157-
is_flag=False,
158-
help="Offsets the given number of projects in the paginated JSON response",
159-
)
160-
@click.option(
161-
"-l",
162-
"--limit",
163-
type=int,
164-
default=None,
165-
is_flag=False,
166-
help="Limits the number of projects to return in the paginated JSON response",
167-
)
172+
@paginated
168173
@click.option(
169174
"--include-public/--no-public",
170175
default=False,
171176
is_flag=True,
172177
help="Includes the public project in the list. Default: False",
173178
)
174179
@click.pass_context
175-
def list_projects(ctx, **opts):
180+
def list_projects(ctx, include_public, **opts):
176181
"""List QFieldCloud projects."""
177182

178183
log("Listing projects…")
179184

180-
projects: List[Dict[str, Any]] = ctx.obj["client"].list_projects(**opts)
185+
projects: List[Dict[str, Any]] = ctx.obj["client"].list_projects(
186+
include_public,
187+
sdk.Pagination(**opts),
188+
)
181189

182190
if ctx.obj["format_json"]:
183191
print_json(projects)
@@ -381,29 +389,18 @@ def delete_files(ctx, project_id, paths, throw_on_error):
381389
type=sdk.JobTypes,
382390
help="Job type. One of package, delta_apply or process_projectfile.",
383391
)
384-
@click.option(
385-
"-o",
386-
"--offset",
387-
type=int,
388-
default=None,
389-
is_flag=False,
390-
help="Offsets the given number of projects in the paginated JSON response",
391-
)
392-
@click.option(
393-
"-l",
394-
"--limit",
395-
type=int,
396-
default=None,
397-
is_flag=False,
398-
help="Limits the number of projects to return in the paginated JSON response",
399-
)
392+
@paginated
400393
@click.pass_context
401-
def list_jobs(ctx, project_id, **opts):
394+
def list_jobs(ctx, project_id, job_type: Optional[sdk.JobTypes], **opts):
402395
"""List project jobs."""
403396

404397
log(f'Listing project "{project_id}" jobs…')
405398

406-
jobs: List[Dict[Any]] = ctx.obj["client"].list_jobs(project_id, **opts)
399+
jobs: List[Dict[Any]] = ctx.obj["client"].list_jobs(
400+
project_id,
401+
job_type,
402+
sdk.Pagination(**opts),
403+
)
407404

408405
if ctx.obj["format_json"]:
409406
print_json(jobs)

qfieldcloud_sdk/interfaces.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,28 @@ def __getitem__(self, k: str) -> Any:
1515
class QfcMockResponse(requests.Response):
1616
def __init__(self, **kwargs):
1717
self.request_kwargs = kwargs
18+
self.url = kwargs["url"]
1819
self.limit = kwargs.get("limit", 5)
1920
self.total = self.limit * 2
2021
self.headers = {
2122
"X-Total-Count": self.total,
22-
"X-Next-Page": "next_url",
23-
"X-Previous-Page": "previous_url",
2423
}
2524

25+
limit = kwargs["params"].get("limit")
26+
offset = kwargs["params"].get("offset", 0)
27+
prev_url = None
28+
next_url = None
29+
if limit:
30+
if offset == 0:
31+
prev_url = None
32+
next_url = f"{self.url}?limit={limit}&offset={limit}"
33+
else:
34+
prev_url = f"{self.url}?limit={limit}&offset=0"
35+
next_url = None
36+
37+
self.headers["X-Previous-Page"] = prev_url
38+
self.headers["X-Next-Page"] = next_url
39+
2640
def json(self) -> Union[QfcMockItem, List[QfcMockItem]]:
2741
if self.request_kwargs["method"] == "GET":
2842
return [QfcMockItem(id=n) for n in range(self.total)]

0 commit comments

Comments
 (0)