Skip to content

Commit

Permalink
Update to use a redirect (#2036)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen authored Sep 10, 2024
1 parent 9a69630 commit b136123
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
23 changes: 9 additions & 14 deletions src/palace/manager/api/enki.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
HoldInfo,
LoanInfo,
PatronActivityCirculationAPI,
RedirectFulfillment,
UrlFulfillment,
)
from palace.manager.api.circulation_exceptions import (
CannotFulfill,
Expand Down Expand Up @@ -473,7 +475,7 @@ def fulfill(
pin: str,
licensepool: LicensePool,
delivery_mechanism: LicensePoolDeliveryMechanism,
) -> FetchFulfillment:
) -> UrlFulfillment:
"""Get the actual resource file to the patron."""
book_id = licensepool.identifier.identifier
enki_library_id = self.enki_library_id(patron.library)
Expand Down Expand Up @@ -509,20 +511,13 @@ def fulfill(
drm_type = mechanism.drm_scheme
break

# TODO: I think in this case we should be returning a RedirectFulfillment
# instead of a FetchFulfillment. I think we are setting the "Accept-Encoding"
# header here to minimize the size of the response, but I think we would be
# better off just returning the URL and letting the client handle the request.
# However I need to test this case, so I'm leaving it as is for now.
headers = {}
if drm_type == DeliveryMechanism.NO_DRM:
headers["Accept-Encoding"] = "deflate"

return FetchFulfillment(
content_link=url,
content_type=drm_type,
include_headers=headers,
)
return RedirectFulfillment(url)
else:
return FetchFulfillment(
content_link=url,
content_type=drm_type,
)

def parse_fulfill_result(
self, result: Mapping[str, Any]
Expand Down
25 changes: 23 additions & 2 deletions tests/manager/api/test_enki.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

import pytest

from palace.manager.api.circulation import FetchFulfillment, LoanInfo
from palace.manager.api.circulation import (
FetchFulfillment,
LoanInfo,
RedirectFulfillment,
)
from palace.manager.api.circulation_exceptions import (
NoAvailableCopies,
PatronAuthorizationFailedException,
Expand Down Expand Up @@ -512,7 +516,7 @@ def test_fulfillment_acs_parser(self, enki_test_fixture: EnkiTestFixure):
)
assert fulfill_data[1] == "epub"

def test_fulfill_success(self, enki_test_fixture: EnkiTestFixure):
def test_fulfill_success_acsm(self, enki_test_fixture: EnkiTestFixure):
db = enki_test_fixture.db
# Test the fulfill() method.
patron = db.patron()
Expand Down Expand Up @@ -550,6 +554,23 @@ def test_fulfill_success(self, enki_test_fixture: EnkiTestFixure):
"http://afs.enkilibrary.org/fulfillment/URLLink.acsm"
)

def test_fulfill_success_open(self, enki_test_fixture: EnkiTestFixure):
db = enki_test_fixture.db
patron = db.patron()
patron.authorization_identifier = "123"
pool = db.licensepool(None, with_open_access_download=True)

data = enki_test_fixture.files.sample_data("checked_out_direct.json")
enki_test_fixture.api.queue_response(200, content=data)
fulfillment = enki_test_fixture.api.fulfill(patron, "pin", pool, MagicMock())

# A Fulfillment for the loan was returned.
assert isinstance(fulfillment, RedirectFulfillment)
assert (
fulfillment.content_link
== "http://cccl.enkilibrary.org/API/UserAPI?method=downloadEContentFile&username=21901000008080&password=deng&lib=1&recordId=2"
)

def test_patron_activity(self, enki_test_fixture: EnkiTestFixure):
db = enki_test_fixture.db
data = enki_test_fixture.files.sample_data("patron_response.json")
Expand Down

0 comments on commit b136123

Please sign in to comment.