From b5cd50012124d698b225fb593b65ef922a7d51ab Mon Sep 17 00:00:00 2001 From: Ali ihsan Cengiz Date: Wed, 18 Mar 2026 13:09:10 +0100 Subject: [PATCH] Add logs endpoint for snaps --- snap_http/__init__.py | 1 + snap_http/api/__init__.py | 1 + snap_http/api/snaps.py | 12 ++++++++++++ tests/unit/api/test_snaps.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/snap_http/__init__.py b/snap_http/__init__.py index 94ffc6c..42f26d3 100644 --- a/snap_http/__init__.py +++ b/snap_http/__init__.py @@ -23,6 +23,7 @@ switch_all, unhold, unhold_all, + logs, list, list_all, get_conf, diff --git a/snap_http/api/__init__.py b/snap_http/api/__init__.py index 844551d..56efc35 100644 --- a/snap_http/api/__init__.py +++ b/snap_http/api/__init__.py @@ -49,6 +49,7 @@ switch_all, unhold, unhold_all, + logs, ) from .snapshots import forget_snapshot, save_snapshot, snapshots from .systems import ( diff --git a/snap_http/api/snaps.py b/snap_http/api/snaps.py index b6ef228..1dad8e3 100644 --- a/snap_http/api/snaps.py +++ b/snap_http/api/snaps.py @@ -261,3 +261,15 @@ def list_all() -> SnapdResponse: """GETs a list of all installed snaps including disabled ones. """ return http.get("/snaps?select=all") + + +def logs(names: List[str], entries: int = 10) -> SnapdResponse: + """GETs snap logs + """ + query_params: Dict[str, Union[str, int]] = { + } + + if names is not None: + query_params["names"] = ",".join(names) + query_params["n"] = entries + return http.get("/logs", query_params=query_params) diff --git a/tests/unit/api/test_snaps.py b/tests/unit/api/test_snaps.py index 032aa3c..daed57e 100644 --- a/tests/unit/api/test_snaps.py +++ b/tests/unit/api/test_snaps.py @@ -926,3 +926,32 @@ def mock_get(path): result = api.list_all() assert result == mock_response + +@pytest.mark.parametrize( + ("names", "entries", "expected_params"), + [ + (None, 10, {"n":10}), + (["snapd", "core24"], 10, {"names": "snapd,core24", "n": 10}), + (["snapd", "core24"], 100, {"names": "snapd,core24", "n": 100}), + ] +) +def test_logs(monkeypatch, names, entries, expected_params): + """`api.logs` returns a `types.SnapdResponse`.""" + mock_response = types.SnapdResponse( + type="sync", + status_code=200, + status="OK", + result=[{"title": "placeholder1"}, {"title": "placeholder2"}], + ) + + def mock_get(path, query_params): + assert path == "/logs" + assert query_params == expected_params + + return mock_response + + monkeypatch.setattr(http, "get", mock_get) + + result = api.logs(names, entries) + + assert result == mock_response