Initial commit. Connects to Asterisk database and API endponts setup to create endpoints, delete endpoints, update passwords, and get array of users.
This commit is contained in:
70
db.py
Normal file
70
db.py
Normal file
@@ -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()
|
||||||
43
main.py
Normal file
43
main.py
Normal file
@@ -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}"}
|
||||||
21
test_main.http
Normal file
21
test_main.http
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user