]> Nutra Git (v1) - nutratech/cli.git/commitdiff
wip bug report
authorShane Jaroch <chown_tee@proton.me>
Wed, 14 Feb 2024 02:14:15 +0000 (21:14 -0500)
committerShane Jaroch <chown_tee@proton.me>
Wed, 14 Feb 2024 02:14:15 +0000 (21:14 -0500)
ntclient/services/api/__init__.py
ntclient/services/api/funcs.py [deleted file]
ntclient/services/bugs.py
requirements-lint.txt
requirements.txt

index 83fe132786bb84332f65c0f754d88da9e7800bb4..c75d95218bcac170e3535a3ea85c4996d262daab 100644 (file)
@@ -5,3 +5,42 @@ Created on Tue Feb 13 14:28:20 2024
 
 @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))
diff --git a/ntclient/services/api/funcs.py b/ntclient/services/api/funcs.py
deleted file mode 100644 (file)
index c955dd0..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/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...")
index 0b32a1164438e7407660c57206eb2c72b8b1c4b9..7f2bd39c0947eabc36cf2c1da8b6cc98be544693 100644 (file)
@@ -9,7 +9,7 @@ import os
 import sqlite3
 import traceback
 
-import ntclient.services.api.funcs
+import ntclient.services.api
 from ntclient.persistence.sql.nt import sql as sql_nt
 
 
@@ -46,12 +46,14 @@ INSERT INTO bug
 
 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")
index cf4d8709657d9be220cde0e7cb80c0b38f86bf60..71f17b8dc65dff5e76b66f61de11081007f63e4c 100644 (file)
@@ -6,5 +6,6 @@ mypy==1.8.0
 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
index 6a6c215df12038634d09608d9f7619a1ec0c8a4f..c2010f586e12d83b33b5c909313814d8e03e0cc9 100644 (file)
@@ -1,4 +1,5 @@
 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