Skip to content

Commit

Permalink
Add get for lcp fulfill
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Sep 5, 2024
1 parent 5e974d2 commit e72ca88
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/palace/manager/api/odl/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dateutil
from dependency_injector.wiring import Provide, inject
from flask import url_for
from requests import Response
from sqlalchemy import or_
from sqlalchemy.orm import Session
from uritemplate import URITemplate
Expand Down Expand Up @@ -66,7 +67,11 @@
from palace.manager.sqlalchemy.util import get_one
from palace.manager.util import base64
from palace.manager.util.datetime_helpers import utc_now
from palace.manager.util.http import BadResponseException, RemoteIntegrationException
from palace.manager.util.http import (
HTTP,
BadResponseException,
RemoteIntegrationException,
)


class OPDS2WithODLApi(
Expand Down Expand Up @@ -540,6 +545,27 @@ def _find_matching_delivery_mechanism(
raise FormatNotAvailable()
return fulfillment

@classmethod
def _lcp_fulfill_get(cls, url: str, **kwargs: Any) -> Response:
try:
return HTTP.get_with_timeout(url, allowed_response_codes=["2xx"], **kwargs)
except BadResponseException as ex:
response = ex.response
cls.logger().exception(
f"Error fulfilling LCP loan. Status code: {response.status_code}. "
f"Response: {response.text}."
)
if response.headers.get("content-type") == "application/api-problem+json":
try:
title = response.json().get("title")
if title is not None:
raise RemoteIntegrationException(
url, title, response.text
) from ex
except ValueError:
pass
raise

def _lcp_fulfill(
self, loan: Loan, delivery_mechanism: LicensePoolDeliveryMechanism
) -> Fulfillment:
Expand Down Expand Up @@ -569,6 +595,7 @@ def _lcp_fulfill(
return FetchFulfillment(
content_link,
content_type,
do_get=self._lcp_fulfill_get,
)

def _bearer_token_fulfill(
Expand Down
1 change: 1 addition & 0 deletions tests/manager/api/odl/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ def test_fulfill_success(
assert isinstance(fulfillment, FetchFulfillment)
assert correct_link == fulfillment.content_link
assert correct_type == fulfillment.content_type
assert opds2_with_odl_api_fixture.api._lcp_fulfill_get == fulfillment.do_get

def test_fulfill_open_access(
self,
Expand Down

0 comments on commit e72ca88

Please sign in to comment.