From 31de311a17c9f83d330a499ec0b5ae5f672bbb65 Mon Sep 17 00:00:00 2001 From: Rasmus Luha Date: Sat, 5 Apr 2025 22:59:24 +0300 Subject: getting and checking api_url done --- common/core.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 common/core.py (limited to 'common/core.py') 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)") + -- cgit v1.2.3