diff options
author | Rasmus Luha <rasmus.luha@ut.ee> | 2025-04-05 22:59:24 +0300 |
---|---|---|
committer | Rasmus Luha <rasmus.luha@ut.ee> | 2025-04-05 22:59:24 +0300 |
commit | 31de311a17c9f83d330a499ec0b5ae5f672bbb65 (patch) | |
tree | 2b295c56daf5bc58466b1e1b88053729a8114ef9 /common/core.py | |
parent | ee1498a3ccacfe9c4cfbe98b9576795d6d06521a (diff) |
getting and checking api_url done
Diffstat (limited to 'common/core.py')
-rw-r--r-- | common/core.py | 40 |
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)") + |