Session notifications
You can optionally configure a notification (webhook) URL to receive QoD session updates. This way, you or your client can be notified of important session events, such as creation, deletion, etc. Learn more about the notification URL/auth token and how to create an HTTP server with a POST endpoint for session notifications.
With a notification URL, you can also keep track of different QoD session statutes. Learn more about the QoD session life cycle.
Creating a session with a notification URL
Create a QoD session providing parameters for your API endpoint and password (authorization token) to identify the sender of the notifications.
This code also creates a Network as Code client using an application key accessible from your Dashboard
and identifies a mobile network device in multiple ways (IP addresses, port, etc).
Learn more about the client
and device objects.
Remember that devices cannot be identified by their public IPv4 address alone.
You will need to provide either their public address and port or public and private addresses.
Learn more
about how the DeviceIPv4Addr
model works using NAT technology.
import network_as_code as nac
from network_as_code.models.device import Device, DeviceIpv4Addr
# Begin by creating a client for Network as Code:
client = nac.NetworkAsCodeClient(
token="<your-application-key-here>",
)
# Then, create a device object.
# Remember to assign its Device ID and current IP address(es):
my_device = client.devices.get(
"device@testcsp.net",
ipv4_address=DeviceIpv4Addr(
public_address="233.252.0.2",
private_address="192.0.2.25",
public_port=80
),
ipv6_address="2001:db8:1234:5678:9abc:def0:fedc:ba98",
# The phone number accepts the "+" sign, but not spaces or "()" marks
phone_number="36721601234567"
)
# Create a QoD session with QOS_L (large bandwidth)
# and get notifications
my_session = my_device.create_qod_session(
service_ipv4="233.252.0.2",
service_ipv6="2001:db8:1234:5678:9abc:def0:fedc:ba98",
profile="QOS_L",
# Use HTTPS to send notifications
notification_url="https://notify.me/here",
notification_auth_token="replace-with-your-auth-token"
)
# Show a list of all the QoD sessions associated with a device
print(my_device.sessions())
Setting up a web server
The code snippet below will set up an HTTP server with a POST endpoint, so you can get session-event updates:
import time
from fastapi import FastAPI, Header
from pydantic import BaseModel
from typing_extensions import Annotated
from typing import Union
# Our web server for receiving QoD notifications
app = FastAPI()
class EventDetail(BaseModel):
sessionId: str
qosStatus: str
statusInfo: str
class Event(BaseModel):
eventType: str
eventTime: str
eventDetail: EventDetail
class Notification(BaseModel):
id: str
source: str
type: str
specversion: str
datacontenttype: str
time: str
event: Event
data: EventDetail
@app.post("/qod")
def receive_notification(
notification: Notification,
authorization: Annotated[Union[str, None], Header]
):
if authorization == "Bearer my-token":
# We can now react to the notifications
# based on the Notification object
print(notification)
Session notification parameters
The Session
object is created with a specific duration,
representing a new session for the device.
The following parameters can be passed to this method:
Parameters | Description |
---|---|
profile | The QoS profile that indicates the connection type to be prioritized between two points. |
service_ipv4 | The service identified by the application IPv4 address (optional). |
service_ipv6 | The service identified by the application IPv6 address (optional). |
notification_url | The recipient's HTTP endpoint, which is a web server configured to receive POST requests. |
notification_auth_token | The password used to identify the sender of the notification. |
Last updated on July 25, 2024