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 all 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
51 changes: 51 additions & 0 deletions tests/api/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,57 @@ def test_get_follows_raises_if_wrong_params_are_passed_in(param, value):
client.users.get_follows('1234', **kwargs)


@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', [
('direction', 'abcd'),
('sort_by', 'abcd'),
])
def test_get_all_follows_raises_if_wrong_params_are_passed_in(param, value):
client = TwitchClient('client id')
kwargs = {param: value}
with pytest.raises(TwitchAttributeException):
client.users.get_all_follows('1234', **kwargs)


@responses.activate
def test_check_follows_channel():
user_id = 1234
Expand Down
29 changes: 26 additions & 3 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,32 @@ 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=DIRECTION_DESC,
sort_by=USERS_SORT_BY_CREATED_AT):
if direction not in DIRECTIONS:
raise TwitchAttributeException(
'Direction is not valid. Valid values are {}'.format(DIRECTIONS))
if sort_by not in USERS_SORT_BY:
raise TwitchAttributeException(
'Sort by is not valud. Valid values are {}'.format(USERS_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:
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 +61,6 @@ 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))

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