From 082e02b7230ebd5595e1442984b7e83f355c3d73 Mon Sep 17 00:00:00 2001 From: David McKee Date: Tue, 12 Dec 2023 12:39:05 +0000 Subject: [PATCH] A stab at a getter --- smoketest/smoketest.py | 3 ++- src/caselawclient/Client.py | 13 +++++++++++++ src/caselawclient/models/history.py | 5 +++++ src/caselawclient/xquery_type_dicts.py | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/smoketest/smoketest.py b/smoketest/smoketest.py index 712baf1e..31a83c93 100644 --- a/smoketest/smoketest.py +++ b/smoketest/smoketest.py @@ -51,4 +51,5 @@ def test_append_history(): api_client.append_history( URI, HistoryEvent({"id": "1"}, ["flag"], "1") ) - # assert api_client.get_history() ... + event = api_client.get_history(URI)[-1] + assert event.attributes["id"] == "1" diff --git a/src/caselawclient/Client.py b/src/caselawclient/Client.py index 56c35164..b596ca8d 100644 --- a/src/caselawclient/Client.py +++ b/src/caselawclient/Client.py @@ -11,6 +11,7 @@ from xml.etree.ElementTree import Element import environ +import lxml.etree import requests from requests.auth import HTTPBasicAuth from requests.structures import CaseInsensitiveDict @@ -743,6 +744,7 @@ def original_judgment_transformation( ) def get_property(self, judgment_uri: DocumentURIString, name: str) -> str: + """This only gets the text of a property""" uri = self._format_uri_for_marklogic(judgment_uri) vars: query_dicts.GetPropertyDict = { "uri": uri, @@ -963,3 +965,14 @@ def append_history(self, uri: DocumentURIString, history: HistoryEvent) -> None: "payload": history.payload, } self._send_to_eval(vars, "append_history.xqy") + + def get_history(self, uri: DocumentURIString) -> list[HistoryEvent]: + formatted_uri = self._format_uri_for_marklogic(uri) + vars: query_dicts.GetHistoryDict = {"uri": formatted_uri} + response = self._send_to_eval(vars, "get_history.xqy") + bytes = get_single_bytestring_from_marklogic_response(response) + breakpoint() + events = [] + for event in lxml.etree.fromstring(bytes).xpath("/history/event"): + events.append(HistoryEvent.from_xml(event)) + return events diff --git a/src/caselawclient/models/history.py b/src/caselawclient/models/history.py index 18f99614..2a449f04 100644 --- a/src/caselawclient/models/history.py +++ b/src/caselawclient/models/history.py @@ -16,3 +16,8 @@ def __init__( self.payload = lxml.etree.tostring(payload) else: self.payload = payload + + @classmethod + def from_xml(cls, element: lxml.etree._Element) -> "HistoryEvent": + flags = [a for a in element.attrib.keys() if element.attrib[a] == "true"] + return cls(element.attrib, flags, element) diff --git a/src/caselawclient/xquery_type_dicts.py b/src/caselawclient/xquery_type_dicts.py index a60b14a9..7a7a5bc5 100644 --- a/src/caselawclient/xquery_type_dicts.py +++ b/src/caselawclient/xquery_type_dicts.py @@ -63,6 +63,11 @@ class DocumentExistsDict(MarkLogicAPIDict): uri: MarkLogicDocumentURIString +# get_history.xqy +class GetHistoryDict(MarkLogicAPIDict): + uri: MarkLogicDocumentURIString + + # get_judgment.xqy class GetJudgmentDict(MarkLogicAPIDict): show_unpublished: Optional[bool]