60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
"""
|
|
Revision operations for CodiMD CLI.
|
|
"""
|
|
|
|
import json
|
|
from typing import List, Dict, Any
|
|
from datetime import datetime
|
|
|
|
from .client import CodiMDClient
|
|
|
|
|
|
class RevisionManager:
|
|
"""Manager for revision operations."""
|
|
|
|
def __init__(self, client: CodiMDClient):
|
|
self.client = client
|
|
|
|
def list_revisions(self, note_id: str) -> List[Dict[str, Any]]:
|
|
"""List all revisions for a note."""
|
|
response = self.client.get(f"/{note_id}/revision")
|
|
|
|
if response.status_code == 404:
|
|
raise FileNotFoundError(f"Note not found: {note_id}")
|
|
if response.status_code == 403:
|
|
raise PermissionError("No permission to view revisions")
|
|
if response.status_code != 200:
|
|
raise Exception(f"Failed to list revisions: {response.status_code}")
|
|
|
|
try:
|
|
data = response.json()
|
|
return data.get("revisions", [])
|
|
except:
|
|
return []
|
|
|
|
def get_revision_at_time(self, note_id: str, timestamp: int) -> Dict[str, Any]:
|
|
"""Get note content at specific revision time."""
|
|
response = self.client.get(
|
|
f"/{note_id}/revision/{timestamp}",
|
|
allow_redirects=True
|
|
)
|
|
|
|
if response.status_code == 404:
|
|
raise FileNotFoundError(f"Note or revision not found")
|
|
if response.status_code != 200:
|
|
raise Exception(f"Failed to get revision: {response.status_code}")
|
|
|
|
try:
|
|
return response.json()
|
|
except:
|
|
return {"note_id": note_id, "timestamp": timestamp, "content": response.text}
|
|
|
|
def get_latest_revision(self, note_id: str) -> Dict[str, Any]:
|
|
"""Get the latest revision of a note."""
|
|
revisions = self.list_revisions(note_id)
|
|
if not revisions:
|
|
raise FileNotFoundError(f"No revisions found for note: {note_id}")
|
|
|
|
latest = revisions[0]
|
|
return self.get_revision_at_time(note_id, latest.get("time", 0))
|