Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dataclasses to clean up a little #679

Merged
merged 1 commit into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/adguardhome/filtering.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Asynchronous Python client for the AdGuard Home API."""
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

from .exceptions import AdGuardHomeError
Expand All @@ -9,16 +10,11 @@
from . import AdGuardHome


@dataclass
class AdGuardHomeFiltering:
"""Controls AdGuard Home filtering. Blocks domains."""

def __init__(self, adguard: AdGuardHome) -> None:
"""Initialize object.

Args:
adguard: The AdGuard Home instance.
"""
self._adguard = adguard
adguard: AdGuardHome

async def _config(
self, *, enabled: bool | None = None, interval: int | None = None
Expand All @@ -34,7 +30,7 @@ async def _config(
if interval is None:
interval = await self.interval()

await self._adguard.request(
await self.adguard.request(
"filtering/config",
method="POST",
json_data={"enabled": enabled, "interval": interval},
Expand All @@ -46,7 +42,7 @@ async def enabled(self) -> bool:
Returns:
The current state of the AdGuard Home filtering.
"""
response = await self._adguard.request("filtering/status")
response = await self.adguard.request("filtering/status")
return response["enabled"]

async def enable(self) -> None:
Expand Down Expand Up @@ -88,7 +84,7 @@ async def interval(self, *, interval: int | None = None) -> int:
await self._config(interval=interval)
return interval

response = await self._adguard.request("filtering/status")
response = await self.adguard.request("filtering/status")
return response["interval"]

