Repository navigation
feat(client): add AI detection cache and audit log methods #61
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from itertools import islice | ||
| from typing import Iterator, Optional | ||
|
|
||
| from datamasque.client.base import BaseClient | ||
| from datamasque.client.models.audit_log import AuditLog | ||
|
|
||
|
|
||
| class AuditLogClient(BaseClient): | ||
| """Audit log API methods. Mixed into `DataMasqueClient`.""" | ||
|
|
||
| def iter_audit_logs(self) -> Iterator[AuditLog]: | ||
| """ | ||
| Lazily iterate audit log entries, newest first, via the paginated endpoint. | ||
|
|
||
| Requires an admin user. | ||
| """ | ||
|
|
||
| return self._iter_paginated("/api/audit-logs/", model=AuditLog) | ||
|
|
||
| def list_audit_logs(self, *, limit: Optional[int] = None) -> list[AuditLog]: | ||
| """ | ||
| Lists audit log entries, newest first. | ||
|
|
||
| When `limit` is given, returns at most that many of the newest entries, | ||
| and fetches only the pages they are on. | ||
| When it is `None` (the default), returns every entry. | ||
| Requires an admin user. | ||
| """ | ||
|
|
||
| entries = self.iter_audit_logs() | ||
| if limit is None: | ||
| return list(entries) | ||
|
|
||
| return list(islice(entries, limit)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| """Typed response shape for clearing the AI detection cache.""" | ||
|
|
||
| from pydantic import BaseModel, ConfigDict | ||
|
|
||
|
|
||
| class AiDetectionCacheClearResult(BaseModel): | ||
| """Result of `clear_ai_detection_cache`.""" | ||
|
|
||
| model_config = ConfigDict(extra="allow") | ||
|
|
||
| freed_bytes: int | ||
| """The disk space the clear freed, in bytes.""" | ||
|
|
||
| size_bytes: int | ||
| """The size of the cache on disk after the clear, in bytes.""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """Typed response shape for the audit log endpoint.""" | ||
|
|
||
| from datetime import datetime | ||
| from enum import Enum | ||
| from typing import Optional | ||
|
|
||
| from pydantic import BaseModel, ConfigDict | ||
|
|
||
|
|
||
| class AuditLogAction(str, Enum): | ||
| """What the user did in an audited event.""" | ||
|
|
||
| created = "created" | ||
| modified = "modified" | ||
| deleted = "deleted" | ||
| logged_in = "logged_in" | ||
| logged_out = "logged_out" | ||
| started = "started" | ||
| cancelled = "cancelled" | ||
|
|
||
|
|
||
| class AuditLog(BaseModel): | ||
| """One audit log entry.""" | ||
|
|
||
| model_config = ConfigDict(extra="allow") | ||
|
|
||
| id: int | ||
| timestamp: datetime | ||
| username: str | ||
| category: str | ||
| """The area the event belongs to, such as `auth`, `run` or `settings`, or the kind of object it changed.""" | ||
|
|
||
| action: AuditLogAction | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should an unknown |
||
| description: str | ||
| project: Optional[str] = None | ||
| """ | ||
| The project the event belongs to, as `<id>-<slug>`, | ||
| or `None` for an event outside any project or from a server older than 3.26.20. | ||
| """ | ||
|
|
||
| project_name: Optional[str] = None | ||
| """ | ||
| The project's name when the event was logged, | ||
| or `None` for an event outside any project or from a server older than 3.26.20. | ||
| """ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| [project] | ||
| name = "datamasque-python" | ||
| version = "1.5.0" | ||
| version = "1.6.0" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1.6.0 already went out with the projects epic, can this rebase onto main, drop the version bump and add its notes to the unreleased section in HISTORY.rst? |
||
| description = "Official Python client for the DataMasque data-masking API." | ||
| authors = [ | ||
| { name = "DataMasque Ltd" }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| """Tests for `AuditLogClient` (listing audit log entries).""" | ||
|
|
||
| from datetime import datetime, timezone | ||
|
|
||
| import requests_mock | ||
|
|
||
| from datamasque.client.models.audit_log import AuditLog, AuditLogAction | ||
|
|
||
| AUDIT_LOGS_URL = "http://test-server/api/audit-logs/" | ||
|
|
||
|
|
||
| def audit_log_json(id, *, action="created", project=None, project_name=None): | ||
| return { | ||
| "id": id, | ||
| "timestamp": f"2026-10-{id:02d}T09:30:00Z", | ||
| "username": "admin", | ||
| "category": "settings", | ||
| "action": action, | ||
| "description": f"admin made change {id}", | ||
| "project": project, | ||
| "project_name": project_name, | ||
| } | ||
|
|
||
|
|
||
| def test_list_audit_logs_parses_fields(client): | ||
| page = { | ||
| "count": 1, | ||
| "next": None, | ||
| "previous": None, | ||
| "results": [audit_log_json(7, action="deleted", project="3-payments", project_name="Payments")], | ||
| } | ||
| with requests_mock.Mocker() as m: | ||
| m.get(AUDIT_LOGS_URL, json=page, status_code=200) | ||
| audit_logs = client.list_audit_logs() | ||
|
|
||
| assert audit_logs == [ | ||
| AuditLog( | ||
| id=7, | ||
| timestamp=datetime(2026, 10, 7, 9, 30, tzinfo=timezone.utc), | ||
| username="admin", | ||
| category="settings", | ||
| action=AuditLogAction.deleted, | ||
| description="admin made change 7", | ||
| project="3-payments", | ||
| project_name="Payments", | ||
| ) | ||
| ] | ||
| assert audit_logs[0].action is AuditLogAction.deleted | ||
| assert audit_logs[0].action == "deleted" | ||
|
|
||
|
|
||
| def test_list_audit_logs_accepts_entries_without_project_fields(client): | ||
| entry = audit_log_json(1, action="logged_in") | ||
| del entry["project"] | ||
| del entry["project_name"] | ||
| with requests_mock.Mocker() as m: | ||
| m.get(AUDIT_LOGS_URL, json={"count": 1, "next": None, "previous": None, "results": [entry]}) | ||
| audit_logs = client.list_audit_logs() | ||
|
|
||
| assert audit_logs[0].action is AuditLogAction.logged_in | ||
| assert audit_logs[0].project is None | ||
| assert audit_logs[0].project_name is None | ||
|
|
||
|
|
||
| def test_list_audit_logs_follows_pages_newest_first(client): | ||
| page1 = { | ||
| "count": 3, | ||
| "next": f"{AUDIT_LOGS_URL}?limit=100&offset=100", | ||
| "previous": None, | ||
| "results": [audit_log_json(3), audit_log_json(2)], | ||
| } | ||
| page2 = { | ||
| "count": 3, | ||
| "next": None, | ||
| "previous": f"{AUDIT_LOGS_URL}?limit=100", | ||
| "results": [audit_log_json(1)], | ||
| } | ||
| with requests_mock.Mocker() as m: | ||
| m.get(AUDIT_LOGS_URL, [{"json": page1, "status_code": 200}, {"json": page2, "status_code": 200}]) | ||
| audit_logs = client.list_audit_logs() | ||
|
|
||
| assert [audit_log.id for audit_log in audit_logs] == [3, 2, 1] | ||
| assert m.request_history[0].qs == {"limit": ["100"], "offset": ["0"]} | ||
| assert m.request_history[1].qs == {"limit": ["100"], "offset": ["100"]} | ||
|
|
||
|
|
||
| def test_list_audit_logs_with_limit_stops_fetching_pages(client): | ||
| page1 = { | ||
| "count": 3, | ||
| "next": f"{AUDIT_LOGS_URL}?limit=100&offset=100", | ||
| "previous": None, | ||
| "results": [audit_log_json(3), audit_log_json(2)], | ||
| } | ||
| with requests_mock.Mocker() as m: | ||
| m.get(AUDIT_LOGS_URL, json=page1, status_code=200) | ||
| audit_logs = client.list_audit_logs(limit=1) | ||
|
|
||
| assert [audit_log.id for audit_log in audit_logs] == [3] | ||
| assert m.call_count == 1 | ||
|
|
||
|
|
||
| def test_list_audit_logs_with_zero_limit_sends_no_request(client): | ||
| with requests_mock.Mocker() as m: | ||
| audit_logs = client.list_audit_logs(limit=0) | ||
|
|
||
| assert audit_logs == [] | ||
| assert m.call_count == 0 | ||
|
|
||
|
|
||
| def test_iter_audit_logs_is_lazy(client): | ||
| page1 = { | ||
| "count": 2, | ||
| "next": f"{AUDIT_LOGS_URL}?limit=100&offset=100", | ||
| "previous": None, | ||
| "results": [audit_log_json(2)], | ||
| } | ||
| with requests_mock.Mocker() as m: | ||
| m.get(AUDIT_LOGS_URL, json=page1, status_code=200) | ||
| newest = next(client.iter_audit_logs()) | ||
|
|
||
| assert newest.id == 2 | ||
| assert m.call_count == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worth taking a
projectfilter? The audit log endpoint has accepted?project=since 3.26.22