From d3b1941eaad6b91f93e19e76361e412e9d072520 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 13 Mar 2021 09:07:19 -0800 Subject: [PATCH] Uses get_data helper in isbn search (plus test) --- bookwyrm/connectors/abstract_connector.py | 16 +++------------- .../test_abstract_minimal_connector.py | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/bookwyrm/connectors/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index 9d8a05e0..7635f57e 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -57,24 +57,14 @@ class AbstractMinimalConnector(ABC): def isbn_search(self, query): """ isbn search """ params = {} - resp = requests.get( + data = get_data( "%s%s" % (self.isbn_search_url, query), params=params, - headers={ - "Accept": "application/json; charset=utf-8", - "User-Agent": settings.USER_AGENT, - }, ) - if not resp.ok: - resp.raise_for_status() - try: - data = resp.json() - except ValueError as e: - logger.exception(e) - raise ConnectorException("Unable to parse json response", e) results = [] - for doc in self.parse_isbn_search_data(data): + # this shouldn't be returning mutliple results, but just in case + for doc in self.parse_isbn_search_data(data)[:10]: results.append(self.format_isbn_search_result(doc)) return results diff --git a/bookwyrm/tests/connectors/test_abstract_minimal_connector.py b/bookwyrm/tests/connectors/test_abstract_minimal_connector.py index d710abf8..fa7c85f4 100644 --- a/bookwyrm/tests/connectors/test_abstract_minimal_connector.py +++ b/bookwyrm/tests/connectors/test_abstract_minimal_connector.py @@ -19,7 +19,7 @@ class AbstractConnector(TestCase): books_url="https://example.com/books", covers_url="https://example.com/covers", search_url="https://example.com/search?q=", - isbn_search_url="https://example.com/isbn", + isbn_search_url="https://example.com/isbn?q=", ) class TestConnector(abstract_connector.AbstractMinimalConnector): @@ -50,7 +50,7 @@ class AbstractConnector(TestCase): self.assertEqual(connector.books_url, "https://example.com/books") self.assertEqual(connector.covers_url, "https://example.com/covers") self.assertEqual(connector.search_url, "https://example.com/search?q=") - self.assertEqual(connector.isbn_search_url, "https://example.com/isbn") + self.assertEqual(connector.isbn_search_url, "https://example.com/isbn?q=") self.assertIsNone(connector.name) self.assertEqual(connector.identifier, "example.com") self.assertIsNone(connector.max_query_count) @@ -83,6 +83,18 @@ class AbstractConnector(TestCase): results = self.test_connector.search("a book title", min_confidence=1) self.assertEqual(len(results), 10) + @responses.activate + def test_isbn_search(self): + """ makes an http request to the outside service """ + responses.add( + responses.GET, + "https://example.com/isbn?q=123456", + json=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"], + status=200, + ) + results = self.test_connector.isbn_search("123456") + self.assertEqual(len(results), 10) + def test_search_result(self): """ a class that stores info about a search result """ result = SearchResult(