@@ -137,6 +137,7 @@ def __init__(self, url: Optional[str] = None,
137137 client_params ["connector" ] = aiohttp .TCPConnector (
138138 ssl = self .validate_certs
139139 )
140+ # HTTP Auth unsupported since Zabbix 7.2
140141 if http_user and http_password :
141142 client_params ["auth" ] = aiohttp .BasicAuth (
142143 login = http_user ,
@@ -153,6 +154,10 @@ def __init__(self, url: Optional[str] = None,
153154
154155 self .__check_version (skip_version_check )
155156
157+ if self .version > 7.0 and http_user and http_password :
158+ self .__close_session ()
159+ raise APINotSupported ("HTTP authentication unsupported since Zabbix 7.2." )
160+
156161 def __getattr__ (self , name : str ) -> Callable :
157162 """Dynamic creation of an API object.
158163
@@ -171,14 +176,18 @@ async def __aenter__(self) -> Callable:
171176 async def __aexit__ (self , * args ) -> None :
172177 await self .logout ()
173178
174- async def __close_session (self ) -> None :
179+ async def __aclose_session (self ) -> None :
175180 if self .__internal_client :
176181 await self .__internal_client .close ()
177182
178183 async def __exception (self , exc ) -> None :
179- await self .__close_session ()
184+ await self .__aclose_session ()
180185 raise exc from exc
181186
187+ def __close_session (self ) -> None :
188+ if self .__internal_client :
189+ self .__internal_client ._connector .close ()
190+
182191 def api_version (self ) -> APIVersion :
183192 """Return object of Zabbix API version.
184193
@@ -261,13 +270,13 @@ async def logout(self) -> None:
261270 if self .__use_token :
262271 self .__session_id = None
263272 self .__use_token = False
264- await self .__close_session ()
273+ await self .__aclose_session ()
265274 return
266275
267276 log .debug ("Logout from Zabbix API" )
268277 await self .user .logout ()
269278 self .__session_id = None
270- await self .__close_session ()
279+ await self .__aclose_session ()
271280 else :
272281 log .debug ("You're not logged in Zabbix API" )
273282
@@ -309,7 +318,9 @@ def __prepare_request(self, method: str, params: Optional[dict] = None,
309318 if need_auth :
310319 if not self .__session_id :
311320 raise ProcessingError ("You're not logged in Zabbix API" )
312- if self .version < 6.4 or self .client_session ._default_auth is not None :
321+ if self .version < 6.4 :
322+ request ['auth' ] = self .__session_id
323+ elif self .version <= 7.0 and self .client_session ._default_auth is not None :
313324 request ['auth' ] = self .__session_id
314325 else :
315326 headers ["Authorization" ] = f"Bearer { self .__session_id } "
@@ -405,6 +416,7 @@ def send_sync_request(self, method: str, params: Optional[dict] = None,
405416
406417 request_json , headers = self .__prepare_request (method , params , need_auth )
407418
419+ # HTTP Auth unsupported since Zabbix 7.2
408420 basic_auth = self .client_session ._default_auth
409421 if basic_auth is not None :
410422 headers ["Authorization" ] = "Basic " + base64 .b64encode (
@@ -433,9 +445,16 @@ def send_sync_request(self, method: str, params: Optional[dict] = None,
433445 resp = ul .urlopen (req , context = ctx )
434446 resp_json = json .loads (resp .read ().decode ('utf-8' ))
435447 except URLError as err :
448+ if basic_auth is not None :
449+ log .warning ("HTTP authentication unsupported since Zabbix 7.2." )
450+ self .__close_session ()
436451 raise ProcessingError (f"Unable to connect to { self .url } :" , err ) from None
437452 except ValueError as err :
453+ self .__close_session ()
438454 raise ProcessingError ("Unable to parse json:" , err ) from None
455+ except Exception as err :
456+ self .__close_session ()
457+ raise ProcessingError (err ) from None
439458
440459 return self .__check_response (method , resp_json )
441460
0 commit comments