"""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)))
@author: shane
"""
+import sqlite3
+
import requests
URL_API = "https://api.nutra.tk"
):
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))
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
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