async def rules_count(self, *, allowlist: bool) -> int:
Expand All @@ -101,7 +97,7 @@ async def rules_count(self, *, allowlist: bool) -> int:
The number of filtering rules currently loaded in the AdGuard
Home instance.
"""
response = await self._adguard.request("filtering/status")
response = await self.adguard.request("filtering/status")

count = "whitelist_filters" if allowlist else "filters"
if not response.get(count):
Expand All @@ -121,7 +117,7 @@ async def add_url(self, *, allowlist: bool, name: str, url: str) -> None:
AdGuardHomeError: Failed adding the filter subscription.
"""
try:
await self._adguard.request(
await self.adguard.request(
"filtering/add_url",
method="POST",
json_data={"whitelist": allowlist, "name": name, "url": url},
Expand All @@ -142,7 +138,7 @@ async def remove_url(self, *, allowlist: bool, url: str) -> None:
AdGuardHomeError: Failed removing the filter subscription.
"""
try:
await self._adguard.request(
await self.adguard.request(
"filtering/remove_url",
method="POST",
json_data={"whitelist": allowlist, "url": url},
Expand All @@ -162,7 +158,7 @@ async def enable_url(self, *, allowlist: bool, url: str) -> None:
Raises:
AdGuardHomeError: Failed enabling filter subscription.
"""
response = await self._adguard.request("filtering/status")
response = await self.adguard.request("filtering/status")
filter_type = "whitelist_filters" if allowlist else "filters"

# Excluded from coverage:
Expand All @@ -177,7 +173,7 @@ async def enable_url(self, *, allowlist: bool, url: str) -> None:
)

try:
await self._adguard.request(
await self.adguard.request(
"filtering/set_url",
method="POST",
json_data={
Expand All @@ -201,7 +197,7 @@ async def disable_url(self, *, allowlist: bool, url: str) -> None:
Raises:
AdGuardHomeError: Failed disabling filter subscription.
"""
response = await self._adguard.request("filtering/status")
response = await self.adguard.request("filtering/status")
filter_type = "whitelist_filters" if allowlist else "filters"

# Excluded from coverage:
Expand All @@ -216,7 +212,7 @@ async def disable_url(self, *, allowlist: bool, url: str) -> None:
)

try:
await self._adguard.request(
await self.adguard.request(
"filtering/set_url",
method="POST",
json_data={
Expand All @@ -243,7 +239,7 @@ async def refresh(self, *, allowlist: bool, force: bool = False) -> None:
force_value = "true" if force else "false"

try:
await self._adguard.request(
await self.adguard.request(
"filtering/refresh",
method="POST",
json_data={"whitelist": allowlist},
Expand Down
16 changes: 6 additions & 10 deletions src/adguardhome/parental.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Asynchronous Python client for the AdGuard Home API."""
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

from .exceptions import AdGuardHomeError
Expand All @@ -9,24 +10,19 @@
from . import AdGuardHome


@dataclass
class AdGuardHomeParental:
"""Controls AdGuard Home parental control."""

def __init__(self, adguard: AdGuardHome) -> None:
"""Initialize object.

Args:
adguard: The AdGuard Home instance.
"""
self._adguard = adguard
adguard: AdGuardHome

async def enabled(self) -> bool:
"""Return if AdGuard Home parental control is enabled or not.

Returns:
The current state of the AdGuard Home parental control.
"""
response = await self._adguard.request("parental/status")
response = await self.adguard.request("parental/status")
return response["enabled"]

async def enable(self) -> None:
Expand All @@ -36,7 +32,7 @@ async def enable(self) -> None:
AdGuardHomeError: If enabling parental control failed.
"""
try:
await self._adguard.request(
await self.adguard.request(
"parental/enable", method="POST", data="sensitivity=TEEN"
)
except AdGuardHomeError as exception:
Expand All @@ -51,7 +47,7 @@ async def disable(self) -> None:
AdGuardHomeError: If disabling parental control failed.
"""
try:
await self._adguard.request("parental/disable", method="POST")
await self.adguard.request("parental/disable", method="POST")
except AdGuardHomeError as exception:
raise AdGuardHomeError(
"Disabling AdGuard Home parental control failed"
Expand Down
16 changes: 6 additions & 10 deletions src/adguardhome/querylog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Asynchronous Python client for the AdGuard Home API."""
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

from .exceptions import AdGuardHomeError
Expand All @@ -9,16 +10,11 @@
from . import AdGuardHome


@dataclass
class AdGuardHomeQueryLog:
"""Controls AdGuard Home query log."""

def __init__(self, adguard: AdGuardHome) -> None:
"""Initialize object.

Args:
adguard: The AdGuard Home instance.
"""
self._adguard = adguard
adguard: AdGuardHome

async def _config(self, enabled: bool | None = None, interval: int | None = None):
"""Configure query log on AdGuard Home.
Expand All @@ -31,7 +27,7 @@ async def _config(self, enabled: bool | None = None, interval: int | None = None
enabled = await self.enabled()
if interval is None:
interval = await self.interval()
await self._adguard.request(
await self.adguard.request(
"querylog_config",
method="POST",
json_data={"enabled": enabled, "interval": interval},
Expand All @@ -43,7 +39,7 @@ async def enabled(self) -> bool:
Returns:
The current state of the AdGuard Home query log.
"""
response = await self._adguard.request("querylog_info")
response = await self.adguard.request("querylog_info")
return response["enabled"]

async def enable(self) -> None:
Expand Down Expand Up @@ -72,7 +68,7 @@ async def interval(self, interval: int | None = None) -> int:
await self._config(interval=interval)
return interval

response = await self._adguard.request("querylog_info")
response = await self.adguard.request("querylog_info")
return response["interval"]

async def disable(self) -> None:
Expand Down
16 changes: 6 additions & 10 deletions src/adguardhome/safebrowsing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Asynchronous Python client for the AdGuard Home API."""
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

from .exceptions import AdGuardHomeError
Expand All @@ -9,24 +10,19 @@
from . import AdGuardHome


@dataclass
class AdGuardHomeSafeBrowsing:
"""Controls AdGuard Home browsing security."""

def __init__(self, adguard: AdGuardHome) -> None:
"""Initialize object.

Args:
adguard: The AdGuard Home instance.
"""
self._adguard = adguard
adguard: AdGuardHome

async def enabled(self) -> bool:
"""Return if AdGuard Home browsing security is enabled or not.

Returns:
The current state of the AdGuard safe browsing feature.
"""
response = await self._adguard.request("safebrowsing/status")
response = await self.adguard.request("safebrowsing/status")
return response["enabled"]

async def enable(self) -> None:
Expand All @@ -36,7 +32,7 @@ async def enable(self) -> None:
AdGuardHomeError: If enabling the safe browsing didn't succeed.
"""
try:
await self._adguard.request("safebrowsing/enable", method="POST")
await self.adguard.request("safebrowsing/enable", method="POST")
except AdGuardHomeError as exception:
raise AdGuardHomeError(
"Enabling AdGuard Home safe browsing failed"
Expand All @@ -49,7 +45,7 @@ async def disable(self) -> None:
AdGuardHomeError: If disabling the safe browsing didn't succeed.
"""
try:
await self._adguard.request("safebrowsing/disable", method="POST")
await self.adguard.request("safebrowsing/disable", method="POST")
except AdGuardHomeError as exception:
raise AdGuardHomeError(
"Disabling AdGuard Home safe browsing failed"
Expand Down
16 changes: 6 additions & 10 deletions src/adguardhome/safesearch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Asynchronous Python client for the AdGuard Home API."""
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

from .exceptions import AdGuardHomeError
Expand All @@ -9,24 +10,19 @@
from . import AdGuardHome


@dataclass
class AdGuardHomeSafeSearch:
"""Controls AdGuard Home safe search enforcing."""

def __init__(self, adguard: AdGuardHome) -> None:
"""Initialize object.

Args:
adguard: The AdGuard Home instance.
"""
self._adguard = adguard
adguard: AdGuardHome

async def enabled(self) -> bool:
"""Return if AdGuard Home safe search enforcing is enabled or not.

Returns:
The current state of the AdGuard Home safe search.
"""
response = await self._adguard.request("safesearch/status")
response = await self.adguard.request("safesearch/status")
return response["enabled"]

async def enable(self) -> None:
Expand All @@ -36,7 +32,7 @@ async def enable(self) -> None:
AdGuardHomeError: If enabling the safe search didn't succeed.
"""
try:
await self._adguard.request("safesearch/enable", method="POST")
await self.adguard.request("safesearch/enable", method="POST")
except AdGuardHomeError as exception:
raise AdGuardHomeError(
"Enabling AdGuard Home safe search failed"
Expand All @@ -49,7 +45,7 @@ async def disable(self) -> None:
AdGuardHomeError: If disabling the safe search didn't succeed.
"""
try:
await self._adguard.request("safesearch/disable", method="POST")
await self.adguard.request("safesearch/disable", method="POST")
except AdGuardHomeError as exception:
raise AdGuardHomeError(
"Disabling AdGuard Home safe search failed"
Expand Down
Loading