Skip to content

Commit

Permalink
Do not set up Loan.external_identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
vbessonov committed Nov 13, 2020
1 parent bf88ce9 commit ce18e58
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 45 deletions.
66 changes: 62 additions & 4 deletions api/proquest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from contextlib import contextmanager

import requests
from flask_babel import lazy_gettext as _
from requests import HTTPError, Request

from core.exceptions import BaseError
from core.model.configuration import (
ConfigurationAttributeType,
Expand All @@ -12,8 +15,6 @@
)
from core.util import is_session
from core.util.string_helpers import is_string
from flask_babel import lazy_gettext as _
from requests import HTTPError, Request


class ProQuestAPIClientConfiguration(ConfigurationGrouping):
Expand Down Expand Up @@ -130,6 +131,63 @@ def missing_property(self):
return self._missing_property


class Book(object):
"""POCO class containing book information."""

def __init__(self, link=None, content=None):
"""Initialize a new instance of Book class.
:param link: Book's link
:type link: Optional[str]
:param content: Book's content
:type content: Optional[Union[str, bytes]]
"""
if link is not None and not is_string(link):
raise ValueError("Argument 'link' must be a string")
if content is not None and not isinstance(content, bytes):
raise ValueError("Argument 'content' must be a bytes string")
if link is not None and content is not None:
raise ValueError(
"'link' and 'content' cannot be both set up at the same time"
)

self._link = link
self._content = content

def __eq__(self, other):
"""Compare self and other other book.
:param other: Other book instance
:type other: Any
:return: Boolean value indicating whether self and other are equal to each to other
:rtype: bool
"""
if not isinstance(other, Book):
return False

return self.link == other.link and self.content == other.content

@property
def link(self):
"""Return the book's link.
:return: Book's link
:rtype: Optional[str]
"""
return self._link

@property
def content(self):
"""Return the book's content.
:return: Book's content
:rtype: Optional[Union[str, bytes]]
"""
return self._content


class ProQuestAPIClient(object):
"""ProQuest API client."""

Expand Down Expand Up @@ -568,15 +626,15 @@ def get_book(self, db, token, document_id):
response, self.DOWNLOAD_LINK_FIELD
)

return response_json[self.DOWNLOAD_LINK_FIELD]
return Book(link=response_json[self.DOWNLOAD_LINK_FIELD])
else:
self._logger.info(
"Finished fetching a book link for Doc ID {0} using JWT token {1}".format(
document_id, token
)
)

return bytes(response.content)
return Book(content=bytes(response.content))


class ProQuestAPIClientFactory(object):
Expand Down
10 changes: 5 additions & 5 deletions api/proquest/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def patron_activity(self, patron, pin):
start_date=loan.start,
end_date=loan.end,
fulfillment_info=None,
external_identifier=loan.external_identifier,
external_identifier=None,
)
)

Expand Down Expand Up @@ -447,7 +447,7 @@ def checkout(self, patron, pin, licensepool, internal_format):
start_date=today,
end_date=expires,
fulfillment_info=None,
external_identifier=licensepool.identifier.identifier,
external_identifier=None,
)

self._logger.info(
Expand Down Expand Up @@ -479,15 +479,15 @@ def fulfill(
patron, configuration, licensepool.identifier.identifier
)

if isinstance(book, bytes):
if book.content is not None:
fulfillment_info = FulfillmentInfo(
licensepool.collection,
licensepool.data_source.name,
licensepool.identifier.type,
licensepool.identifier.identifier,
content_link=None,
content_type=internal_format.delivery_mechanism.media_type,
content=book,
content=book.content,
content_expires=None,
)
else:
Expand All @@ -496,7 +496,7 @@ def fulfill(
licensepool.data_source.name,
licensepool.identifier.type,
licensepool.identifier.identifier,
content_link=book,
content_link=book.link,
content_type=internal_format.delivery_mechanism.media_type,
content=None,
content_expires=None,
Expand Down
14 changes: 8 additions & 6 deletions tests/proquest/test_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import json

import requests_mock
from mock import MagicMock, create_autospec
from nose.tools import assert_raises, eq_
from parameterized import parameterized
from requests import HTTPError

from api.proquest.client import (
Book,
ProQuestAPIClient,
ProQuestAPIClientConfiguration,
ProQuestAPIInvalidJSONResponseError,
Expand All @@ -15,10 +21,6 @@
HasExternalIntegration,
)
from core.testing import DatabaseTest
from mock import MagicMock, create_autospec
from nose.tools import assert_raises, eq_
from parameterized import parameterized
from requests import HTTPError

BOOKS_CATALOG_SERVICE_URL = "https://proquest.com/lib/nyulibrary-ebooks/BooksCatalog"
PARTNER_AUTH_TOKEN_SERVICE_URL = (
Expand Down Expand Up @@ -315,12 +317,12 @@ def test_get_book_correctly_fails(
ProQuestAPIClient.DOWNLOAD_LINK_FIELD: "https://proquest.com/books/12345",
}
},
u"https://proquest.com/books/12345",
Book(link=u"https://proquest.com/books/12345"),
),
(
"when_it_is_passed_in_response_body",
{"content": "PDF Book12345"},
bytes("PDF Book12345"),
Book(content=bytes("PDF Book12345")),
),
]
)
Expand Down
5 changes: 3 additions & 2 deletions tests/proquest/test_credential.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime
import json

from nose.tools import eq_
from parameterized import parameterized

from api.authenticator import BaseSAMLAuthenticationProvider
from api.proquest.credential import ProQuestCredentialManager, ProQuestCredentialType
from api.saml.metadata import (
Expand All @@ -12,8 +15,6 @@
)
from core.model import Credential, DataSource
from core.testing import DatabaseTest
from nose.tools import eq_
from parameterized import parameterized
from tests.saml import fixtures


Expand Down
5 changes: 3 additions & 2 deletions tests/proquest/test_identifier.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from api.proquest.identifier import ProQuestIdentifierParser
from core.model import Identifier
from nose.tools import eq_
from parameterized import parameterized

from api.proquest.identifier import ProQuestIdentifierParser
from core.model import Identifier


class TestProQuestIdentifierParser(object):
@parameterized.expand(
Expand Down
Loading

0 comments on commit ce18e58

Please sign in to comment.