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,7 +1,53 @@
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 HTTPException, status
from .config import settings
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):
"""
@@ -20,37 +66,84 @@ def update_did_routing(did: str, routing: str):
params = {
"api_username": settings.voipms_api_username,
"api_password": settings.voipms_api_password,
"method": "setDIDInfo",
"method": "setDIDRouting", # Correct method for routing updates
"did": did,
"routing": routing,
"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)
response.raise_for_status() # Raises an HTTPError for bad responses (4xx or 5xx)
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
# VoIP.ms API has its own status field in the JSON response
if data.get("status") != "success":
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"VoIP.ms API Error: {data.get('status')}"
detail=f"VoIP.ms API Error: {data.get('status')} - Full response: {data}"
)
return data
except requests.exceptions.RequestException as e:
# Handle network errors, timeouts, etc.
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Failed to connect to VoIP.ms API: {e}"
detail=f"Failed to connect to VoIP.ms API: {e} - Request params: {params}"
)
except Exception as e:
# Catch any other exceptions, including the ones we raised manually
if isinstance(e, HTTPException):
raise e # Re-raise if it's already an HTTPException
raise e
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"An unexpected error occurred: {e}"
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}"
)