@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)
)
+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."""
+++ /dev/null
-#!/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
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:
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")