| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 64d01ef commit bc9471a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,12 +18,14 @@ | |||
| 18 | 18 | ||
| 19 | 19 | import os | |
| 20 | 20 | import time | |
| 21 | + import warnings | ||
| 21 | 22 | from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union | |
| 22 | 23 | ||
| 23 | 24 | import requests | |
| 24 | 25 | import requests.utils | |
| 25 | 26 | from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore | |
| 26 | 27 | ||
| 28 | + import gitlab | ||
| 27 | 29 | import gitlab.config | |
| 28 | 30 | import gitlab.const | |
| 29 | 31 | import gitlab.exceptions | |
@@ -35,6 +37,14 @@ | |||
| 35 | 37 | "{source!r} to {target!r}" | |
| 36 | 38 | ) | |
| 37 | 39 | ||
| 40 | + # https://docs.gitlab.com/ee/api/#offset-based-pagination | ||
| 41 | + DEFAULT_PER_PAGE = 20 | ||
| 42 | + MAXIMUM_PER_PAGE = 100 | ||
| 43 | + _PAGINATION_URL = ( | ||
| 44 | + f"https://python-gitlab.readthedocs.io/en/v{gitlab.__version__}/" | ||
| 45 | + f"api-usage.html#pagination" | ||
| 46 | + ) | ||
| 47 | + | ||
| 38 | 48 | ||
| 39 | 49 | class Gitlab: | |
| 40 | 50 | """Represents a GitLab server connection. | |
@@ -816,9 +826,22 @@ def http_list( | |||
| 816 | 826 | if get_all is True and as_list is True: | |
| 817 | 827 | return list(GitlabList(self, url, query_data, **kwargs)) | |
| 818 | 828 | ||
| 829 | + per_page = min(kwargs.get("per_page", DEFAULT_PER_PAGE), MAXIMUM_PER_PAGE) | ||
| 819 | 830 | if page or as_list is True: | |
| 820 | 831 | # pagination requested, we return a list | |
| 821 | - return list(GitlabList(self, url, query_data, get_next=False, **kwargs)) | ||
| 832 | + items = list(GitlabList(self, url, query_data, get_next=False, **kwargs)) | ||
| 833 | + if page is None and len(items) >= per_page: | ||
| 834 | + # Warn the user that they are only going to retrieve `per_page` maximum | ||
| 835 | + # items. This is a common cause of issues filed. | ||
| 836 | + warnings.warn( | ||
| 837 | + ( | ||
| 838 | + f"Calling a `list()` method without specifying `all=True` or " | ||
| 839 | + f"`as_list=False` will return a maximum of {per_page} items. See " | ||
| 840 | + f"{_PAGINATION_URL} for more details" | ||
| 841 | + ), | ||
| 842 | + UserWarning, | ||
| 843 | + ) | ||
| 844 | + return items | ||
| 822 | 845 | ||
| 823 | 846 | # No pagination, generator requested | |
| 824 | 847 | return GitlabList(self, url, query_data, **kwargs) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,5 @@ | |||
| 1 | + import warnings | ||
| 2 | + | ||
| 1 | 3 | import pytest | |
| 2 | 4 | import requests | |
| 3 | 5 | import responses | |
@@ -329,20 +331,83 @@ def test_list_request(gl): | |||
| 329 | 331 | match=MATCH_EMPTY_QUERY_PARAMS, | |
| 330 | 332 | ) | |
| 331 | 333 | ||
| 332 | - result = gl.http_list("/projects", as_list=True) | ||
| 334 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 335 | + result = gl.http_list("/projects", as_list=True) | ||
| 336 | + assert len(caught_warnings) == 0 | ||
| 333 | 337 | assert isinstance(result, list) | |
| 334 | 338 | assert len(result) == 1 | |
| 335 | 339 | ||
| 336 | 340 | result = gl.http_list("/projects", as_list=False) | |
| 337 | 341 | assert isinstance(result, GitlabList) | |
| 338 | - assert len(result) == 1 | ||
| 342 | + assert len(list(result)) == 1 | ||
| 339 | 343 | ||
| 340 | 344 | result = gl.http_list("/projects", all=True) | |
| 341 | 345 | assert isinstance(result, list) | |
| 342 | 346 | assert len(result) == 1 | |
| 343 | 347 | assert responses.assert_call_count(url, 3) is True | |
| 344 | 348 | ||
| 345 | 349 | ||
| 350 | + @responses.activate | ||
| 351 | + def test_list_request_pagination_warning(gl): | ||
| 352 | + url = "http://localhost/api/v4/projects" | ||
| 353 | + responses.add( | ||
| 354 | + method=responses.GET, | ||
| 355 | + url=url, | ||
| 356 | + json=[ | ||
| 357 | + {"name": "project01"}, | ||
| 358 | + {"name": "project02"}, | ||
| 359 | + {"name": "project03"}, | ||
| 360 | + {"name": "project04"}, | ||
| 361 | + {"name": "project05"}, | ||
| 362 | + {"name": "project06"}, | ||
| 363 | + {"name": "project07"}, | ||
| 364 | + {"name": "project08"}, | ||
| 365 | + {"name": "project09"}, | ||
| 366 | + {"name": "project10"}, | ||
| 367 | + {"name": "project11"}, | ||
| 368 | + {"name": "project12"}, | ||
| 369 | + {"name": "project13"}, | ||
| 370 | + {"name": "project14"}, | ||
| 371 | + {"name": "project15"}, | ||
| 372 | + {"name": "project16"}, | ||
| 373 | + {"name": "project17"}, | ||
| 374 | + {"name": "project18"}, | ||
| 375 | + {"name": "project19"}, | ||
| 376 | + {"name": "project20"}, | ||
| 377 | + ], | ||
| 378 | + headers={"X-Total": "1"}, | ||
| 379 | + status=200, | ||
| 380 | + match=MATCH_EMPTY_QUERY_PARAMS, | ||
| 381 | + ) | ||
| 382 | + | ||
| 383 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 384 | + result = gl.http_list("/projects", as_list=True) | ||
| 385 | + assert len(caught_warnings) == 1 | ||
| 386 | + warning = caught_warnings[0] | ||
| 387 | + assert isinstance(warning.message, UserWarning) | ||
| 388 | + message = str(caught_warnings[0].message) | ||
| 389 | + assert "Calling" in message | ||
| 390 | + assert "return a maximum of" in message | ||
| 391 | + assert "readthedocs" in message | ||
| 392 | + | ||
| 393 | + assert isinstance(result, list) | ||
| 394 | + assert len(result) == 20 | ||
| 395 | + | ||
| 396 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 397 | + result = gl.http_list("/projects", as_list=False) | ||
| 398 | + assert len(caught_warnings) == 0 | ||
| 399 | + assert isinstance(result, GitlabList) | ||
| 400 | + assert len(list(result)) == 20 | ||
| 401 | + | ||
| 402 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 403 | + result = gl.http_list("/projects", all=True) | ||
| 404 | + assert len(caught_warnings) == 0 | ||
| 405 | + | ||
| 406 | + assert isinstance(result, list) | ||
| 407 | + assert len(result) == 20 | ||
| 408 | + assert responses.assert_call_count(url, 3) is True | ||
| 409 | + | ||
| 410 | + | ||
| 346 | 411 | @responses.activate | |
| 347 | 412 | def test_list_request_404(gl): | |
| 348 | 413 | url = "http://localhost/api/v4/not_there" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments