156 lines
5.4 KiB
Python
156 lines
5.4 KiB
Python
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
# Dynamically import settings based on MODE environment variable
|
|
mode = os.environ.get('MODE', 'DEVELOPMENT').upper()
|
|
if mode == 'PRODUCTION':
|
|
from settings_prod import settings
|
|
else:
|
|
from settings_dev import settings
|
|
|
|
import requests
|
|
from fastapi import HTTPException, status
|
|
|
|
def get_did_info(did: str):
|
|
"""
|
|
Calls the VoIP.ms API to get DID information.
|
|
|
|
Args:
|
|
did (str): The phone number (DID) to query.
|
|
|
|
Raises:
|
|
HTTPException: If the API call fails or returns an error.
|
|
|
|
Returns:
|
|
dict: The JSON response from the VoIP.ms API on success.
|
|
"""
|
|
params = {
|
|
"api_username": settings.voipms_api_username,
|
|
"api_password": settings.voipms_api_password,
|
|
"method": "getDIDInfo",
|
|
"did": did,
|
|
}
|
|
|
|
try:
|
|
response = requests.get(settings.voipms_api_url, params=params)
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
if data.get("status") != "success":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"VoIP.ms API Error: {data.get('status')}. Full response: {data}. Request params: {params}"
|
|
)
|
|
return data
|
|
except requests.exceptions.RequestException as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail=f"Failed to connect to VoIP.ms API: {e}"
|
|
)
|
|
except Exception as e:
|
|
if isinstance(e, HTTPException):
|
|
raise e
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"An unexpected error occurred: {e}"
|
|
)
|
|
|
|
def update_did_routing(did: str, routing: str):
|
|
"""
|
|
Calls the VoIP.ms API to update the routing for a specific DID.
|
|
|
|
Args:
|
|
did (str): The phone number (DID) to update.
|
|
routing (str): The new routing string (e.g., 'sip:user@server' or 'fwd:15551234567').
|
|
|
|
Raises:
|
|
HTTPException: If the API call fails or returns an error.
|
|
|
|
Returns:
|
|
dict: The JSON response from the VoIP.ms API on success.
|
|
"""
|
|
params = {
|
|
"api_username": settings.voipms_api_username,
|
|
"api_password": settings.voipms_api_password,
|
|
"method": "setDIDRouting", # Correct method for routing updates
|
|
"did": did,
|
|
"routing": routing, # e.g., 'fwd:7743342638'
|
|
"pop": "75", # Match the DID's current POP (from getDIDsInfo)
|
|
}
|
|
|
|
try:
|
|
response = requests.get(settings.voipms_api_url, params=params)
|
|
print(f"Request URL: {response.request.url}") # Debug
|
|
print(f"Request Params: {params}") # Debug
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
print(f"VoIP.ms API Response: {data}") # Debug
|
|
|
|
if data.get("status") != "success":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"VoIP.ms API Error: {data.get('status')} - Full response: {data}"
|
|
)
|
|
|
|
return data
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail=f"Failed to connect to VoIP.ms API: {e} - Request params: {params}"
|
|
)
|
|
except Exception as e:
|
|
if isinstance(e, HTTPException):
|
|
raise e
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"An unexpected error occurred: {e} - Request params: {params}"
|
|
)
|
|
|
|
def get_forwardings(phone_number: str = None):
|
|
"""
|
|
Retrieves call forwarding entries from VoIP.ms.
|
|
|
|
Args:
|
|
phone_number (str, optional): Filter by phone number (e.g., '7743342638').
|
|
|
|
Returns:
|
|
dict: The JSON response from the VoIP.ms API, including forwarding IDs.
|
|
"""
|
|
params = {
|
|
"api_username": settings.voipms_api_username,
|
|
"api_password": settings.voipms_api_password,
|
|
"method": "getForwardings",
|
|
}
|
|
try:
|
|
response = requests.get(settings.voipms_api_url, params=params)
|
|
print(f"Get Forwardings Request URL: {response.request.url}")
|
|
print(f"Get Forwardings Response: {response.json()}")
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
if data.get("status") != "success":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"VoIP.ms API Error: {data.get('status')} - Full response: {data}"
|
|
)
|
|
# Filter by phone_number if provided
|
|
if phone_number and "forwardings" in data:
|
|
for forwarding in data["forwardings"]:
|
|
if forwarding.get("phone_number") == phone_number:
|
|
return forwarding
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"No forwarding entry found for phone number {phone_number}"
|
|
)
|
|
return data
|
|
except requests.exceptions.RequestException as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail=f"Failed to connect to VoIP.ms API: {e} - Request params: {params}"
|
|
)
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"An unexpected error occurred: {e} - Request params: {params}"
|
|
)
|