Skip to content
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

An attempt to solve Issue 218 for caching requests #219

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions iiif_prezi3/helpers/set_hwd_from_iiif.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
from functools import lru_cache
import requests

from ..loader import monkeypatch_schema
from ..skeleton import Canvas, Resource, ResourceItem


@lru_cache(maxsize=3)
def get_info_json(url):
"""Cache the info.json response of the URL request.

Args:
url (str): An HTTP URL for the IIIF image endpoint.

Returns:
dict: The info.json as a dictionary.
"""
response = requests.get(url)
# if response is not 200, raise exception
if response.status_code != requests.codes.ok:
response.raise_for_status()
# if response is not valid json, request will raise
# requests.exceptions.JSONDecodeError
# — handle or document and let calling code handle?
return response.json()


class SetHwdFromIIIF:
# should probably be added to canvas helpers

def set_hwd_from_iiif(self, url):
def set_hwd_from_iiif(self, url, use_cache=True):
"""Set height and width on a Canvas object.

Requests IIIF Image information remotely for an
Expand All @@ -16,22 +36,18 @@ def set_hwd_from_iiif(self, url):

Args:
url (str): An HTTP URL for the IIIF image endpoint.
use_cache (bool): If True multiple requests to the same URL will
use the cached response without making new requests to the server
(default True).
"""
# resource url may or may not end with info.json;
# add if not present
if not url.endswith("info.json"):
url = f"{ url.rstrip('/') }/info.json"

response = requests.get(url)
# if response is not 200, raise exception
if response.status_code != requests.codes.ok:
response.raise_for_status()
# if response is not valid json, request will raise
# requests.exceptions.JSONDecodeError
# — handle or document and let calling code handle?
resource_info = response.json()
resource_info = (
get_info_json(url) if use_cache else get_info_json.__wrapped__(url)
)
self.set_hwd(resource_info.get("height"), resource_info.get("width"))

return resource_info


Expand Down
18 changes: 12 additions & 6 deletions tests/test_set_hwd_from_iiif.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ def test_set_hwd_from_iiif(self, mockrequest_get):
image_id = 'http://iiif.example.org/images/1234abcd'
image_info_url = f'{image_id}/info.json'
# image api url without info.json
self.canvas.set_hwd_from_iiif(image_id)
self.canvas.set_hwd_from_iiif(image_id, use_cache=False)
mockrequest_get.assert_called_with(image_info_url)

# image api url with info.json
mockrequest_get.reset_mock()
self.canvas.set_hwd_from_iiif(image_id)
self.canvas.set_hwd_from_iiif(image_id, use_cache=False)
mockrequest_get.assert_called_with(image_info_url)

# image url with trailing slash
mockrequest_get.reset_mock()
self.canvas.set_hwd_from_iiif(image_id + "/")
self.canvas.set_hwd_from_iiif(image_id + "/", use_cache=False)
mockrequest_get.assert_called_with(image_info_url)

# non-200 response
Expand All @@ -37,15 +37,15 @@ def test_set_hwd_from_iiif(self, mockrequest_get):
mockresponse.raise_for_status.side_effect = requests.exceptions.HTTPError
mockrequest_get.return_value = mockresponse
with self.assertRaises(requests.exceptions.HTTPError):
self.canvas.set_hwd_from_iiif(image_id)
self.canvas.set_hwd_from_iiif(image_id, use_cache=False)
# non-200 should raise exception for status
mockresponse.raise_for_status.assert_called_once()

# 200 response but not valid json should raise exception
mockresponse.status_code = 200
mockresponse.json.side_effect = requests.exceptions.JSONDecodeError("", "", 0) # JSONDecodeError needs arguments
with self.assertRaises(requests.exceptions.JSONDecodeError):
self.canvas.set_hwd_from_iiif(image_id)
self.canvas.set_hwd_from_iiif(image_id, use_cache=False)

# successful response with dimensions
mockresponse.json.side_effect = None
Expand All @@ -59,5 +59,11 @@ def test_set_hwd_from_iiif(self, mockrequest_get):
}
# patch the method this helper calls so we can inspect
with patch.object(Canvas, 'set_hwd') as mock_set_hwd:
self.canvas.set_hwd_from_iiif(image_id)
self.canvas.set_hwd_from_iiif(image_id, use_cache=False)
mock_set_hwd.assert_called_with(6608, 4821)

# we test the caching mechanism
self.canvas.set_hwd_from_iiif(image_id, use_cache=True) # will call get
mockrequest_get.reset_mock()
self.canvas.set_hwd_from_iiif(image_id, use_cache=True) # should not
mockrequest_get.assert_not_called()
Loading