From: Shane Jaroch Date: Thu, 15 Feb 2024 01:08:17 +0000 (-0500) Subject: bug submission MVP. wip configparser X-Git-Tag: v0.2.8.dev0~37 X-Git-Url: https://git.nutra.tk/v1?a=commitdiff_plain;h=1b9dca7d7ac07a381a213ad1157000a8bd2d2ec4;p=nutratech%2Fcli.git bug submission MVP. wip configparser --- diff --git a/ntclient/persistence/__init__.py b/ntclient/persistence/__init__.py index 8422701..48f780c 100644 --- a/ntclient/persistence/__init__.py +++ b/ntclient/persistence/__init__.py @@ -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) diff --git a/ntclient/services/api/__init__.py b/ntclient/services/api/__init__.py index 5571e40..02fb4cd 100644 --- a/ntclient/services/api/__init__.py +++ b/ntclient/services/api/__init__.py @@ -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 index 560484e..0000000 --- a/ntclient/services/api/mirrorcache.py +++ /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 diff --git a/ntclient/services/bugs.py b/ntclient/services/bugs.py index 4266518..3a8cd6a 100644 --- a/ntclient/services/bugs.py +++ b/ntclient/services/bugs.py @@ -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")