8000 Add hook to GitHubIterator for object responses · jeffgreenca/github3.py@453d095 · GitHub
[go: up one dir, main page]

Skip to content

Commit 453d095

Browse files
committed
Add hook to GitHubIterator for object responses
Some GitHub APIs don't directly return lists when listing objects, but contain the elements below a dict key. The hook allows classes to declare a 'list_response_dict_key' attribute that is used to lookup the list of elements in the return JSON object.
1 parent b915af8 commit 453d095

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/github3/checks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class CheckSuite(models.GitHubCore):
193193
"""
194194

195195
class_name = "CheckSuite"
196+
list_response_dict_key = "check_suites"
196197
CUSTOM_HEADERS = {"Accept": "application/vnd.github.antiope-preview+json"}
197198

198199
def _update_attributes(self, suite):
@@ -320,6 +321,7 @@ class CheckRun(models.GitHubCore):
320321
"""
321322

322323
class_name = "CheckRun"
324+
list_response_dict_key = "check_runs"
323325
CUSTOM_HEADERS = {"Accept": "application/vnd.github.antiope-preview+json"}
324326

325327
def _update_attributes(self, run):

src/github3/structs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ def __next__(self):
112112
return next(self.__i__)
113113

114114
def _get_json(self, response):
115-
return self._json(response, 200)
115+
json = self._json(response, 200)
116+
if isinstance(json, dict):
117+
list_key = getattr(self.cls, "list_response_dict_key", None)
118+
if list_key is not None:
119+
return json.get(list_key)
120+
return json
116121

117122
def refresh(self, conditional=False):
118123
self.count = self.original

0 commit comments

Comments
 (0)
0