Notification handling
You can optionally configure a notification (webhook) URL with an auth token to receive updates from different functionalities, such as device connectivity status, QoD session or slice creation, deletion and so on. This way, you or your client can receive update notifications from device events and stay in control.
Subscribing to notifications
import network_as_code as nac
from network_as_code.models.device import Device, DeviceIpv4Addr
client = nac.NetworkAsCodeClient(...)
device = client.devices.get(...)
# Here, we will keep track of when a device gets connected with a limit of 5 reports
subscription = client.connectivity.subscribe(
event_type="CONNECTIVITY",
device=device,
max_num_of_reports=5,
notification_url="https://example.com/notifications",
notification_auth_token="replace-with-your-auth-token"
)
Subscription parameters
Parameters | Description |
---|---|
event_type | The status type you want to check, e.g. "CONNECTIVITY" or "ROAMING_STATUS" . |
device | Device ID callback parameter. |
max_num_of_reports | How many reports will be sent to endpoint. |
notification_url | Create or provide an API endpoint (also referred as callback URL or webhook). It should point to the recipient's HTTP endpoint that will receive the notifications. It needs to be a web server that is configured to receive POST requests that will contain session related updates, such as session creation, deletion, duration, etc. |
notification_auth_token | An auth token is required to identify the sender of the notification. The incoming POST request will contain the token as Authorization: Bearer <token> header and Network-as-Code backend will send it to the informed notification_url . Always specify this parameter when using the notification functionality. |
NOTE: The exact implementation of the
notification_url
HTTP endpoint, which listens to the incomingPOST
requests at the/notifications
URL path, can be handled by developers as they see fit.
Notifications handler
The code snippet below will set up an HTTP server with a POST endpoint. Notifications will be sent for when a device is available or not.
# status_handler.py for CONNECTIVITY
# run with: uvicorn status_handler:app
from fastapi import FastAPI, Header
from pydantic import BaseModel
from typing_extensions import Annotated
from typing import Union
app = FastAPI()
class ConnectivityEventDetail(BaseModel):
deviceStatus: str
class Event(BaseModel):
eventType: str
eventTime: str
eventDetail: ConnectivityEventDetail
class Notification(BaseModel):
eventSubscriptionId: str
event: Event
@app.post("/notifications")
def receive_notification(notification: Notification,
authorization: Annotated[Union[str, None], Header]):
if authorization == "Bearer my-token":
if notification.event.eventDetail.deviceStatus == "REACHABLE":
print("Device is available")
elif notification.event.eventDetail.deviceStatus == "UNREACHABLE":
print("Device is not available")
Where can I use a notification URL and token?
Now that you've learned what a notification URL is and how to create one, you can explore more about the functionalities that use it to notify you of important Network as Code events.
- Check out how to create a QoD session with a notification URL.
- Get device-connectivity or roaming-status notifications.
- Monitor a network slice with notifications.