working api compltely

This commit is contained in:
2025-09-23 22:40:16 -04:00
parent f2faced238
commit f6e22bb975
21 changed files with 249 additions and 53 deletions

View File

@@ -1,6 +1,10 @@
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from settings_dev import settings
import requests
from fastapi import FastAPI, HTTPException, status
from .config import settings
from .voipms_client import update_did_routing
from .voipms_client import update_did_routing, get_forwardings
from .database import Session
from .models import Call
@@ -9,9 +13,33 @@ app = FastAPI(
description="An API to manage routing for a VoIP.ms DID.",
version="1.0.0",
)
@app.get("/test/forwardings")
def test_get_forwardings():
try:
result = get_forwardings(phone_number=settings.target_cellphone_1)
return {
"message": f"Forwarding entry for {settings.target_cellphone_1}",
"voipms_response": result
}
except HTTPException as e:
raise e
@app.get("/test/did")
def test_did_info():
params = {
"api_username": settings.voipms_api_username,
"api_password": settings.voipms_api_password,
"method": "getDIDsInfo", # Correct: plural "DIDs"
"did": settings.target_did, # Filters to one DID
}
try:
response = requests.get(settings.voipms_api_url, params=params)
print(f"Test Request URL: {response.request.url}")
print(f"Test Response: {response.json()}")
return response.json()
except requests.exceptions.RequestException as e:
return {"error": f"Failed to connect to VoIP.ms API: {str(e)}", "params": params}
except Exception as e:
return {"error": f"Unexpected error: {str(e)}", "params": params}
@app.get("/", tags=["General"])
def read_root():
"""A simple root endpoint to confirm the API is running."""
@@ -23,10 +51,12 @@ def route_to_sip_account():
"""
Routes the target DID to the pre-configured SIP account.
"""
routing_string = f"sip:{settings.target_sip_account}"
try:
# Use sub-account ID from TARGET_SIP_ACCOUNT
sip_account_id = settings.target_sip_account.split('@')[0] # Extract '407323_auburnoil'
routing_string = f"account:{sip_account_id}" # e.g., 'account:407323_auburnoil'
result = update_did_routing(did=settings.target_did, routing=routing_string)
target_phone = routing_string.split(':')[1]
target_phone = sip_account_id
db = Session()
db.add(Call(current_phone=target_phone))
db.commit()
@@ -36,7 +66,6 @@ def route_to_sip_account():
"voipms_response": result
}
except HTTPException as e:
# Re-raise the exception to let FastAPI handle the response
raise e
@@ -45,10 +74,18 @@ def route_to_cellphone_1():
"""
Routes the target DID to the pre-configured Cellphone #1.
"""
routing_string = f"fwd:{settings.target_cellphone_1}"
try:
# Get the forwarding entry for the phone number
forwarding = get_forwardings(phone_number=settings.target_cellphone_1)
forwarding_id = forwarding.get("forwarding") # e.g., '998692'
if not forwarding_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"No forwarding ID found for phone number {settings.target_cellphone_1}"
)
routing_string = f"fwd:{forwarding_id}" # e.g., 'fwd:998692'
result = update_did_routing(did=settings.target_did, routing=routing_string)
target_phone = routing_string.split(':')[1]
target_phone = settings.target_cellphone_1
db = Session()
db.add(Call(current_phone=target_phone))
db.commit()
@@ -60,16 +97,23 @@ def route_to_cellphone_1():
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:
# Get the forwarding entry for the phone number
forwarding = get_forwardings(phone_number=settings.target_cellphone_2)
forwarding_id = forwarding.get("forwarding") # e.g., ID for 9143306100
if not forwarding_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"No forwarding ID found for phone number {settings.target_cellphone_2}"
)
routing_string = f"fwd:{forwarding_id}" # e.g., 'fwd:<ID>'
result = update_did_routing(did=settings.target_did, routing=routing_string)
target_phone = routing_string.split(':')[1]
target_phone = settings.target_cellphone_2
db = Session()
db.add(Call(current_phone=target_phone))
db.commit()
@@ -79,4 +123,4 @@ def route_to_cellphone_2():
"voipms_response": result
}
except HTTPException as e:
raise e
raise e