summaryrefslogtreecommitdiff
path: root/common/core.py
blob: 44248be9719247a033920531bb70a42c962fc31d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)")