Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow user to get all follows by user #48

Merged
merged 4 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions tests/api/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,72 @@ def test_get_follows():
assert follow.channel.name == example_channel['name']


@responses.activate
def test_get_follows_with_get_all():
user_id = 1234
response = {
'_total': 27,
'follows': [example_follow]
}
responses.add(responses.GET,
'{}users/{}/follows/channels'.format(BASE_URL, user_id),
body=json.dumps(response),
status=200,
content_type='application/json')

client = TwitchClient('client id')

follows = client.users.get_follows(user_id, get_all=True)

assert len(responses.calls) == 1
assert len(follows) == 1
follow = follows[0]
assert isinstance(follow, Follow)
assert follow.notifications == example_follow['notifications']
assert isinstance(follow.channel, Channel)
assert follow.channel.id == example_channel['_id']
assert follow.channel.name == example_channel['name']


@responses.activate
def test__get_all_follows():
user_id = 1234
response_with_offset = {
'_total': 27,
'_offset': 1234,
'follows': [example_follow]
}
response_without_offset = {
'_total': 27,
'follows': [example_follow]
}
responses.add(responses.GET,
'{}users/{}/follows/channels'.format(BASE_URL, user_id),
body=json.dumps(response_with_offset),
status=200,
content_type='application/json')
responses.add(responses.GET,
'{}users/{}/follows/channels'.format(BASE_URL, user_id),
body=json.dumps(response_without_offset),
status=200,
content_type='application/json')

client = TwitchClient('client id')

follows = client.users._get_all_follows(user_id,
direction='desc',
sort_by='last_broadcast')

assert len(responses.calls) == 2
assert len(follows) == 2
follow = follows[0]
assert isinstance(follow, Follow)
assert follow.notifications == example_follow['notifications']
assert isinstance(follow.channel, Channel)
assert follow.channel.id == example_channel['_id']
assert follow.channel.name == example_channel['name']


@responses.activate
@pytest.mark.parametrize('param,value', [
('limit', 101),
Expand Down
26 changes: 22 additions & 4 deletions twitch/api/users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from twitch.api.base import TwitchAPI
from twitch.constants import DIRECTIONS, DIRECTION_DESC, USERS_SORT_BY, USERS_SORT_BY_CREATED_AT
from twitch.constants import (DIRECTIONS, DIRECTION_DESC, MAX_FOLLOWS_LIMIT,
USERS_SORT_BY, USERS_SORT_BY_CREATED_AT)
from twitch.decorators import oauth_required
from twitch.exceptions import TwitchAttributeException
from twitch.resources import Follow, Subscription, User, UserBlock
Expand All @@ -26,9 +27,25 @@ def check_subscribed_to_channel(self, user_id, channel_id):
response = self._request_get('users/{}/subscriptions/{}'.format(user_id, channel_id))
return Subscription.construct_from(response)

def _get_all_follows(self, user_id, direction, sort_by):
offset = 0
params = {
'limit': MAX_FOLLOWS_LIMIT,
'direction': direction
}
follows = []
while offset is not None:
params.update({
'offset': offset
})
response = self._request_get('users/{}/follows/channels'.format(user_id), params=params)
offset = response.get('_offset')
follows.extend(response['follows'])
return [Follow.construct_from(x) for x in follows]

def get_follows(self, user_id, limit=25, offset=0, direction=DIRECTION_DESC,
sort_by=USERS_SORT_BY_CREATED_AT):
if limit > 100:
sort_by=USERS_SORT_BY_CREATED_AT, get_all=False):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than changing a get_follows function and adding a parameter that changes how the function behaves, could you create another function get_all_follows or something like that, that would return all follows by a user?
It's much nicer to have two functions than one being changed by a parameter.

if limit > MAX_FOLLOWS_LIMIT:
raise TwitchAttributeException(
'Maximum number of objects returned in one request is 100')
if direction not in DIRECTIONS:
Expand All @@ -37,7 +54,8 @@ def get_follows(self, user_id, limit=25, offset=0, direction=DIRECTION_DESC,
if sort_by not in USERS_SORT_BY:
raise TwitchAttributeException(
'Sort by is not valud. Valid values are {}'.format(USERS_SORT_BY))

if get_all:
return self._get_all_follows(user_id, direction, sort_by)
params = {
'limit': limit,
'offset': offset,
Expand Down
1 change: 1 addition & 0 deletions twitch/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
USERS_SORT_BY_LAST_BROADCAST,
USERS_SORT_BY_LOGIN
]
MAX_FOLLOWS_LIMIT = 100

DIRECTION_ASC = 'asc'
DIRECTION_DESC = 'desc'
Expand Down