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 | |
parent | ee1498a3ccacfe9c4cfbe98b9576795d6d06521a (diff) |
getting and checking api_url done
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | common/core.py | 40 | ||||
-rw-r--r-- | main.py | 2 | ||||
-rw-r--r-- | modules/nifi/core.py | 45 | ||||
-rw-r--r-- | modules/telegraf/core.py | 2 |
6 files changed, 71 insertions, 27 deletions
@@ -1 +1,2 @@ venv/ +__pycache__/ @@ -0,0 +1,8 @@ +TODOs + +API urli kontrollimine + - lihtsalt kontrollimine + - autentimine + + +https://api.open-meteo.com/v1/forecast?latitude=58.38&longitude=26.72¤t_weather=true 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)") + @@ -18,7 +18,7 @@ def main(): platform = AVAILABLE_PLATFORMS.get(plat_choice) if not platform: - print("Ebaõnnestunud valik ...") + print("Ebaõnnestunud valik, sulgen rakenduse...") return name, module = platform diff --git a/modules/nifi/core.py b/modules/nifi/core.py index 82f28b2..bdac035 100644 --- a/modules/nifi/core.py +++ b/modules/nifi/core.py @@ -1,36 +1,31 @@ from pyfiglet import figlet_format from rich.console import Console +from common import core as common -import requests +import sys def introduction(): console = Console() ascii_art = figlet_format("Nifi") console.print(ascii_art, style="cyan") - - print("Valisid Nifi Platformi!") - - - -def api_url_validness_check(url): - try: - response = requests.get(url) - response.raise_for_status() - response.json() - return True - except (requests.exceptions.RequestException, ValueError) as e: - return False - - + print("Valisid Nifi Platformi!\n") def build_pipeline(): - api_url = input("Palun sisesta andmete API URL: ").strip() - - if (input("Kas API vajab ka autentimist?(Jah/Ei): ").strip().lower() == 'jah'): - print("TODO") - - if(api_url_validness_check(api_url)): - print("Good") - else: - print("Bad") + while True: + api_url = input("Palun sisesta andmete API URL: ").strip() + username = "placeholder" + passwd = "placeholder" + + needs_auth = common.ask_binary_input(prompt="Kas API vajab ka kasutajaga autentimist?(jah/ei): ").strip().lower() == 'jah' + if needs_auth: + username=input("Sisesta kasutajanimi: ") + passwd=input("Sisesta parool: ") + + if common.is_app_url_correct(api_url,needs_auth,username,passwd): + break # Exit loop if URL is correct + else: + choice = common.ask_binary_input(prompt="\nKas soovid URL-i (m)uuta URL-i või (v)äljuda?(m/v): ",valikud=["m","v"]).strip().lower() + if choice == 'v': + print("Väljun programmist.") + sys.exit() diff --git a/modules/telegraf/core.py b/modules/telegraf/core.py index e1bdb70..1d49917 100644 --- a/modules/telegraf/core.py +++ b/modules/telegraf/core.py @@ -7,4 +7,4 @@ def introduction(): ascii_art = figlet_format("Telegraf") console.print(ascii_art, style="cyan") - print("Valisid Telegraf Platformi!") + print("Valisid Telegraf Platformi!\n") |