Skip to content

Commit 1f300a4

Browse files
committed
Refactor pagination handling in list_labels
1 parent dedf260 commit 1f300a4

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/labels/github.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import typing
21
import logging
2+
from typing import Any, Dict, List, Optional, Tuple
33

44
import attr
55
import requests
@@ -15,7 +15,7 @@ class Repository:
1515
name: str
1616

1717

18-
def not_read_only(attr: attr.Attribute, value: typing.Any) -> bool:
18+
def not_read_only(attr: attr.Attribute, value: Any) -> bool:
1919
"""Filter for attr that checks for a leading underscore."""
2020
return not attr.name.startswith("_")
2121

@@ -35,12 +35,12 @@ class Label:
3535
_url: str = ""
3636

3737
@property
38-
def params_dict(self) -> typing.Dict[str, typing.Any]:
38+
def params_dict(self) -> Dict[str, Any]:
3939
"""Return label parameters as a dict."""
4040
return attr.asdict(self, recurse=True, filter=not_read_only)
4141

4242
@property
43-
def params_tuple(self) -> typing.Tuple[typing.Any, ...]:
43+
def params_tuple(self) -> Tuple[Any, ...]:
4444
"""Return label parameters as a tuple."""
4545
return attr.astuple(self, recurse=True, filter=not_read_only)
4646

@@ -56,7 +56,7 @@ def __init__(
5656
self.session = requests.Session()
5757
self.session.auth = auth
5858

59-
def list_labels(self, repo: Repository) -> typing.List[Label]:
59+
def list_labels(self, repo: Repository) -> List[Label]:
6060
"""Return the list of Labels from the repository.
6161
6262
GitHub API docs:
@@ -65,9 +65,10 @@ def list_labels(self, repo: Repository) -> typing.List[Label]:
6565
logger = logging.getLogger("labels")
6666
logger.debug(f"Requesting labels for {repo.owner}/{repo.name}")
6767

68+
headers = {"Accept": "application/vnd.github.symmetra-preview+json"}
69+
6870
response = self.session.get(
69-
f"{self.base_url}/repos/{repo.owner}/{repo.name}/labels",
70-
headers={"Accept": "application/vnd.github.symmetra-preview+json"},
71+
f"{self.base_url}/repos/{repo.owner}/{repo.name}/labels", headers=headers
7172
)
7273

7374
if response.status_code != 200:
@@ -77,15 +78,14 @@ def list_labels(self, repo: Repository) -> typing.List[Label]:
7778
f"{response.reason}"
7879
)
7980

80-
json = response.json()
81+
repo_labels: List[Dict] = response.json()
8182

82-
next_page = response.links.get('next', None)
83-
while next_page:
84-
logger.debug(f"Requesting {next_page}")
85-
response = self.session.get(
86-
next_page['url'],
87-
headers={"Accept": "application/vnd.github.symmetra-preview+json"},
88-
)
83+
next_page: Optional[Dict] = response.links.get("next", None)
84+
85+
while next_page is not None:
86+
87+
logger.debug(f"Requesting next page of labels")
88+
response = self.session.get(next_page["url"], headers=headers)
8989

9090
if response.status_code != 200:
9191
raise GitHubException(
@@ -94,10 +94,11 @@ def list_labels(self, repo: Repository) -> typing.List[Label]:
9494
f"{response.reason}"
9595
)
9696

97-
json.extend(response.json())
98-
next_page = response.links.get('next', None)
97+
repo_labels.extend(response.json())
98+
99+
next_page = response.links.get("next", None)
99100

100-
return [Label(**data) for data in json]
101+
return [Label(**label) for label in repo_labels]
101102

102103
def get_label(self, repo: Repository, *, name: str) -> Label:
103104
"""Return a single Label from the repository.

0 commit comments

Comments
 (0)