Skip to content

Commit 840c8c7

Browse files
authored
Add follow stats (#124)
Add endpoint support to get follow stats of a feed
1 parent 51920e9 commit 840c8c7

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

stream/client.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,34 @@ def og(self, target_url):
489489
auth_token = self.create_jwt_token("*", "*", feed_id="*")
490490
params = {"url": target_url}
491491
return self.get("og/", auth_token, params=params)
492+
493+
def follow_stats(self, feed_id, followers_slugs=None, following_slugs=None):
494+
"""
495+
Retrieve the number of follower and following feed stats of a given feed.
496+
For each count, feed slugs can be provided to filter counts accordingly.
497+
498+
eg.
499+
client.follow_stats(me, followers_slugs=['user'], following_slugs=['commodities'])
500+
this means to find counts of users following me and count of commodities I am following
501+
"""
502+
auth_token = self.create_jwt_token("*", "*", feed_id="*")
503+
params = {
504+
"followers": feed_id,
505+
"following": feed_id,
506+
}
507+
508+
if followers_slugs:
509+
params["followers_slugs"] = (
510+
",".join(followers_slugs)
511+
if isinstance(followers_slugs, list)
512+
else followers_slugs
513+
)
514+
515+
if following_slugs:
516+
params["following_slugs"] = (
517+
",".join(following_slugs)
518+
if isinstance(following_slugs, list)
519+
else following_slugs
520+
)
521+
522+
return self.get("stats/follow/", auth_token, params=params)

stream/tests/test_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,3 +1620,24 @@ def test_og(self):
16201620
response = client.og("https://google.com")
16211621
self.assertTrue("title" in response)
16221622
self.assertTrue("description" in response)
1623+
1624+
def test_follow_stats(self):
1625+
uniq = uuid4()
1626+
f = client.feed("user", uniq)
1627+
f.follow("user", uuid4())
1628+
f.follow("user", uuid4())
1629+
f.follow("user", uuid4())
1630+
1631+
client.feed("user", uuid4()).follow("user", uniq)
1632+
client.feed("timeline", uuid4()).follow("user", uniq)
1633+
1634+
feed_id = "user:" + str(uniq)
1635+
response = client.follow_stats(feed_id)["results"]
1636+
self.assertEqual(response["following"]["count"], 3)
1637+
self.assertEqual(response["followers"]["count"], 2)
1638+
1639+
response = client.follow_stats(
1640+
feed_id, followers_slugs=["timeline"], following_slugs=["timeline"]
1641+
)["results"]
1642+
self.assertEqual(response["following"]["count"], 0)
1643+
self.assertEqual(response["followers"]["count"], 1)

0 commit comments

Comments
 (0)