Skip to content

Commit

Permalink
Fix typings
Browse files Browse the repository at this point in the history
  • Loading branch information
suricactus committed May 15, 2024
1 parent 8a675a3 commit 15b5db6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
4 changes: 2 additions & 2 deletions qfieldcloud_sdk/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def logout(ctx):
help="Includes the public project in the list. Default: False",
)
@click.pass_context
def list_projects(ctx, include_public, **opts):
def list_projects(ctx, include_public: bool, **opts) -> None:
"""List QFieldCloud projects."""

log("Listing projects…")
Expand Down Expand Up @@ -396,7 +396,7 @@ def list_jobs(ctx, project_id, job_type: Optional[sdk.JobTypes], **opts):

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

jobs: List[Dict[Any]] = ctx.obj["client"].list_jobs(
jobs: List[Dict] = ctx.obj["client"].list_jobs(
project_id,
job_type,
sdk.Pagination(**opts),
Expand Down
6 changes: 3 additions & 3 deletions qfieldcloud_sdk/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Any, List, Union
from typing import Any

import requests

Expand Down Expand Up @@ -37,7 +37,7 @@ def __init__(self, **kwargs):
self.headers["X-Previous-Page"] = prev_url
self.headers["X-Next-Page"] = next_url

def json(self) -> Union[QfcMockItem, List[QfcMockItem]]:
def json(self):
if self.request_kwargs["method"] == "GET":
return [QfcMockItem(id=n) for n in range(self.total)]
else:
Expand Down Expand Up @@ -69,7 +69,7 @@ def __init__(self, response: requests.Response, *args):
except Exception:
json_content = ""

self.reason = f'Requested "{response.url}" and got "{response.status_code} {response.reason}":\n{json_content or response.content}'
self.reason = f'Requested "{response.url}" and got "{response.status_code} {response.reason}":\n{json_content or response.content.decode()}'

def __str__(self) -> str:
return self.reason
Expand Down
29 changes: 17 additions & 12 deletions qfieldcloud_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from enum import Enum
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Union
from typing import Any, Callable, Dict, List, Optional, Union, cast
from urllib import parse as urlparse

import requests
Expand Down Expand Up @@ -136,13 +136,13 @@ def list_projects(
their own and optionally the public ones.
"""
params = {
"include-public": int(include_public),
"include-public": str(int(include_public)), # type: ignore
}

payload = self._request_json(
"GET", "projects", params=params, pagination=pagination
)
return payload
return cast(List, payload)

def list_remote_files(
self, project_id: str, skip_metadata: bool = True
Expand Down Expand Up @@ -337,7 +337,7 @@ def list_jobs(
"""
Returns a paginated lists of jobs accessible to the user.
"""
return self._request_json(
payload = self._request_json(
"GET",
"jobs/",
{
Expand All @@ -346,6 +346,7 @@ def list_jobs(
},
pagination=pagination,
)
return cast(List, payload)

def job_trigger(
self, project_id: str, job_type: JobTypes, force: bool = False
Expand Down Expand Up @@ -580,7 +581,7 @@ def download_file(
remote_filename: Path,
show_progress: bool,
remote_etag: str = None,
) -> requests.Response:
) -> Optional[requests.Response]:
"""Download a single project file.
Args:
Expand All @@ -595,7 +596,7 @@ def download_file(
NotImplementedError: Raised if unknown `download_type` is passed
Returns:
requests.Response: the response object
requests.Response | None: the response object
"""

if remote_etag and local_filename.exists():
Expand All @@ -608,7 +609,7 @@ def download_file(
logger.info(
f'Skipping download of "{remote_filename}" because it is already present locally'
)
return
return None

if download_type == FileTransferType.PROJECT:
url = f"files/{project_id}/{remote_filename}"
Expand Down Expand Up @@ -692,7 +693,7 @@ def _request_json(
allow_redirects=None,
pagination: Pagination = Pagination(),
) -> Union[List, Dict]:
result = None
result: Optional[Union[List, Dict]] = None
is_empty_pagination = pagination.is_empty

while True:
Expand All @@ -712,11 +713,15 @@ def _request_json(
payload = resp.json()

if isinstance(payload, list):
result = cast(List, result)

if result:
result += payload
else:
result = payload
elif isinstance(payload, dict):
result = cast(Dict, result)

if result:
result = {**result, **payload}
else:
Expand All @@ -735,8 +740,8 @@ def _request_json(

query_params = urlparse.parse_qs(urlparse.urlparse(next_url).query)
pagination = Pagination(
limit=query_params["limit"],
offset=query_params["offset"],
limit=cast(int, query_params["limit"]),
offset=cast(int, query_params["offset"]),
)

return result
Expand Down Expand Up @@ -782,8 +787,8 @@ def _request(
offset = pagination.offset or 0
params = {
**params,
"limit": limit,
"offset": offset,
"limit": str(limit),
"offset": str(offset),
}

request_params = {
Expand Down

0 comments on commit 15b5db6

Please sign in to comment.