diff --git a/api/proquest/client.py b/api/proquest/client.py index 14cb685e4..f69dd1ce6 100644 --- a/api/proquest/client.py +++ b/api/proquest/client.py @@ -621,6 +621,17 @@ def get_book(self, db, token, document_id): ) ) + if self.DOWNLOAD_LINK_FIELD not in response_json: + raise ProQuestAPIMissingJSONPropertyError( + response, self.DOWNLOAD_LINK_FIELD + ) + + # The API just returns another link leading to the actual ACSM book. + link = response_json[self.DOWNLOAD_LINK_FIELD] + response, response_json = self._send_request( + configuration, "get", link, {}, token, response_must_be_json=True + ) + if self.DOWNLOAD_LINK_FIELD not in response_json: raise ProQuestAPIMissingJSONPropertyError( response, self.DOWNLOAD_LINK_FIELD diff --git a/tests/proquest/test_client.py b/tests/proquest/test_client.py index abde43f6a..0dda70006 100644 --- a/tests/proquest/test_client.py +++ b/tests/proquest/test_client.py @@ -307,29 +307,11 @@ def test_get_book_correctly_fails( with assert_raises(expected_exception_class): self._client.get_book(self._db, token, document_id) - @parameterized.expand( - [ - ( - "when_its_link_is_inside_json_document", - { - "json": { - ProQuestAPIClient.RESPONSE_STATUS_CODE_FIELD: 200, - ProQuestAPIClient.DOWNLOAD_LINK_FIELD: "https://proquest.com/books/12345", - } - }, - Book(link=u"https://proquest.com/books/12345"), - ), - ( - "when_it_is_passed_in_response_body", - {"content": "PDF Book12345"}, - Book(content=bytes("PDF Book12345")), - ), - ] - ) - def test_get_book_correctly_extracts_book( - self, _, response_arguments, expected_book - ): + def test_get_book_correctly_extracts_book_when_it_is_passed_in_response_body(self): # Arrange + response_arguments = {"content": "PDF Book12345"} + expected_book = Book(content=bytes("PDF Book12345")) + token = "12345" document_id = "12345" download_link_service_url = URLUtility.build_url( @@ -350,3 +332,46 @@ def test_get_book_correctly_extracts_book( # Assert eq_(expected_book, book) eq_(type(expected_book), type(book)) + + def test_get_book_correctly_extracts_book_when_its_link_is_inside_json_document( + self, + ): + # Arrange + download_link = "https://proquest.com/fulfill?documentID=12345" + book_link = u"https://proquest.com/books/12345" + expected_book = Book(link=book_link) + + first_response_arguments = { + "json": { + ProQuestAPIClient.RESPONSE_STATUS_CODE_FIELD: 200, + ProQuestAPIClient.DOWNLOAD_LINK_FIELD: download_link, + } + } + second_response_arguments = { + "json": { + ProQuestAPIClient.RESPONSE_STATUS_CODE_FIELD: 200, + ProQuestAPIClient.DOWNLOAD_LINK_FIELD: book_link, + } + } + + token = "12345" + document_id = "12345" + download_link_service_url = URLUtility.build_url( + DOWNLOAD_LINK_SERVICE_URL, {"docID": document_id} + ) + + with self._configuration_factory.create( + self._configuration_storage, self._db, ProQuestAPIClientConfiguration + ) as configuration: + configuration.download_link_service_url = DOWNLOAD_LINK_SERVICE_URL + + with requests_mock.Mocker() as request_mock: + request_mock.get(download_link_service_url, **first_response_arguments) + request_mock.get(download_link, **second_response_arguments) + + # Act + book = self._client.get_book(self._db, token, document_id) + + # Assert + eq_(expected_book, book) + eq_(type(expected_book), type(book))