Adder json API by going to /json/nameofsoftware

This commit is contained in:
jyamihud 2022-04-02 10:43:52 +03:00
parent 8402645a14
commit 55f375a49f

View file

@ -35,7 +35,10 @@ class handler(BaseHTTPRequestHandler):
def start_page(self):
self.send_response(200)
if "/json/" not in self.path:
self.send_header('Content-type', 'text/html')
else:
self.send_header('Content-type', 'application/json')
self.end_headers()
def send(self, text):
@ -50,8 +53,22 @@ class handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.startswith("/json/"):
if self.path == "/":
# API CALL
term = self.path[6:]
software_data, match = search.search_app(term)
data = {"found":{"data":software_data,"match":match}}
data["suggestions"] = search.suggest(software_data)
text = json.dumps(data, indent = 2)
self.start_page()
self.wfile.write(text.encode("utf-8"))
elif self.path == "/":
# If the user is at the front page, let him get only the search bar