Skip to content

Commit

Permalink
✨ Adds method to request number of active rules
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck committed May 30, 2019
1 parent 6fe5345 commit adfd84f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions adguardhome/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ async def disable(self) -> bool:
)
return True

async def rules_count(self) -> int:
"""Return the number of rules loaded."""
response = await self._adguard._request("filtering/status")
count = 0
for filt in response["filters"]:
count += filt["rulesCount"]
return count

async def add_url(self, name: str, url: str) -> bool:
"""Add a new filter subscription to AdGuard Home."""
response = await self._adguard._request(
Expand Down
3 changes: 3 additions & 0 deletions examples/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ async def main(loop):
result = await adguard.stats.replaced_safesearch()
print("Number of enforced safe searches:", result)

result = await adguard.filtering.rules_count()
print("Total number of active rules:", result)


if __name__ == "__main__":
loop = asyncio.get_event_loop()
Expand Down
32 changes: 32 additions & 0 deletions tests/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,38 @@ async def test_disable(event_loop, aresponses):
await adguard.filtering.disable()


@pytest.mark.asyncio
async def test_rules_count(event_loop, aresponses):
"""Test getting rules count of the AdGuard Home filtering."""
aresponses.add(
"example.com:3000",
"/control/filtering/status",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text='{"filters": [{"rulesCount": 99}, {"rulesCount": 1}]}',
),
)
aresponses.add(
"example.com:3000",
"/control/filtering/status",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text='{"filters": []}',
),
)

async with aiohttp.ClientSession(loop=event_loop) as session:
adguard = AdGuardHome("example.com", session=session, loop=event_loop)
result = await adguard.filtering.rules_count()
assert result == 100
result = await adguard.filtering.rules_count()
assert result == 0


@pytest.mark.asyncio
async def test_add_url(event_loop, aresponses):
"""Test add new filter subscription to AdGuard Home filtering."""
Expand Down

0 comments on commit adfd84f

Please sign in to comment.