Files
eamco_voipms/app/main.py
2025-09-22 21:18:24 -04:00

83 lines
2.7 KiB
Python

from fastapi import FastAPI, HTTPException, status
from .config import settings
from .voipms_client import update_did_routing
from .database import Session
from .models import Call
app = FastAPI(
title="EAMCO VoIP.ms Controller",
description="An API to manage routing for a VoIP.ms DID.",
version="1.0.0",
)
@app.get("/", tags=["General"])
def read_root():
"""A simple root endpoint to confirm the API is running."""
return {"message": f"Welcome to the VoIP.ms API for DID: {settings.target_did}"}
@app.post("/route/sip", status_code=status.HTTP_200_OK, tags=["DID Routing"])
def route_to_sip_account():
"""
Routes the target DID to the pre-configured SIP account.
"""
routing_string = f"sip:{settings.target_sip_account}"
try:
result = update_did_routing(did=settings.target_did, routing=routing_string)
target_phone = routing_string.split(':')[1]
db = Session()
db.add(Call(current_phone=target_phone))
db.commit()
db.close()
return {
"message": f"Successfully routed DID {settings.target_did} to SIP account {settings.target_sip_account}",
"voipms_response": result
}
except HTTPException as e:
# Re-raise the exception to let FastAPI handle the response
raise e
@app.post("/route/cellphone1", status_code=status.HTTP_200_OK, tags=["DID Routing"])
def route_to_cellphone_1():
"""
Routes the target DID to the pre-configured Cellphone #1.
"""
routing_string = f"fwd:{settings.target_cellphone_1}"
try:
result = update_did_routing(did=settings.target_did, routing=routing_string)
target_phone = routing_string.split(':')[1]
db = Session()
db.add(Call(current_phone=target_phone))
db.commit()
db.close()
return {
"message": f"Successfully routed DID {settings.target_did} to Cellphone #1 ({settings.target_cellphone_1})",
"voipms_response": result
}
except HTTPException as e:
raise e
@app.post("/route/cellphone2", status_code=status.HTTP_200_OK, tags=["DID Routing"])
def route_to_cellphone_2():
"""
Routes the target DID to the pre-configured Cellphone #2.
"""
routing_string = f"fwd:{settings.target_cellphone_2}"
try:
result = update_did_routing(did=settings.target_did, routing=routing_string)
target_phone = routing_string.split(':')[1]
db = Session()
db.add(Call(current_phone=target_phone))
db.commit()
db.close()
return {
"message": f"Successfully routed DID {settings.target_did} to Cellphone #2 ({settings.target_cellphone_2})",
"voipms_response": result
}
except HTTPException as e:
raise e