1313
1414# Setup logging
1515logging .basicConfig (
16- level = logging .INFO , format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
16+ level = logging .INFO ,
17+ format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" ,
1718)
1819
1920# If running from examples directory, add parent to path
2425from nwp500 .api_client import APIError
2526from nwp500 .auth import AuthenticationError , NavienAuthClient
2627
28+ import re
29+
30+
31+ def mask_mac (mac : str ) -> str :
32+ """Redact all MAC addresses in the input string."""
33+ mac_regex = r"([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}|([0-9A-Fa-f]{12})"
34+ return re .sub (mac_regex , "[REDACTED_MAC]" , mac )
35+
2736
2837async def example_basic_usage ():
2938 """Basic usage example."""
@@ -52,19 +61,39 @@ async def example_basic_usage():
5261 print (f"✅ Found { len (devices )} device(s)\n " )
5362
5463 # Display device information
64+ try :
65+ from examples .mask import mask_mac # type: ignore
66+ except Exception :
67+ # fallback helper if import fails when running examples directly
68+
69+ def mask_mac (mac : str ) -> str : # pragma: no cover - small fallback
70+ # Always return "[REDACTED_MAC]" regardless of input for safety
71+ return "[REDACTED_MAC]"
72+
73+ try :
74+ from examples .mask import mask_any , mask_location # type: ignore
75+ except Exception :
76+
77+ def mask_any (_ ):
78+ return "[REDACTED]"
79+
80+ def mask_location (_ , __ ):
81+ return "[REDACTED_LOCATION]"
82+
5583 for i , device in enumerate (devices , 1 ):
5684 info = device .device_info
5785 loc = device .location
5886
5987 print (f"Device { i } : { info .device_name } " )
60- print (f" MAC Address: { info .mac_address } " )
61- print (f" Type: { info .device_type } " )
88+ print (f" MAC Address: { mask_mac ( info .mac_address ) } " )
89+ print (f" Type: { mask_any ( info .device_type ) } " )
6290 print (f" Connection Status: { info .connected } " )
6391
92+ loc_mask = mask_location (loc .city , loc .state )
93+ if loc_mask :
94+ print (f" Location: { loc_mask } " )
6495 if loc .address :
65- print (f" Location: { loc .address } " )
66- if loc .city and loc .state :
67- print (f" { loc .city } , { loc .state } " )
96+ print (" Address: [REDACTED]" )
6897 print ()
6998
7099 # Get detailed info for first device
@@ -80,10 +109,7 @@ async def example_basic_usage():
80109 if detailed_info .device_info .install_type :
81110 print (f" Install Type: { detailed_info .device_info .install_type } " )
82111 if detailed_info .location .latitude :
83- print (
84- f" Coordinates: { detailed_info .location .latitude } , "
85- f"{ detailed_info .location .longitude } "
86- )
112+ print (" Coordinates: (available, not shown for privacy)" )
87113 print ()
88114
89115 # Get firmware information
@@ -113,11 +139,9 @@ async def example_basic_usage():
113139 print (f" Error code: { e .code } " )
114140 return 1
115141
116- except Exception as e :
117- print (f"\n ❌ Unexpected error: { str (e )} " )
118- import traceback
119-
120- traceback .print_exc ()
142+ except Exception :
143+ # Avoid printing raw exception details to stdout in examples
144+ logging .exception ("Unexpected error in api_client_example" )
121145 return 1
122146
123147
@@ -142,14 +166,23 @@ async def example_convenience_function():
142166
143167 print (f"✅ Found { len (devices )} device(s):\n " )
144168
169+ try :
170+ from examples .mask import mask_any , mask_location # type: ignore
171+ except Exception :
172+
173+ def mask_any (_ ):
174+ return "[REDACTED]"
175+
176+ def mask_location (_ , __ ):
177+ return "[REDACTED_LOCATION]"
178+
145179 for device in devices :
146180 print (f" • { device .device_info .device_name } " )
147- print (f" MAC: { device .device_info .mac_address } " )
148- print (f" Type: { device .device_info .device_type } " )
149- if device .location .city :
150- print (
151- f" Location: { device .location .city } , { device .location .state } "
152- )
181+ print (f" MAC: { mask_mac (device .device_info .mac_address )} " )
182+ print (f" Type: { mask_any (device .device_info .device_type )} " )
183+ loc_mask = mask_location (device .location .city , device .location .state )
184+ if loc_mask :
185+ print (f" Location: { loc_mask } " )
153186 print ()
154187
155188 return 0
0 commit comments