API-Server Frist Try

master
Sebastian Serfling 2023-08-14 22:24:38 +02:00
commit 6952203a61
8 changed files with 106 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

10
.idea/api.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (api)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/api.iml" filepath="$PROJECT_DIR$/.idea/api.iml" />
</modules>
</component>
</project>

5
Classes/main_classes.py Normal file
View File

@ -0,0 +1,5 @@
from pydantic import BaseModel
class CL_Database_get(BaseModel):
name: str

62
main.py Normal file
View File

@ -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

8
return_api.py Normal file
View File

@ -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)