Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pagination parameters to the projects endpoint (2/3) #36

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Install pipenv
run: pip install pipenv
cache: 'pip'
- name: Check code formatting
run: |
pipenv install pre_commit
pipenv run python -m pre_commit run --all-files
pip install pre_commit
python -m pre_commit run --all-files
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ repos:

# Sort imports
- repo: https://github.com/pycqa/isort
rev: "5.7.0"
rev: "5.12.0"
hooks:
- id: isort
args: ["--profile", "black"]

# Black formatting
- repo: https://github.com/psf/black
rev: 22.3.0
rev: "22.3.0"
hooks:
- id: black

# Lint files
- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/pycqa/flake8
rev: "3.9.0"
hooks:
- id: flake8
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