@author: shane
"""
+import requests
+
+URL_API = "https://api.nutra.tk"
+REQUEST_READ_TIMEOUT = 18
+REQUEST_CONNECT_TIMEOUT = 5
+
+
+class ApiClient:
+ """Client for connecting to the remote server/API."""
+
+ def __init__(
+ self,
+ host: str = URL_API,
+ ):
+ self.host = host
+
+ def get(self, path: str) -> dict:
+ """Get data from the API."""
+ req = requests.get(
+ f"{self.host}/{path}",
+ timeout=(REQUEST_CONNECT_TIMEOUT, REQUEST_READ_TIMEOUT),
+ )
+ req.raise_for_status()
+ return dict(req.json())
+
+ def post(self, path: str, data: dict) -> dict:
+ """Post data to the API."""
+ req = requests.post(
+ f"{self.host}/{path}",
+ json=data,
+ timeout=(REQUEST_CONNECT_TIMEOUT, REQUEST_READ_TIMEOUT),
+ )
+ req.raise_for_status()
+ return dict(req.json())
+
+ def post_bug(self, bug: tuple) -> None:
+ """Post a bug report to the developer."""
+ print("posting bug report...")
+ self.post("bug", dict(bug))
+++ /dev/null
-#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
-"""
-Created on Tue Feb 13 14:28:44 2024
-
-@author: shane
-"""
-
-
-def post_bug(bug: tuple) -> None:
- """Post a bug report to the developer."""
- print("posting bug report...")
import sqlite3
import traceback
-import ntclient.services.api.funcs
+import ntclient.services.api
from ntclient.persistence.sql.nt import sql as sql_nt
def submit() -> int:
"""Submit bug reports to developer, return n_submitted."""
- n_submitted = 0
sql_bugs = sql_nt("SELECT * FROM bug WHERE submitted = 0")
+ api_client = ntclient.services.api.ApiClient()
+
+ n_submitted = 0
print(f"submitting {len(sql_bugs)} bug reports...")
for bug in sql_bugs:
# print(", ".join(str(x) for x in bug))
- ntclient.services.api.funcs.post_bug(bug)
+ api_client.post_bug(bug)
n_submitted += 1
# 1 / 0 # force exception
# raise Exception("submitting bug reports failed")
pylint==3.0.3
types-colorama==0.4.15.12
types-psycopg2==2.9.21.20
+types-requests==2.31.0.20240125
types-setuptools==69.0.0.0
types-tabulate==0.9.0.3
argcomplete>=1.8.2,<=1.12.3
colorama>=0.1.17,<=0.4.1
fuzzywuzzy>=0.3.0
+requests>=2.0.0
tabulate>=0.4.3,<=0.8.9