manjaro / mdd

Manjaro Data Donor - WIP
MIT License
12 stars 3 forks source link

Refactor submission into a function #24

Open fhdk opened 6 days ago

fhdk commented 6 days ago

This will allow for calling the submit function from a GUI.

diff --git a/mdd.py b/mdd.py
index cb9f3c1..63eaf22 100644
--- a/mdd.py
+++ b/mdd.py
@@ -24,6 +24,22 @@ from dateutil import parser as date_parser
 inxi = None

+def http_post_info(sys_info) -> bool:
+    try:
+        response = requests.post(
+            "https://metrics-api.manjaro.org/send",
+            json=sys_info,
+            headers={"Content-Type": "application/json"},
+            timeout=2,
+        )
+
+        response.raise_for_status()
+        return True
+    except Exception as e:
+        logging.error(f"submitting telemetry: {e}")
+        return False
+
+
 def json_beaut(input, sort_keys=False):
     return json.dumps(input, indent=4, sort_keys=sort_keys)

@@ -826,21 +842,11 @@ def main():
         print("Note: Skipping data submission because of dry run.")
         return

-    try:
-        response = requests.post(
-            "https://metrics-api.manjaro.org/send",
-            json=data,
-            headers={"Content-Type": "application/json"},
-            timeout=2,
-        )
-
-        response.raise_for_status()
-    except Exception as e:
-        logging.error(f"submitting telemetry: {e}")
+    if http_post_info(data):
+        print("Succesful sent at", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
+    else:
         exit(1)

-    print("Succesful sent at", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
-

 if __name__ == "__main__":
     main()

refactor-submission-to-function.txt