From 6952203a613ab226a7e7a8b2b4485e22d4fbe79e Mon Sep 17 00:00:00 2001 From: Sebastian Serfling Date: Mon, 14 Aug 2023 22:24:38 +0200 Subject: [PATCH] API-Server Frist Try --- .idea/.gitignore | 3 + .idea/api.iml | 10 +++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ Classes/main_classes.py | 5 ++ main.py | 62 +++++++++++++++++++ return_api.py | 8 +++ 8 files changed, 106 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/api.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 Classes/main_classes.py create mode 100644 main.py create mode 100644 return_api.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/api.iml b/.idea/api.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/.idea/api.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..eefea33 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d50cf45 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Classes/main_classes.py b/Classes/main_classes.py new file mode 100644 index 0000000..bc3ab63 --- /dev/null +++ b/Classes/main_classes.py @@ -0,0 +1,5 @@ +from pydantic import BaseModel + +class CL_Database_get(BaseModel): + name: str + diff --git a/main.py b/main.py new file mode 100644 index 0000000..36dcf1f --- /dev/null +++ b/main.py @@ -0,0 +1,62 @@ +from fastapi import FastAPI +from pydantic import BaseModel +import mysql.connector +from datetime import datetime +import Classes.main_classes as CL + +def database(query,name,user): + # server.start() + # print(f"{filename}-SSH Server start Port:{ssh_tunnel.server_port()}") + + mydb = mysql.connector.connect( + host="172.17.1.21", + port=3306, + user="root", + password="N53yBCswuawzBzS445VNAhWVMs3N59Gb9szEsrzXRBzarDqpdETpQeyt5v5CGe", + database="" + name, + auth_plugin='mysql_native_password', + ) + mydb.connect() + # print(f"{filename}-SQL Server Connect") + # print(f"{filename}-{query}") + cursor = mydb.cursor() + if "SELECT" in query: + if user: + cursor.execute(query, user) + return cursor.fetchone() + else: + cursor.execute(query, user) + # print(f"{filename}- Inside SELECT ALL") + return cursor.fetchall() + if "SHOW" in query: + cursor.execute(query) + return cursor.fetchall() + if "INSERT" in query: + cursor.execute(query,user) + mydb.commit() + if "UPDATE" in query: + cursor.execute(query,user) + mydb.commit() + mydb.close() + + +app = FastAPI() + +class Ping_Set(BaseModel): + ## Test + name: str + ipadress: str + +@app.post("/ping") +async def ping(ping: Ping_Set): + database(f"INSERT INTO `Ping-Server` VALUES ('{datetime.now()}','{ping.name}','{ping.ipadress}')","Stines-GmbH","") + return f"ADD Ping from {ping.name} to Server" + +## Get Database from Server +@app.post("/database_get/") +async def database_get(get: CL.CL_Database_get): + get_database = database("SHOW DATABASES","","") + if get.name in f"({get_database},)": + return True + else: + return False \ No newline at end of file diff --git a/return_api.py b/return_api.py new file mode 100644 index 0000000..ba55607 --- /dev/null +++ b/return_api.py @@ -0,0 +1,8 @@ +import requests + +def request(function,variable): + url = (f"http://127.0.0.1:8000/{function}/") + response = requests.post(url,json = variable) + return response + +print(request("database_get", {'name':'Jaehler-GmbH'}).text) \ No newline at end of file