Skip to content

Commit

Permalink
Merge pull request #664 from nationalarchives/FCL-269-deprecate-xml-t…
Browse files Browse the repository at this point in the history
…ools-in-api-client

Remove deprecated xml_tools module
  • Loading branch information
jacksonj04 authored Sep 10, 2024
2 parents db32b1e + 3eb5f9d commit 4944026
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 473 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog 1.0.0].
## Unreleased

- Multiple stylistic improvements, and enabling ruff to allow us to keep standards up in future
- **Breaking:** Remove xml_tools

## [Release 24.0.1]

Expand Down
23 changes: 20 additions & 3 deletions src/caselawclient/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path
from typing import Any, Optional, Type, Union
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import Element, ParseError, fromstring

import environ
import requests
Expand All @@ -34,7 +34,6 @@
MarkLogicPrivilegeURIString,
)

from . import xml_tools
from .content_hash import validate_content_hash
from .errors import (
DocumentNotFoundError,
Expand Down Expand Up @@ -250,6 +249,23 @@ def _get_error_code_class(self, error_code: str) -> Type[MarklogicAPIError]:
def _path_to_request_url(self, path: str) -> str:
return f"{self.base_url}/{path.lstrip('/')}"

@classmethod
def _get_error_code(cls, content_as_xml: Optional[str]) -> str:
logging.warning(
"XMLTools is deprecated and will be removed in later versions. "
"Use methods from MarklogicApiClient.Client instead.",
)
if not content_as_xml:
return "Unknown error, Marklogic returned a null or empty response"
try:
xml = fromstring(content_as_xml)
return xml.find(
"message-code",
namespaces={"": "http://marklogic.com/xdmp/error"},
).text # type: ignore
except (ParseError, TypeError, AttributeError):
return "Unknown error, Marklogic returned a null or empty response"

def _raise_for_status(self, response: requests.Response) -> None:
try:
response.raise_for_status()
Expand All @@ -268,7 +284,8 @@ def _raise_for_status(self, response: requests.Response) -> None:

if new_error_class == self.default_http_error_class:
# Attempt to decode the error code from the response
error_code = xml_tools.get_error_code(response.content.decode("utf-8"))

error_code = self._get_error_code(response.content.decode("utf-8"))

new_error_class = self._get_error_code_class(error_code)

Expand Down
129 changes: 0 additions & 129 deletions src/caselawclient/xml_tools.py

This file was deleted.

48 changes: 48 additions & 0 deletions tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,51 @@ def test_user_agent(self):
Request("GET", "http://example.invalid"),
).headers["user-agent"]
assert re.match(r"^ds-caselaw-marklogic-api-client/\d+", user_agent)

def test_get_error_code(self):
xml_string = """
<error-response xmlns="http://marklogic.com/xdmp/error">
<status-code>500</status-code>
<status>Internal Server Error</status>
<message-code>XDMP-DOCNOTFOUND</message-code>
<message>
XDMP-DOCNOTFOUND:
xdmp:document-get-properties("/a/fake/judgment/.xml", fn:QName("","published"))
-- Document not found
</message>
<message-detail>
<message-title>Document not found</message-title>
</message-detail>
</error-response>
"""
result = self.client._get_error_code(xml_string)
self.assertEqual(result, "XDMP-DOCNOTFOUND")

def test_get_error_code_missing_message(self):
xml_string = """
<error-response xmlns="http://marklogic.com/xdmp/error">
<status-code>500</status-code>
<status>Internal Server Error</status>
</error-response>
"""
result = self.client._get_error_code(xml_string)
self.assertEqual(
result,
"Unknown error, Marklogic returned a null or empty response",
)

def test_get_error_code_xml_empty_string(self):
xml_string = ""
result = self.client._get_error_code(xml_string)
self.assertEqual(
result,
"Unknown error, Marklogic returned a null or empty response",
)

def test_get_error_code_xml_none(self):
xml_string = None
result = self.client._get_error_code(xml_string)
self.assertEqual(
result,
"Unknown error, Marklogic returned a null or empty response",
)
Loading

0 comments on commit 4944026

Please sign in to comment.