Skip to content

Commit

Permalink
Add support for pagination on the list_projects command
Browse files Browse the repository at this point in the history
  • Loading branch information
why-not-try-calmer authored and suricactus committed Jul 11, 2023
1 parent f398010 commit 8502e1b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
22 changes: 18 additions & 4 deletions src/qfieldcloud_sdk/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import platform
import sys
from enum import Enum
from typing import Any, Dict, List

import click

Expand Down Expand Up @@ -159,20 +160,33 @@ def logout(ctx):


@cli.command()
@click.option(
"-off",
"--offset",
default=None,
is_flag=False,
help="Offsets the given number of projects in the paginated JSON response",
)
@click.option(
"-l",
"--limit",
default=None,
is_flag=False,
help="Limits the number of projects to return in the paginated JSON response",
)
@click.option(
"--include-public/--no-public",
default=False,
is_flag=True,
help="Includes the public project in the list. Default: False",
)
@click.pass_context
def list_projects(ctx, include_public):
def list_projects(ctx, **opts):
"""List QFieldCloud projects."""

log("Listing projects…")

projects = ctx.obj["client"].list_projects(
include_public=include_public,
)
projects: List[Dict[str, Any]] = ctx.obj["client"].list_projects(**opts)

if ctx.obj["format_json"]:
print_json(projects)
Expand Down
30 changes: 19 additions & 11 deletions src/qfieldcloud_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def __init__(
"""Prepares a new client.
If the `url` is not provided, uses `QFIELDCLOUD_URL` from the environment.
If the `token` is not provided, uses `QFIELDCLOUD_TOKEN` from the environment."""
If the `token` is not provided, uses `QFIELDCLOUD_TOKEN` from the environment.
"""
self.url = url or os.environ.get("QFIELDCLOUD_URL", None)
self.token = token or os.environ.get("QFIELDCLOUD_TOKEN", None)
self.verify_ssl = verify_ssl
Expand Down Expand Up @@ -117,16 +118,23 @@ def logout(self) -> None:
return resp.json()

def list_projects(
self, username: Optional[str] = None, include_public: Optional[bool] = False
) -> Dict:
"""Lists the project of a given user. If the user is omitted, it fallbacks to the currently logged in user"""
resp = self._request(
"GET",
"projects",
params={
"include-public": "1" if include_public else "0",
},
)
self,
username: Optional[str] = None,
include_public: Optional[bool] = False,
limit: Optional[int] = None,
offset: Optional[int] = None,
) -> List[Dict[str, Any]]:
"""Returns a paginated lists the project of a given user. If the user is omitted, it fallbacks to the currently logged in user"""
params = {
"include-public": int(include_public),
}

if limit:
params["limit"] = limit
if offset:
params["offset"] = offset

resp = self._request("GET", "projects", params=params)

return resp.json()

Expand Down

0 comments on commit 8502e1b

Please sign in to comment.