Skip to content

Commit

Permalink
Merge branch 'release/0.7.1' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
rlskoeser committed Feb 16, 2022
2 parents c5ebaf5 + 7052fad commit 5b6d59b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

0.7.1
-----

* Improved error handling for connection errors when importing IIIF manifests

0.7
---

Expand Down
2 changes: 1 addition & 1 deletion djiffy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
default_app_config = 'djiffy.apps.DjiffyConfig'

__version_info__ = (0, 7, 0, None)
__version_info__ = (0, 7, 1, None)


# Dot-connect all but the last. Last is dash-connected if not None.
Expand Down
2 changes: 1 addition & 1 deletion djiffy/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def import_paths(self, paths):
try:
manifest = IIIFPresentation.from_file_or_url(path)
except IIIFException as err:
self.stderr.write(str(err))
self.error_msg(str(err))
continue

if manifest.type == 'sc:Collection':
Expand Down
34 changes: 19 additions & 15 deletions djiffy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import rdflib
from rdflib.namespace import DC
import requests
from requests.exceptions import ConnectionError


def get_iiif_url(url):
Expand Down Expand Up @@ -311,21 +312,24 @@ def from_url(cls, uri):
:raises: :class:`IIIFException` if URL is not retrieved successfully,
if the response is not JSON content, or if the JSON cannot be parsed.
'''
response = get_iiif_url(uri)
if response.status_code == requests.codes.ok:
try:
return cls(response.json())
except json.decoder.JSONDecodeError as err:
# if json fails, two possibilities:
# - we didn't actually get json (e.g. redirect for auth)
if 'application/json' not in response.headers['content-type']:
raise IIIFException('No JSON found at %s' % uri)
# - there is something wrong with the json
raise IIIFException('Error parsing JSON for %s: %s' %
(uri, err))

raise IIIFException('Error retrieving manifest at %s: %s %s' %
(uri, response.status_code, response.reason))
try:
response = get_iiif_url(uri)
if response.status_code == requests.codes.ok:
try:
return cls(response.json())
except json.decoder.JSONDecodeError as err:
# if json fails, two possibilities:
# - we didn't actually get json (e.g. redirect for auth)
if 'application/json' not in response.headers['content-type']:
raise IIIFException('No JSON found at %s' % uri)
# - there is something wrong with the json
raise IIIFException('Error parsing JSON for %s: %s' %
(uri, err))
raise IIIFException('Error retrieving manifest at %s: %s %s' %
(uri, response.status_code, response.reason))
except ConnectionError:
# could not reach URL to get a status code in the first place
raise IIIFException('Error connecting to manifest at %s' % uri)

@classmethod
def is_url(cls, url):
Expand Down
6 changes: 6 additions & 0 deletions djiffy/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ def test_from_url(self):
IIIFPresentation.from_url(manifest_url)
assert 'Error parsing JSON' in str(excinfo.value)

def test_from_url_bad_domain(self):
manifest_url = 'http://ma.ni/fe.st'
with pytest.raises(IIIFException):
IIIFPresentation.from_url(manifest_url)


def test_from_url_or_file(self):
with patch.object(IIIFPresentation, 'from_url') as mock_from_url:
# local fixture file
Expand Down

0 comments on commit 5b6d59b

Please sign in to comment.