Skip to content

Commit

Permalink
Add possibility to change logs location (#70)
Browse files Browse the repository at this point in the history
* Add possibility to change logs location

* Unit tests
  • Loading branch information
bieniu committed Nov 24, 2022
1 parent 959401a commit ae5184b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ async def main():
# allowed values are: 1, 6, 24, 168, 720, 960, 4320, 8760, 17520
# await nextdns.set_logs_retention(profile_id, 720)

# set logs location to Switzerland
# allowed values are: ch, eu, gb, us
# await nextdns.set_logs_location(profile_id, "ch")

# clear logs
# await nextdns.clear_logs(profile_id)

Expand Down
17 changes: 17 additions & 0 deletions nextdns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
from aiohttp import ClientSession

from .const import (
ALLOWED_LOG_LOCATION,
ALLOWED_LOG_RETENTION,
ATTR_ANALYTICS,
ATTR_CLEAR_LOGS,
ATTR_ENABLED,
ATTR_GET_LOGS,
ATTR_LOCATION,
ATTR_LOGS,
ATTR_LOGS_LOCATION,
ATTR_LOGS_RETENTION,
ATTR_PARENTAL_CONTROL_CATEGORIES,
ATTR_PARENTAL_CONTROL_SERVICES,
Expand Down Expand Up @@ -122,6 +125,7 @@ async def get_settings(self, profile_id: str) -> Settings:
],
anonymized_ecs=profile_data.settings[ATTR_PERFORMANCE][ApiNames.ECS],
logs=profile_data.settings[ATTR_LOGS][ATTR_ENABLED],
logs_location=profile_data.settings[ATTR_LOGS][ATTR_LOCATION],
logs_retention=profile_data.settings[ATTR_LOGS][ATTR_RETENTION],
web3=profile_data.settings[ATTR_WEB3],
allow_affiliate=profile_data.privacy[ApiNames.ALLOW_AFFILIATE],
Expand Down Expand Up @@ -279,6 +283,19 @@ async def get_all_analytics(self, profile_id: str) -> AllAnalytics:

return AllAnalytics(*resp)

async def set_logs_location(self, profile_id: str, location: str) -> bool:
"""Set logs location."""
if location not in ALLOWED_LOG_LOCATION:
raise ValueError(
f"Invalid logs location value. Allowed values are: {ALLOWED_LOG_LOCATION}"
)

url = MAP_SETTING[ATTR_LOGS_LOCATION].url.format(profile_id=profile_id)
name = MAP_SETTING[ATTR_LOGS_LOCATION].name
result = await self._http_request("patch", url, data={name: location})

return result.get("success", False) is True

async def set_logs_retention(self, profile_id: str, hours: int) -> bool:
"""Set logs retention."""
if hours not in ALLOWED_LOG_RETENTION:
Expand Down
5 changes: 5 additions & 0 deletions nextdns/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
ATTR_CACHE_BOOST = "cache_boost"
ATTR_CNAME_FLATTENING = "cname_flattening"
ATTR_LOGS = "logs"
ATTR_LOGS_LOCATION = "logs_location"
ATTR_LOGS_RETENTION = "logs_retention"
ATTR_RETENTION = "retention"
ATTR_LOCATION = "location"
ATTR_WEB3 = "web3"

ATTR_ALLOW_AFFILIATE = "allow_affiliate"
Expand Down Expand Up @@ -77,6 +79,7 @@
ATTR_PARENTAL_CONTROL_SERVICE: "https://api.nextdns.io/profiles/{profile_id}/parentalControl/services/{service}",
ATTR_PARENTAL_CONTROL_SERVICES: "https://api.nextdns.io/profiles/{profile_id}/parentalControl/services",
ATTR_LOGS: "https://api.nextdns.io/profiles/{profile_id}/settings/logs",
ATTR_LOGS_LOCATION: "https://api.nextdns.io/profiles/{profile_id}/settings/logs/location",
ATTR_LOGS_RETENTION: "https://api.nextdns.io/profiles/{profile_id}/settings/logs/retention",
ATTR_BLOCK_PAGE: "https://api.nextdns.io/profiles/{profile_id}/settings/blockPage",
}
Expand Down Expand Up @@ -111,6 +114,7 @@
ATTR_ANONYMIZED_ECS: SettingDescription(ENDPOINTS[ATTR_PERFORMANCE], ApiNames.ECS),
ATTR_WEB3: SettingDescription(ENDPOINTS[ATTR_SETTINGS], ATTR_WEB3),
ATTR_LOGS: SettingDescription(ENDPOINTS[ATTR_LOGS], ATTR_ENABLED),
ATTR_LOGS_LOCATION: SettingDescription(ENDPOINTS[ATTR_LOGS], ATTR_LOCATION),
ATTR_LOGS_RETENTION: SettingDescription(ENDPOINTS[ATTR_LOGS], ATTR_RETENTION),
ATTR_ALLOW_AFFILIATE: SettingDescription(
ENDPOINTS[ATTR_PRIVACY], ApiNames.ALLOW_AFFILIATE
Expand Down Expand Up @@ -286,4 +290,5 @@
),
}

ALLOWED_LOG_LOCATION = ("ch", "eu", "gb", "us")
ALLOWED_LOG_RETENTION = (1, 6, 24, 168, 720, 960, 4320, 8760, 17520)
1 change: 1 addition & 0 deletions nextdns/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class Settings(NextDnsData):
cname_flattening: bool
anonymized_ecs: bool
logs: bool
logs_location: str
logs_retention: int
web3: bool

Expand Down
48 changes: 48 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,51 @@ async def test_set_logs_retention_with_invalid_value():
"Invalid logs retention value. Allowed values are: (1, 6, 24, 168, 720, 960, 4320, 8760, 17520)"
in str(exc.value)
)


@pytest.mark.asyncio
async def test_set_logs_location():
"""Test set_logs_location() method."""
with open("tests/fixtures/profiles.json", encoding="utf-8") as file:
profiles_data = json.load(file)

session = aiohttp.ClientSession()

with aioresponses() as session_mock:
session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)
session_mock.patch(
ENDPOINTS[ATTR_LOGS].format(profile_id=PROFILE_ID),
status=HTTPStatus.NO_CONTENT.value,
)

nextdns = await NextDns.create(session, "fakeapikey")

result = await nextdns.set_logs_location(PROFILE_ID, "us")

await session.close()

assert result is True


@pytest.mark.asyncio
async def test_set_logs_location_with_invalid_value():
"""Test set_logs_location() method with invalid value."""
with open("tests/fixtures/profiles.json", encoding="utf-8") as file:
profiles_data = json.load(file)

session = aiohttp.ClientSession()

with aioresponses() as session_mock:
session_mock.get(ENDPOINTS[ATTR_PROFILES], payload=profiles_data)

nextdns = await NextDns.create(session, "fakeapikey")

await session.close()

with pytest.raises(ValueError) as exc:
await nextdns.set_logs_location(PROFILE_ID, "pl")

assert (
"Invalid logs location value. Allowed values are: ('ch', 'eu', 'gb', 'us')"
in str(exc.value)
)

0 comments on commit ae5184b

Please sign in to comment.