From b6653f3c152836985f68b31a36486d484482a9fc Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 25 Mar 2025 16:53:31 -0400 Subject: [PATCH] Initial commit. Connects to Asterisk database and API endponts setup to create endpoints, delete endpoints, update passwords, and get array of users. --- db.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 43 +++++++++++++++++++++++++++++++ test_main.http | 21 +++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 db.py create mode 100644 main.py create mode 100644 test_main.http diff --git a/db.py b/db.py new file mode 100644 index 0000000..207b259 --- /dev/null +++ b/db.py @@ -0,0 +1,70 @@ +import mysql.connector +def test(): + cnx = mysql.connector.connect(user='asterisk', password='ESDx]hqxS*z.qHu3', host='10.0.0.10', database='asterisk') + cursor = cnx.cursor() + + cursor.execute("select * from ps_aors") + for row in cursor: + print(row) + + sequence = cursor.column_names + print(sequence) + + + cnx.close() + +def create_Endpoint(username, context, password): + cnx = mysql.connector.connect(user='asterisk', password='ESDx]hqxS*z.qHu3', host='10.0.0.10', database='asterisk') + cursor = cnx.cursor() +# endpoint + query = ("INSERT INTO asterisk.ps_endpoints(id, transport,aors,auth,CONTEXT, disallow, allow) " + "VALUES(\'"+username+"\', 'transport-udp',\'"+username+"\',\'"+username+"\',\'"+context+"\','all','ulaw')") + cursor.execute(query ) + print(query) +# aors + query = ("INSERT INTO asterisk.ps_aors(id, max_contacts)" + "VALUES(\'"+username+"\', 2)") + cursor.execute(query) +# auths + query = ("INSERT INTO asterisk.ps_auths(id, auth_type, PASSWORD, username)" + "VALUES(\'" + username + "\','userpass',\'"+password+"\',\'"+username+"\')") + + cursor.execute(query) + cnx.commit() + cnx.close() + +def delete_Endpoint(username): + cnx = mysql.connector.connect(user='asterisk', password='ESDx]hqxS*z.qHu3', host='10.0.0.10', database='asterisk') + cursor = cnx.cursor() +# endpoint + query = ("DELETE FROM asterisk.ps_endpoints WHERE id=\'"+username+"\'") + cursor.execute(query) +# aors + query = ("DELETE FROM asterisk.ps_aors WHERE id=\'"+username+"\'") + cursor.execute(query) +# auths + query = ("DELETE FROM asterisk.ps_auths WHERE id=\'"+username+"\'") + cursor.execute(query) + cnx.commit() + cnx.close() + +def update_Password(username, password): + cnx = mysql.connector.connect(user='asterisk', password='ESDx]hqxS*z.qHu3', host='10.0.0.10', database='asterisk') + cursor = cnx.cursor() + + query = ("update asterisk.ps_auths set password = \'"+password+"\' where id=\'"+username+"\'") + cursor.execute(query) + + cnx.commit() + cnx.close() + +def getUsers(): + + cnx = mysql.connector.connect(user='asterisk', password='ESDx]hqxS*z.qHu3', host='10.0.0.10', database='asterisk') + cursor = cnx.cursor() + + query = ("select username from asterisk.ps_auths") + cursor.execute(query) + # print(cursor.fetchall()) + return cursor.fetchall() + cnx.close() \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..c972cee --- /dev/null +++ b/main.py @@ -0,0 +1,43 @@ +from fastapi import FastAPI + +import db + +app = FastAPI() + + +@app.get("/") +async def root(): + return {"message": "Hello World"} + + +@app.get("/hello/{name}") +async def say_hello(name: str): + return {"message": f"Hello {name}"} + +@app.get("/test/{name}+{name2}") +async def say_hello(name: str, name2: str): + return {"message": f"Hello {name}. Or are you known as {name2}?"} + +@app.get("/createEndpoint/{username}+{context}+{password}") +async def createEndpoint(username: str, context: str, password: str): + db.create_Endpoint(username, context, password) + return {"message": f"{username}, {context}, {password}"} + +@app.get("/deleteEndpoint/{username}") +async def deleteEndpoint(username: str): + db.delete_Endpoint(username) + return {"message": f"{username}"} + +@app.get("/updatePassword/{username}+{password}") +async def updatePassword(username: str, password: str): + db.update_Password(username, password) + return {"message": f"{username}"} + +@app.get("/getUsers/") +async def getUsers(): + all=[] + users=db.getUsers() + for each in users: + all.append(each[0]) + print(all) + return {"message": f"{all}"} \ No newline at end of file diff --git a/test_main.http b/test_main.http new file mode 100644 index 0000000..8166a39 --- /dev/null +++ b/test_main.http @@ -0,0 +1,21 @@ +# Test your FastAPI endpoints + +GET http://127.0.0.1:8000/ +Accept: application/json + +### + +GET http://127.0.0.1:8000/hello/User +Accept: application/json +### +GET http://127.0.0.1:8000/test/User +Accept: application/json +### +GET http://127.0.0.1:8000/creatEndpoint/data +Accept: application/json +### +GET http://127.0.0.1:8000/deleteEndpoint/data +Accept: application/json +### +GET http://127.0.0.1:8000/updatePassword/data +Accept: application/json \ No newline at end of file