]> Nutra Git (v1) - nutratech/cli.git/commitdiff
bug submission MVP. wip configparser
authorShane Jaroch <chown_tee@proton.me>
Thu, 15 Feb 2024 01:08:17 +0000 (20:08 -0500)
committerShane Jaroch <chown_tee@proton.me>
Thu, 15 Feb 2024 01:08:17 +0000 (20:08 -0500)
ntclient/persistence/__init__.py
ntclient/services/api/__init__.py
ntclient/services/api/mirrorcache.py [deleted file]
ntclient/services/bugs.py

index 8422701f49e545270a111c069b65300601aa5a3b..48f780c6f3c6bee766459c8b985cb3d8f0fb8d68 100644 (file)
@@ -8,12 +8,17 @@ Created on Sat Mar 23 13:09:07 2019
 
 @author: shane
 """
+import configparser
 import os
 
 from ntclient import NUTRA_HOME
 
 # TODO: create and maintain prefs.json file?  See if there's a library for that, lol
 
-PREFS_JSON = os.path.join(NUTRA_HOME, "prefs.json")
+PREFS_FILE = os.path.join(NUTRA_HOME, "prefs.ini")
 
-# if
+if not os.path.exists(PREFS_FILE):
+    print("INFO: Generating prefs.ini file")
+    config = configparser.ConfigParser()
+    with open(PREFS_FILE, "w", encoding="utf-8") as _prefs_file:
+        config.write(_prefs_file)
index 5571e405ca142f71a853cfd36db156fbf937f1eb..02fb4cde061ecfd8a1f0fc188619d32f9f4e4e15 100644 (file)
@@ -19,14 +19,33 @@ URLS_API = (
 )
 
 
+def cache_mirrors() -> str:
+    """Cache mirrors"""
+    for mirror in URLS_API:
+        try:
+            _res = requests.get(
+                mirror,
+                timeout=(REQUEST_CONNECT_TIMEOUT, REQUEST_READ_TIMEOUT),
+                verify=mirror.startswith("https://"),
+            )
+
+            _res.raise_for_status()
+            # TODO: save in persistence config.ini
+            print(f"INFO: mirror SUCCESS '{mirror}'")
+            return mirror
+        except requests.exceptions.ConnectionError:
+            print(f"WARN: mirror FAILURE '{mirror}'")
+
+    return str()
+
+
 class ApiClient:
     """Client for connecting to the remote server/API."""
 
-    def __init__(
-        self,
-        host: str = URLS_API[0],
-    ):
-        self.host = host
+    def __init__(self) -> None:
+        self.host = cache_mirrors()
+        if not self.host:
+            raise ConnectionError("Cannot find suitable API host!")
 
     def get(self, path: str) -> requests.Response:
         """Get data from the API."""
diff --git a/ntclient/services/api/mirrorcache.py b/ntclient/services/api/mirrorcache.py
deleted file mode 100644 (file)
index 560484e..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
-"""
-Created on Wed Feb 14 18:58:12 2024
-
-@author: shane
-"""
-
-import requests
-
-from ntclient.services.api import (
-    REQUEST_CONNECT_TIMEOUT,
-    REQUEST_READ_TIMEOUT,
-    URLS_API,
-)
-
-
-def cache_mirrors() -> bool:
-    """Cache mirrors"""
-    for mirror in URLS_API:
-        try:
-            _res = requests.get(
-                mirror,
-                timeout=(REQUEST_CONNECT_TIMEOUT, REQUEST_READ_TIMEOUT),
-                verify=mirror.startswith("https://"),
-            )
-
-            _res.raise_for_status()
-            print(f"INFO: mirror '{mirror}' SUCCEEDED!  Saving it.")
-            return True
-        except requests.exceptions.ConnectionError:
-            print(f"INFO: mirror '{mirror}' failed")
-
-    return False
index 4266518b4907fadc98f3a6c154533ca41846d0c2..3a8cd6ab883c7a9bbaaa1b46a714f455a06ac001 100644 (file)
@@ -11,7 +11,6 @@ import traceback
 
 import ntclient.services.api
 from ntclient.persistence.sql.nt import sql as sql_nt
-from ntclient.services.api.mirrorcache import cache_mirrors
 
 
 def insert(args: list, exception: Exception) -> None:
@@ -53,11 +52,6 @@ def list_bugs() -> list:
 
 def submit_bugs() -> int:
     """Submit bug reports to developer, return n_submitted."""
-    # Probe mirrors, cache best working one
-    is_mirror_alive = cache_mirrors()
-    if not is_mirror_alive:
-        print("ERROR: we couldn't find an active mirror, can't submit bugs.")
-        return -1
 
     # Gather bugs for submission
     sql_bugs = sql_nt("SELECT * FROM bug WHERE submitted = 0")