-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_calls.py
66 lines (57 loc) · 2.42 KB
/
api_calls.py
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
"""
Manage the api address
"""
import ssl
import requests
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QColor, QIcon
from PyQt5.QtWidgets import QLabel, QWidget, QTabWidget
from PyQt5.QtCore import QUrl, QUrlQuery, pyqtSignal, QEventLoop, QCoreApplication
class ApiCalls:
def __init__(self, dialog=None):
self.dialog = dialog
self.search_term = ""
self.base_url = "https://inaadress.maaamet.ee/inaadress/gazetteer"
self.my_context = ssl._create_unverified_context()
self.params = {}
self.search_responses = {}
self.search_addresses = []
self.search_dictionary = {}
def tr(self, message):
return QCoreApplication.translate('InAdsSearch', message)
def clearPreviousSearch(self):
self.search_responses = {}
self.search_addresses = []
def formatResponse(self, request_data):
self.clearPreviousSearch()
try:
self.search_responses = request_data.json()['addresses']
for full_response in self.search_responses:
taisaadress = full_response['taisaadress']
self.search_addresses.append(taisaadress)
self.search_dictionary[taisaadress] = full_response
except (IndexError, KeyError, TypeError):
# data does not have the inner structure you expect
self.dialog.response_list.addItem("No match found")
def displayAddress(self):
self.dialog.response_list.clear()
# Just to make it visually better, limiting the response display list
if len(self.search_addresses) >= 7:
for x in range(7):
self.dialog.response_list.addItem(self.search_addresses[x])
elif len(self.search_addresses) >= 1:
self.dialog.response_list.addItems(self.search_addresses)
else:
self.dialog.response_list.addItem("No match found")
def searchAddress(self, term):
self.search_term = term
self.params = {'address': term}
r = requests.get(url=self.base_url, params=self.params)
self.formatResponse(r)
self.displayAddress()
self.dialog.addres_text_box.cleared.connect(self.clearAddresses)
def clearAddresses(self):
self.search_responses = {}
self.search_addresses = []
self.search_dictionary = {}
self.dialog.response_list.clear()