Debian Codegraph @

src/adguardhome/safebrowsing.py

raw
1 """Asynchronous Python client for the AdGuard Home API."""
2
3 from __future__ import annotations
4
5 from dataclasses import dataclass
6 from typing import TYPE_CHECKING
7
8 from .exceptions import AdGuardHomeError
9
10 if TYPE_CHECKING:
11 from . import AdGuardHome
12
13
14 @dataclass
15 class AdGuardHomeSafeBrowsing:
16 """Controls AdGuard Home browsing security."""
17
18 adguard: AdGuardHome
19
20 async def enabled(self) -> bool:
21 """Return if AdGuard Home browsing security is enabled or not.
22
23 Returns
24 -------
25 The current state of the AdGuard safe browsing feature.
26
27 """
28 response = await self.adguard.request("safebrowsing/status")
29 return response["enabled"]
30
31 async def enable(self) -> None:
32 """Enable AdGuard Home browsing security.
33
34 Raises
35 ------
36 AdGuardHomeError: If enabling the safe browsing didn't succeed.
37
38 """
39 try:
40 await self.adguard.request("safebrowsing/enable", method="POST")
41 except AdGuardHomeError as exception:
42 msg = "Enabling AdGuard Home safe browsing failed"
43 raise AdGuardHomeError(msg) from exception
44
45 async def disable(self) -> None:
46 """Disable AdGuard Home browsing security.
47
48 Raises
49 ------
50 AdGuardHomeError: If disabling the safe browsing didn't succeed.
51
52 """
53 try:
54 await self.adguard.request("safebrowsing/disable", method="POST")
55 except AdGuardHomeError as exception:
56 msg = "Disabling AdGuard Home safe browsing failed"
57 raise AdGuardHomeError(msg) from exception