first commit
This commit is contained in:
56
app/voipms_client.py
Normal file
56
app/voipms_client.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import requests
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
from .config import settings
|
||||
|
||||
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": "setDIDInfo",
|
||||
"did": did,
|
||||
"routing": routing,
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.get(settings.voipms_api_url, params=params)
|
||||
response.raise_for_status() # Raises an HTTPError for bad responses (4xx or 5xx)
|
||||
|
||||
data = response.json()
|
||||
|
||||
# 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')}"
|
||||
)
|
||||
|
||||
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}"
|
||||
)
|
||||
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 HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"An unexpected error occurred: {e}"
|
||||
)
|
||||
Reference in New Issue
Block a user