2222
2323import logging
2424import time
25+ import warnings
2526from typing import TYPE_CHECKING
2627
2728from ..utils import UNCHANGED , _Sentinel , check_stale , get_url_from_template , locked
@@ -48,7 +49,9 @@ class Link:
4849 "capture_start" : "{lab}/links/{id}/capture/start" ,
4950 "capture_stop" : "{lab}/links/{id}/capture/stop" ,
5051 "capture_status" : "{lab}/links/{id}/capture/status" ,
51- "capture_key" : "{lab}/links/{id}/capture/key" ,
52+ "pcap_file" : "{pcap}/{id}" ,
53+ "pcap_packets" : "{pcap}/{id}/packets" ,
54+ "pcap_packet" : "{pcap}/{id}/packets/{packet_id}" ,
5255 }
5356
5457 def __init__ (
@@ -113,8 +116,11 @@ def _url_for(self, endpoint: str, **kwargs):
113116 :param **kwargs: Keyword arguments used to format the URL.
114117 :returns: The formatted URL.
115118 """
116- kwargs ["lab" ] = self ._lab ._url_for ("lab" )
117- kwargs ["id" ] = self .id
119+ if endpoint .startswith ("pcap" ):
120+ kwargs ["pcap" ] = f"{ self ._session .base_url } /api/v0/pcap"
121+ else :
122+ kwargs ["lab" ] = self ._lab ._url_for ("lab" )
123+ kwargs ["id" ] = self ._id
118124 return get_url_from_template (endpoint , self ._URL_TEMPLATES , kwargs )
119125
120126 @property
@@ -442,7 +448,7 @@ def start_capture(
442448 if bpfilter is not None :
443449 data ["bpfilter" ] = bpfilter
444450
445- _LOGGER .info (f"Starting packet capture on link { self .id } " )
451+ _LOGGER .info (f"Starting packet capture on link { self ._id } " )
446452 return self ._session .put (url , json = data ).json ()
447453
448454 @check_stale
@@ -451,7 +457,7 @@ def stop_capture(self) -> None:
451457 Stop the packet capture on this link.
452458 """
453459 url = self ._url_for ("capture_stop" )
454- _LOGGER .info (f"Stopping packet capture on link { self .id } " )
460+ _LOGGER .info (f"Stopping packet capture on link { self ._id } " )
455461 self ._session .put (url )
456462
457463 @check_stale
@@ -464,58 +470,33 @@ def capture_status(self) -> dict:
464470 url = self ._url_for ("capture_status" )
465471 return self ._session .get (url ).json ()
466472
467- @check_stale
468- def capture_key (self ) -> str :
469- """
470- Get the capture key (UUID) for the packet capture on this link.
471-
472- :returns: The capture key as a string.
473- :raises: HTTP exception if no capture is running on this link.
474- """
475- url = self ._url_for ("capture_key" )
476- return self ._session .get (url ).json ()
477-
478- def download_capture (self , capture_key : str | None = None ) -> bytes :
473+ def download_capture (self ) -> bytes :
479474 """
480- Download the PCAP file for this link's capture.
475+ Download the PCAP file for this link's last capture.
481476
482- :param capture_key: The capture key. If None, will fetch it automatically.
483477 :returns: The PCAP file content as bytes.
484478 """
485- if capture_key is None :
486- capture_key = self .capture_key ()
487-
488- url = f"{ self ._lab ._session .base_url } /api/v0/pcap/{ capture_key } "
489- _LOGGER .info (f"Downloading PCAP for capture key { capture_key } " )
479+ url = self ._url_for ("pcap_file" )
480+ _LOGGER .info (f"Downloading PCAP for link { self ._id } " )
490481 return self ._session .get (url ).content
491482
492- def get_capture_packets (self , capture_key : str | None = None ) -> list [dict ]:
483+ def get_capture_packets (self ) -> list [dict ]:
493484 """
494- Get a list of all captured packets in decoded format.
485+ Get a list of all captured packets in decoded format from last capture .
495486
496- :param capture_key: The capture key. If None, will fetch it automatically.
497487 :returns: List of packet dictionaries with decoded packet information.
498488 """
499- if capture_key is None :
500- capture_key = self .capture_key ()
501-
502- url = f"{ self ._lab ._session .base_url } /api/v0/pcap/{ capture_key } /packets"
503- _LOGGER .info (f"Getting packet list for capture key { capture_key } " )
489+ url = self ._url_for ("pcap_packets" )
490+ _LOGGER .info (f"Getting packet list for link { self ._id } " )
504491 return self ._session .get (url ).json ()
505492
506- def download_capture_packet (
507- self , packet_id : int , capture_key : str | None = None
508- ) -> dict :
493+ def get_capture_packet (self , packet_id : int ) -> dict :
509494 """
510- Download a specific packet from the capture in decoded format.
495+ Get a specific packet from the last capture in decoded format.
511496
512- :param packet_id: The ID of the packet to download (1-based).
513- :param capture_key: The capture key. If None, will fetch it automatically.
497+ :param packet_id: The ID of the packet (1-based index).
514498 :returns: Dictionary containing the decoded packet information.
515499 """
516- if capture_key is None :
517- capture_key = self .capture_key ()
518-
519- url = f"{ self ._lab ._session .base_url } /api/v0/pcap/{ capture_key } /packet/{ packet_id } "
520- _LOGGER .info (f"Downloading packet { packet_id } for capture key { capture_key } " )
500+ url = self ._url_for ("pcap_packet" , packet_id = packet_id )
501+ _LOGGER .info (f"Downloading packet { packet_id } for link { self ._id } " )
521502 return self ._session .get (url ).json ()
0 commit comments