|
5 | 5 |
|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
| 8 | +import inspect |
8 | 9 | import itertools |
9 | 10 | import json |
10 | 11 | import os |
@@ -419,7 +420,7 @@ def _query_resource( |
419 | 420 | """Query the endpoint for a Resource containing a list of documents |
420 | 421 | and meta information about pagination and total document count. |
421 | 422 |
|
422 | | - For the end-user, methods .query() and .count() are intended to be |
| 423 | + For the end-user, methods .search() and .count() are intended to be |
423 | 424 | easier to use. |
424 | 425 |
|
425 | 426 | Arguments: |
@@ -994,8 +995,9 @@ def _submit_request_and_process( |
994 | 995 | ) |
995 | 996 |
|
996 | 997 | if response.status_code in [400]: |
997 | | - warnings.warn( |
998 | | - f"The server does not support the request made to {response.url}. This may be due to an outdated mp-api package, or a problem with the query." |
| 998 | + raise MPRestError( |
| 999 | + f"The server does not support the request made to {response.url}. " |
| 1000 | + "This may be due to an outdated mp-api package, or a problem with the query." |
999 | 1001 | ) |
1000 | 1002 |
|
1001 | 1003 | if response.status_code == 200: |
@@ -1266,34 +1268,45 @@ def count(self, criteria: dict | None = None) -> int | str: |
1266 | 1268 | """Return a count of total documents. |
1267 | 1269 |
|
1268 | 1270 | Args: |
1269 | | - criteria (dict | None): As in .query(). Defaults to None |
| 1271 | + criteria (dict | None): As in .search(). Defaults to None |
1270 | 1272 |
|
1271 | 1273 | Returns: |
1272 | 1274 | (int | str): Count of total results, or string indicating error |
1273 | 1275 | """ |
1274 | | - try: |
1275 | | - criteria = criteria or {} |
1276 | | - user_preferences = ( |
1277 | | - self.monty_decode, |
1278 | | - self.use_document_model, |
1279 | | - self.mute_progress_bars, |
1280 | | - ) |
1281 | | - self.monty_decode, self.use_document_model, self.mute_progress_bars = ( |
1282 | | - False, |
1283 | | - False, |
1284 | | - True, |
1285 | | - ) # do not waste cycles decoding |
1286 | | - results = self._query_resource( |
1287 | | - criteria=criteria, num_chunks=1, chunk_size=1 |
1288 | | - ) |
1289 | | - ( |
1290 | | - self.monty_decode, |
1291 | | - self.use_document_model, |
1292 | | - self.mute_progress_bars, |
1293 | | - ) = user_preferences |
1294 | | - return results["meta"]["total_doc"] |
1295 | | - except Exception: # pragma: no cover |
1296 | | - return "Problem getting count" |
| 1276 | + criteria = criteria or {} |
| 1277 | + user_preferences = ( |
| 1278 | + self.monty_decode, |
| 1279 | + self.use_document_model, |
| 1280 | + self.mute_progress_bars, |
| 1281 | + ) |
| 1282 | + self.monty_decode, self.use_document_model, self.mute_progress_bars = ( |
| 1283 | + False, |
| 1284 | + False, |
| 1285 | + True, |
| 1286 | + ) # do not waste cycles decoding |
| 1287 | + results = self._query_resource(criteria=criteria, num_chunks=1, chunk_size=1) |
| 1288 | + cnt = results["meta"]["total_doc"] |
| 1289 | + |
| 1290 | + no_query = not {field for field in criteria if field[0] != "_"} |
| 1291 | + if no_query and hasattr(self, "search"): |
| 1292 | + allowed_params = inspect.getfullargspec(self.search).args |
| 1293 | + if "deprecated" in allowed_params: |
| 1294 | + criteria["deprecated"] = True |
| 1295 | + results = self._query_resource( |
| 1296 | + criteria=criteria, num_chunks=1, chunk_size=1 |
| 1297 | + ) |
| 1298 | + cnt += results["meta"]["total_doc"] |
| 1299 | + warnings.warn( |
| 1300 | + "Omitting a query also includes deprecated documents in the results. " |
| 1301 | + "Make sure to post-filter them out." |
| 1302 | + ) |
| 1303 | + |
| 1304 | + ( |
| 1305 | + self.monty_decode, |
| 1306 | + self.use_document_model, |
| 1307 | + self.mute_progress_bars, |
| 1308 | + ) = user_preferences |
| 1309 | + return cnt |
1297 | 1310 |
|
1298 | 1311 | @property |
1299 | 1312 | def available_fields(self) -> list[str]: |
|
0 commit comments