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

feat: added settings endpoints #147

Merged
merged 1 commit into from
Oct 19, 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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ install:
cp misc/aw-server.service /usr/lib/systemd/user/aw-server.service

test:
@# Note that extensive integration tests are also run in the bundle repo,
@# for both aw-server and aw-server-rust, but without code coverage.
python -c 'import aw_server'
python -m pytest tests/test_server.py

Expand Down
11 changes: 11 additions & 0 deletions aw_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from .__about__ import __version__
from .exceptions import NotFound
from .settings import Settings

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -50,6 +51,7 @@ def g(self, bucket_id, *args, **kwargs):
class ServerAPI:
def __init__(self, db, testing) -> None:
self.db = db
self.settings = Settings(testing)
self.testing = testing
self.last_event = {} # type: dict

Expand Down Expand Up @@ -354,3 +356,12 @@ def get_log(self):
for line in log_file.readlines()[::-1]:
payload.append(json.loads(line))
return payload, 200

def get_setting(self, key):
"""Get a setting"""
return self.settings.get(key, None)

def set_setting(self, key, value):
"""Set a setting"""
self.settings[key] = value
return value
17 changes: 17 additions & 0 deletions aw_server/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,20 @@ class LogResource(Resource):
@copy_doc(ServerAPI.get_log)
def get(self):
return current_app.api.get_log(), 200


# SETTINGS


@api.route("/0/settings", defaults={"key": ""})
@api.route("/0/settings/<string:key>")
class SettingsResource(Resource):
def get(self, key: str):
data = current_app.api.get_setting(key)
return jsonify(data)

def post(self, key: str):
if not key:
raise BadRequest("MissingParameter", "Missing required parameter key")
data = current_app.api.set_setting(key, request.get_json())
return data
41 changes: 41 additions & 0 deletions aw_server/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json
from pathlib import Path

from aw_core.dirs import get_config_dir


class Settings:
def __init__(self, testing: bool):
filename = "settings.json" if not testing else "settings-testing.json"
self.config_file = Path(get_config_dir("aw-server")) / filename
self.load()

def __getitem__(self, key):
return self.get(key)

def __setitem__(self, key, value):
return self.set(key, value)

def load(self):
if self.config_file.exists():
with open(self.config_file) as f:
self.data = json.load(f)
else:
self.data = {}

def save(self):
with open(self.config_file, "w") as f:
json.dump(self.data, f, indent=4)

def get(self, key: str, default=None):
if not key:
return self.data
return self.data.get(key, default)

def set(self, key, value):
if value:
self.data[key] = value
else:
if key in self.data:
del self.data[key]
self.save()
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

import pytest
from aw_client import ActivityWatchClient
from aw_server.server import AWFlask

logging.basicConfig(level=logging.WARN)
Expand All @@ -14,3 +15,18 @@ def app():
@pytest.fixture(scope="session")
def flask_client(app):
yield app.test_client()


@pytest.fixture(scope="session")
def aw_client():
# TODO: Could it be possible to write a sisterclass of ActivityWatchClient
# which calls aw_server.api directly? Would it be of use? Would add another
# layer of integration tests that are actually more like unit tests.
c = ActivityWatchClient("aw-client-test", testing=True)
yield c

# Delete test buckets after all tests needing the fixture have been run
buckets = c.get_buckets()
for bucket_id in buckets:
if bucket_id.startswith("test-"):
c.delete_bucket(bucket_id)
28 changes: 9 additions & 19 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import logging
from datetime import datetime, timezone, timedelta
import random
from time import sleep
from datetime import datetime, timedelta, timezone
from pprint import pprint
from time import sleep

import pytest

from aw_core.models import Event
from aw_client import ActivityWatchClient

logging.basicConfig(level=logging.WARN)

Expand All @@ -16,21 +14,6 @@
# layer of integration tests that are actually more like unit tests.


@pytest.fixture(scope="session")
def aw_client():
# TODO: Could it be possible to write a sisterclass of ActivityWatchClient
# which calls aw_server.api directly? Would it be of use? Would add another
# layer of integration tests that are actually more like unit tests.
c = ActivityWatchClient("client-test", testing=True)
yield c

# Delete test buckets after all tests needing the fixture have been run
buckets = c.get_buckets()
for bucket_id in buckets:
if bucket_id.startswith("test-"):
c.delete_bucket(bucket_id)


@pytest.fixture(scope="function")
def bucket(aw_client):
bucket_id = "test-" + str(random.randint(0, 10**5))
Expand Down Expand Up @@ -285,3 +268,10 @@ def test_midnight_heartbeats(aw_client, bucket):
)
pprint(recv_events_after_midnight)
assert len(recv_events_after_midnight) == int(len(recv_events_merged) / 2)


def test_settings(aw_client):
aw_client.set_setting("test", "test")
assert aw_client.get_setting("test") == "test"
assert aw_client.get_setting("test2") is None
assert aw_client.get_setting() == {"test": "test"}
Loading