summaryrefslogtreecommitdiff
path: root/common/core.py
blob: 7fd8cff9257a6eb8438976cbeaefe05dc49a7279 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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 ask_digit_input(max_index):
    while True:
        user_input = input(f"Vali number (0 - {max_index}): ").strip()

        if not user_input.isdigit():
            print("Palun vali korrektne numer.")
            continue # algusesse

        index = int(user_input)

        if 0 <= index <= max_index:
            return int(index)
        else:
            print(f" Number ei kuulu valikusse. Palun vali number vahemikus 0-{max_index}.")


## Checks if api url is correct, if so then returns the json
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 data, True

    except requests.exceptions.RequestException as e:
        print(f"HTTP error: {e}")
        return None, False
    except ValueError:
        print("andmeallikas ei tagasta vallidset JSON kuju...")
        return None, False
    except Exception as e:
        print(f"API kutsel tekkis viga: {e}")
        return None, False


## TODO - add list level support with porcessors etc
## Asks the user json item to extract and returns it as dict item-value pair, where item is name and value json path
def inspect_json_top_level(json_data):
    path = ""

    while True:
        print(json.dumps(json_data, indent=2))
        print("\nVali json võti millest soovid väärtuse andmekonveieriga ekstrakteerida\n")

        keys = list(json_data.keys())

        for index, key in enumerate(keys):
            value = json_data[key]
            value_type = type(value).__name__ ## näitab mis json itemi type'i 
            if isinstance(value, list):
                suggestion = "SplitJson"
            else:
                suggestion = "EvaluateJsonPath"
            print(f"  [{index}] {key} ({value_type})")

        selected_index = ask_digit_input(len(list(json_data.keys())) - 1)
        selected_key = keys[selected_index]
        selected_value = json_data[selected_key]

        # Wrap into new json object
        #extracted = {selected_key: selected_value}

        if isinstance(selected_value, dict) or isinstance(selected_value,list):
            json_data = selected_value
            path += "." + selected_key
            continue
        else:
            print(f"\nValitud võti: '{selected_key}':")
            path += "." + selected_key
            return {selected_key: path}





###### Other option ##########

def inspect_json_top_level_test(json_data, has_list=False):
    path = ""
    last_key = "value" #Placeholder

    while True:
        print(json.dumps(json_data, indent=2))
        print("\nVali json võti või indeks millest soovid väärtuse andmekonveieriga ekstrakteerida\n")

        if isinstance(json_data, dict):
            keys = list(json_data.keys())
            for index, key in enumerate(keys):
                value = json_data[key]
                value_type = type(value).__name__
                suggestion = "SplitJson" if isinstance(value, list) else "EvaluateJsonPath"
                print(f"  [{index}] {key} ({value_type}) → {suggestion}")

            selected_index = ask_digit_input(len(keys) - 1)
            selected_key = keys[selected_index]
            selected_value = json_data[selected_key]
            path += "." + selected_key
            last_key = selected_key

        elif isinstance(json_data, list):
            has_list = True
            for index, item in enumerate(json_data):
                item_type = type(item).__name__
                print(f"  [{index}] [{item_type}]")

            selected_index = ask_digit_input(len(json_data) - 1)
            selected_value = json_data[selected_index]
            path += f"[{selected_index}]"
            last_key = str(selected_index)

        else:
            # Primitive value, nothing to dive into
            print(f"\nLõppväärtus: {json_data}")
            return {last_key: path}



        if isinstance(selected_value, (dict, list)):
            json_data = selected_value
        else:
            #print(f"\nValitud väärtus: '{selected_value}'")
            print(f"\nValitud väärtus: '{path}'")
            return {last_key: path}


def get_data_values():

    chosen_json_values = {}

    ##Getting API url and json values
    while True:
        api_url = input("Palun sisesta andmete API URL: ").strip()
        username = "placeholder"
        passwd = "placeholder"

        needs_auth = 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: ")

        json_data, api_url_correct = is_app_url_correct(api_url,needs_auth,username,passwd)


        ## TODO itemite eemaldamise v6malus
        if api_url_correct:
            while True:

                chosen_json_values.update(inspect_json_top_level_test(json_data))
                ## Testing
                print("Oled hetkel valinud järgmised väärtused JSON lõppväärtused: ", ", ".join(chosen_json_values))
                choose_another = ask_binary_input(prompt="\nKas soovid (v)alida veel mõne väärtuse või liikuda (e)dasi?(v/e): ",valikud=["v","e"]).strip().lower()

                if choose_another == 'e':
                    return chosen_json_values, api_url, username, passwd
        else:
            choice = 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()