Session notifications

Creating a session with a notification URL

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.

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)

Device object parameters

The snippet above identified a mobile network device in multiple ways (IP addresses, port, etc). Learn how to create a device object and understand how the DeviceIPv4Addr model works using NAT technology.

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:

ParametersDescription
profileThe QoS profile that indicates the connection type to be prioritized between two points.
service_ipv4The service identified by the application IPv4 address (optional).
service_ipv6The service identified by the application IPv6 address (optional).
notification_urlThe recipient's HTTP endpoint, which is a web server configured to receive POST requests.
notification_auth_tokenThe password used to identify the sender of the notification.

What is a notification URL?

Learn more about the notification URL/auth token and how to create a web server for them.

Last updated on May 21, 2024