handle_request

View as MarkdownOpen in Claude

Dispatch a request over plain values instead of FastAPI objects. Performs proxy detection, basic auth, the routing callback check, and on_request() modification, then renders the document. The FastAPI routes delegate to the same logic, so both paths return identical responses. Basic auth is always on: pass basic_auth to the constructor and send a matching Authorization header, or the call returns 401.

Parameters

method
strRequired

HTTP method, such as "GET" or "POST".

url
strRequired

The full request URL. Used for proxy detection and to match a registered routing callback path.

headers
dict[str, str]Required

Request headers as a plain dictionary.

body
dict[str, Any] | NoneDefaults to None

The already-parsed JSON body for POST requests.

Returns

tuple[int, dict[str, str], str](status_code, response_headers, body).

  • 200 with the SWML document as a JSON string.
  • 307 with a Location header and an empty body when a routing callback returned a destination.
  • 401 with WWW-Authenticate: Basic and a JSON error when basic auth fails.

Example

import base64
import json
from signalwire import SWMLService
service = SWMLService(name="greeting", route="/greeting", basic_auth=("greeting", "s3cret"))
service.add_verb("answer", {})
service.add_verb("play", {"url": "say:Welcome to Bayview Taxi."})
credentials = base64.b64encode(b"greeting:s3cret").decode()
status, headers, body = service.handle_request(
"GET",
"https://bayview-taxi.example.com/greeting",
{"authorization": f"Basic {credentials}"},
)
print(status, json.loads(body)["sections"]["main"])