Skip to content

Commit ef2f8b6

Browse files
add list_organization_followers helper for /api/organizations/{org}/followers (#3467)
* feat: Add list_organization_followers method and corresponding test * fix: Correct indentation for "list_organization_followers" * Apply style fixes --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent b8bb902 commit ef2f8b6

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/huggingface_hub/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
"list_lfs_files",
238238
"list_liked_repos",
239239
"list_models",
240+
"list_organization_followers",
240241
"list_organization_members",
241242
"list_papers",
242243
"list_pending_access_requests",
@@ -893,6 +894,7 @@
893894
"list_lfs_files",
894895
"list_liked_repos",
895896
"list_models",
897+
"list_organization_followers",
896898
"list_organization_members",
897899
"list_papers",
898900
"list_pending_access_requests",
@@ -1249,6 +1251,7 @@ def __dir__():
12491251
list_lfs_files, # noqa: F401
12501252
list_liked_repos, # noqa: F401
12511253
list_models, # noqa: F401
1254+
list_organization_followers, # noqa: F401
12521255
list_organization_members, # noqa: F401
12531256
list_papers, # noqa: F401
12541257
list_pending_access_requests, # noqa: F401

src/huggingface_hub/hf_api.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9528,6 +9528,35 @@ def get_organization_overview(self, organization: str, token: Union[bool, str, N
95289528
hf_raise_for_status(r)
95299529
return Organization(**r.json())
95309530

9531+
@validate_hf_hub_args
9532+
def list_organization_followers(self, organization: str, token: Union[bool, str, None] = None) -> Iterable[User]:
9533+
"""
9534+
List followers of an organization on the Hub.
9535+
9536+
Args:
9537+
organization (`str`):
9538+
Name of the organization to get the followers of.
9539+
token (`bool` or `str`, *optional*):
9540+
A valid user access token (string). Defaults to the locally saved
9541+
token, which is the recommended method for authentication (see
9542+
https://huggingface.co/docs/huggingface_hub/quick-start#authentication).
9543+
To disable authentication, pass `False`.
9544+
9545+
Returns:
9546+
`Iterable[User]`: A list of [`User`] objects with the followers of the organization.
9547+
9548+
Raises:
9549+
[`HfHubHTTPError`]:
9550+
HTTP 404 If the organization does not exist on the Hub.
9551+
9552+
"""
9553+
for follower in paginate(
9554+
path=f"{constants.ENDPOINT}/api/organizations/{organization}/followers",
9555+
params={},
9556+
headers=self._build_hf_headers(token=token),
9557+
):
9558+
yield User(**follower)
9559+
95319560
def list_organization_members(self, organization: str, token: Union[bool, str, None] = None) -> Iterable[User]:
95329561
"""
95339562
List of members of an organization on the Hub.
@@ -10819,6 +10848,7 @@ def _parse_revision_from_pr_url(pr_url: str) -> str:
1081910848
# User API
1082010849
get_user_overview = api.get_user_overview
1082110850
get_organization_overview = api.get_organization_overview
10851+
list_organization_followers = api.list_organization_followers
1082210852
list_organization_members = api.list_organization_members
1082310853
list_user_followers = api.list_user_followers
1082410854
list_user_following = api.list_user_following

tests/test_hf_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4247,6 +4247,14 @@ def test_organization_members(self) -> None:
42474247
members = self.api.list_organization_members("huggingface")
42484248
assert len(list(members)) > 1
42494249

4250+
def test_organization_followers(self) -> None:
4251+
followers = self.api.list_organization_followers("huggingface")
4252+
first_follower = next(followers)
4253+
assert isinstance(first_follower, User)
4254+
assert first_follower.username
4255+
assert first_follower.fullname
4256+
assert first_follower.avatar_url
4257+
42504258
def test_user_followers(self) -> None:
42514259
followers = self.api.list_user_followers("clem")
42524260
assert len(list(followers)) > 500

0 commit comments

Comments
 (0)