From b13612320d4dfb01f7f7c12c1d47ac8f88acf3aa Mon Sep 17 00:00:00 2001 From: Jonathan Green Date: Tue, 10 Sep 2024 06:16:35 -0300 Subject: [PATCH] Update to use a redirect (#2036) --- src/palace/manager/api/enki.py | 23 +++++++++-------------- tests/manager/api/test_enki.py | 25 +++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/palace/manager/api/enki.py b/src/palace/manager/api/enki.py index 6aaa89e0a..234a954b4 100644 --- a/src/palace/manager/api/enki.py +++ b/src/palace/manager/api/enki.py @@ -19,6 +19,8 @@ HoldInfo, LoanInfo, PatronActivityCirculationAPI, + RedirectFulfillment, + UrlFulfillment, ) from palace.manager.api.circulation_exceptions import ( CannotFulfill, @@ -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) @@ -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] diff --git a/tests/manager/api/test_enki.py b/tests/manager/api/test_enki.py index c950dae9b..e604f6efe 100644 --- a/tests/manager/api/test_enki.py +++ b/tests/manager/api/test_enki.py @@ -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, @@ -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() @@ -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")