summaryrefslogtreecommitdiff
path: root/common/core.py
diff options
context:
space:
mode:
Diffstat (limited to 'common/core.py')
-rw-r--r--common/core.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/common/core.py b/common/core.py
new file mode 100644
index 0000000..44248be
--- /dev/null
+++ b/common/core.py
@@ -0,0 +1,40 @@
+import requests
+import json
+from requests.auth import HTTPBasicAuth
+
+
+def ask_binary_input(prompt="Kas jah või ei?: ", valikud=["jah","ei"]):
+ while True:
+ answer = input(prompt).strip().lower()
+ if answer in valikud:
+ return answer
+ print(f"Ebakorretne sisend.Palun vasta kas '{valikud[0]}' või '{valikud[1]}'")
+
+def is_app_url_correct(api_url, needs_auth, username,passwd):
+ print("Teostan API kutset...\n")
+ try:
+ if needs_auth:
+ response = requests.get(api_url, auth=HTTPBasicAuth(username, passwd))
+ else:
+ response = requests.get(api_url)
+
+ response.raise_for_status() ## Check if staus code is 2xx
+ data = response.json()
+ print(json.dumps(data, indent=2))
+ return True
+
+ except requests.exceptions.RequestException as e:
+ print(f"HTTP error: {e}")
+ return False
+ except ValueError:
+ print("andmeallikas ei tagasta vallidset JSON kuju...")
+ return False
+ except Exception as e:
+ print(f"API kutsel tekkis viga: {e}")
+ return False
+
+
+##TODO
+def add_api_authentication():
+ print("Adding api authentication ... (TODO)")
+