-
Notifications
You must be signed in to change notification settings - Fork 13
/
set_hwd_from_iiif.py
38 lines (28 loc) · 1.25 KB
/
set_hwd_from_iiif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import requests
from ..loader import monkeypatch_schema
from ..skeleton import Canvas, Resource, ResourceItem
class SetHwdFromIIIF:
# should probably be added to canvas helpers
def set_hwd_from_iiif(self, url):
"""Set height and width on a Canvas object.
Requests IIIF Image information remotely for an
image resource and sets resulting height and width.
This method will return the info.json
Args:
url (str): An HTTP URL for the IIIF image endpoint.
"""
# 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()
self.set_hwd(resource_info.get("height"), resource_info.get("width"))
return resource_info
monkeypatch_schema([Canvas, Resource, ResourceItem], SetHwdFromIIIF)