44
55import json
66import logging
7+ from urllib .parse import urlparse
78
89import aiohttp
910
1213logger = logging .getLogger (__name__ )
1314
1415
15- class AirOS8 :
16+ class AirOS :
1617 """Set up connection to AirOS."""
1718
1819 def __init__ (
@@ -21,12 +22,22 @@ def __init__(
2122 username : str ,
2223 password : str ,
2324 session : aiohttp .ClientSession ,
25+ use_ssl : bool = True ,
2426 verify_ssl : bool = True ,
2527 ):
2628 """Initialize AirOS8 class."""
2729 self .username = username
2830 self .password = password
29- self .base_url = f"https://{ host } "
31+
32+ parsed_host = urlparse (host )
33+ scheme = (
34+ parsed_host .scheme
35+ if parsed_host .scheme
36+ else ("https" if use_ssl else "http" )
37+ )
38+ hostname = parsed_host .hostname if parsed_host .hostname else host
39+
40+ self .base_url = f"{ scheme } ://{ hostname } "
3041
3142 self .session = session
3243 self .verify_ssl = verify_ssl
@@ -51,6 +62,8 @@ def __init__(
5162 "X-Requested-With" : "XMLHttpRequest" ,
5263 }
5364
65+ self .connected = False
66+
5467 async def login (self ) -> bool :
5568 """Log in to the device assuring cookies and tokens set correctly."""
5669 # --- Step 0: Pre-inject the 'ok=1' cookie before login POST (mimics curl) ---
@@ -146,11 +159,10 @@ async def login(self) -> bool:
146159 if not airos_cookie_found and not ok_cookie_found :
147160 raise DataMissingError from None
148161
149- response_text = await response .text ()
150-
151162 if response .status == 200 :
152163 try :
153- json .loads (response_text )
164+ json .loads (response .text )
165+ self .connected = True
154166 return True
155167 except json .JSONDecodeError as err :
156168 logger .exception ("JSON Decode Error" )
@@ -166,6 +178,10 @@ async def login(self) -> bool:
166178
167179 async def status (self ) -> dict :
168180 """Retrieve status from the device."""
181+ if not self .connected :
182+ logger .error ("Not connected, login first" )
183+ raise ConnectionFailedError from None
184+
169185 # --- Step 2: Verify authenticated access by fetching status.cgi ---
170186 authenticated_get_headers = {** self ._common_headers }
171187 if self .current_csrf_token :
@@ -177,18 +193,16 @@ async def status(self) -> dict:
177193 headers = authenticated_get_headers ,
178194 ssl = self .verify_ssl ,
179195 ) as response :
180- status_response_text = await response .text ()
181-
182196 if response .status == 200 :
183197 try :
184- return json .loads (status_response_text )
198+ return json .loads (response . text )
185199 except json .JSONDecodeError :
186200 logger .exception (
187201 "JSON Decode Error in authenticated status response"
188202 )
189203 raise DataMissingError from None
190204 else :
191- log = f"Authenticated status.cgi failed: { response .status } . Response: { status_response_text } "
205+ log = f"Authenticated status.cgi failed: { response .status } . Response: { response . text } "
192206 logger .error (log )
193207 except aiohttp .ClientError as err :
194208 logger .exception ("Error during authenticated status.cgi call" )
0 commit comments