From: Shane Jaroch Date: Wed, 14 Feb 2024 20:57:47 +0000 (-0500) Subject: keep chugging on bugs X-Git-Tag: v0.2.8.dev0~41 X-Git-Url: https://git.nutra.tk/v2?a=commitdiff_plain;h=7be43ba7641ba7bcefa106b4b63152a1aba5528d;p=nutratech%2Fcli.git keep chugging on bugs --- diff --git a/ntclient/argparser/funcs.py b/ntclient/argparser/funcs.py index 9b0b77e..d63a2f1 100644 --- a/ntclient/argparser/funcs.py +++ b/ntclient/argparser/funcs.py @@ -342,12 +342,16 @@ def bugs_list(args: argparse.Namespace) -> tuple: """List bug reports that have een saved""" _bugs_list = ntclient.services.bugs.list_bugs() - print(f"You have {len(_bugs_list)} unique bugs to report/submit! Good job.") + print(f"You have: {len(_bugs_list)} total bugs amassed in your journey.") + print( + f"Of these, {len([x for x in _bugs_list if not bool(x[-1])])} " + f"require submission/reporting." + ) print() for bug in _bugs_list: # Skip submitted bugs by default - if bug[-1] != 0 and not args.all: + if bool(bug[-1]) and not args.all: continue # Print all (except noisy stacktrace) print(", ".join(str(x) for x in bug if "\n" not in str(x))) diff --git a/ntclient/services/api/__init__.py b/ntclient/services/api/__init__.py index 7135c9d..0b1e89a 100644 --- a/ntclient/services/api/__init__.py +++ b/ntclient/services/api/__init__.py @@ -5,6 +5,8 @@ Created on Tue Feb 13 14:28:20 2024 @author: shane """ +import sqlite3 + import requests URL_API = "https://api.nutra.tk" @@ -27,26 +29,26 @@ class ApiClient: ): self.host = host - def get(self, path: str) -> dict: + def get(self, path: str) -> requests.Response: """Get data from the API.""" - req = requests.get( + _res = requests.get( f"{self.host}/{path}", timeout=(REQUEST_CONNECT_TIMEOUT, REQUEST_READ_TIMEOUT), ) - req.raise_for_status() - return dict(req.json()) + _res.raise_for_status() + return _res - def post(self, path: str, data: dict) -> dict: + def post(self, path: str, data: dict) -> requests.Response: """Post data to the API.""" - req = requests.post( + _res = requests.post( f"{self.host}/{path}", json=data, timeout=(REQUEST_CONNECT_TIMEOUT, REQUEST_READ_TIMEOUT), ) - req.raise_for_status() - return dict(req.json()) + _res.raise_for_status() + return _res # TODO: move this outside class; support with host iteration helper method - def post_bug(self, bug: tuple) -> None: + def post_bug(self, bug: sqlite3.Row) -> requests.Response: """Post a bug report to the developer.""" - self.post("bug", dict(bug)) + return self.post("bug", dict(bug)) diff --git a/ntclient/services/bugs.py b/ntclient/services/bugs.py index 75e047d..28d1e84 100644 --- a/ntclient/services/bugs.py +++ b/ntclient/services/bugs.py @@ -46,7 +46,7 @@ INSERT INTO bug def list_bugs() -> list: """List all bugs.""" - sql_bugs = sql_nt("SELECT * FROM bug WHERE submitted = 0") + sql_bugs = sql_nt("SELECT * FROM bug") return sql_bugs @@ -58,12 +58,19 @@ def submit_bugs() -> int: n_submitted = 0 print(f"submitting {len(sql_bugs)} bug reports...") print("_" * len(sql_bugs)) + for bug in sql_bugs: + _res = api_client.post_bug(bug) + + # Differentially store unique vs. duplicate bugs (someone else submitted) + if _res.status_code == 201: + sql_nt("UPDATE bug SET submitted = 1 WHERE id = %s", bug.id) + elif _res.status_code in {200, 204}: + sql_nt("UPDATE bug SET submitted = 2 WHERE id = %s", bug.id) + print(".", end="", flush=True) - api_client.post_bug(bug) n_submitted += 1 + print() - # 1 / 0 # force exception - # raise Exception("submitting bug reports failed") return n_submitted