Skip to content

Commit 397ed62

Browse files
committed
Move endpoint validation to http
Moving poll and schedule validation to http in case people access these methods directly.
1 parent d397e9d commit 397ed62

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

twitchio/http.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,14 @@ async def get_channel_schedule(
706706
utc_offset: Optional[int] = None,
707707
first: int = 20,
708708
):
709+
if first > 25 or first < 1:
710+
raise ValueError("The parameter 'first' was malformed: the value must be less than or equal to 25")
711+
if segment_ids is not None and len(segment_ids) > 100:
712+
raise ValueError("segment_id can only have 100 entries")
713+
if start_time:
714+
start_time = start_time.strftime("%Y-%m-%dT%H:%M:%SZ")
715+
if utc_offset:
716+
utc_offset = str(utc_offset)
709717
q = [
710718
x
711719
for x in [
@@ -860,7 +868,12 @@ async def get_polls(
860868
poll_ids: Optional[List[str]] = None,
861869
first: Optional[int] = 20,
862870
):
871+
if poll_ids and len(poll_ids) > 100:
872+
raise ValueError("poll_ids can only have up to 100 entries")
873+
if first and (first > 25 or first < 1):
874+
raise ValueError("first can only be between 1 and 20")
863875
q = [("broadcaster_id", broadcaster_id), ("first", first)]
876+
864877
if poll_ids:
865878
q.extend(("id", poll_id) for poll_id in poll_ids)
866879
return await self.request(Route("GET", "polls", query=q, token=token), paginate=False, full_body=True)
@@ -877,6 +890,20 @@ async def post_poll(
877890
channel_points_voting_enabled: Optional[bool] = False,
878891
channel_points_per_vote: Optional[int] = None,
879892
):
893+
894+
if len(title) > 60:
895+
raise ValueError("title must be less than or equal to 60 characters")
896+
if len(choices) < 2 or len(choices) > 5:
897+
raise ValueError("You must have between 2 and 5 choices")
898+
for c in choices:
899+
if len(c) > 25:
900+
raise ValueError("choice title must be less than or equal to 25 characters")
901+
if duration < 15 or duration > 1800:
902+
raise ValueError("duration must be between 15 and 1800 seconds")
903+
if bits_per_vote and bits_per_vote > 10000:
904+
raise ValueError("bits_per_vote must bebetween 0 and 10000")
905+
if channel_points_per_vote and channel_points_per_vote > 1000000:
906+
raise ValueError("channel_points_per_vote must bebetween 0 and 1000000")
880907
body = {
881908
"broadcaster_id": broadcaster_id,
882909
"title": title,

twitchio/user.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -777,25 +777,13 @@ async def fetch_schedule(
777777
"""
778778
from .models import Schedule
779779

780-
if first > 25 or first < 1:
781-
raise ValueError("The parameter 'first' was malformed: the value must be less than or equal to 25")
782-
if segment_ids is not None and len(segment_ids) > 100:
783-
raise ValueError("segment_id can only have 100 entries")
784-
785-
if start_time:
786-
start_time = start_time.strftime("%Y-%m-%dT%H:%M:%SZ")
787-
788-
if utc_offset:
789-
utc_offset = str(utc_offset)
790-
791780
data = await self._http.get_channel_schedule(
792781
broadcaster_id=str(self.id),
793782
segment_ids=segment_ids,
794783
start_time=start_time,
795784
utc_offset=utc_offset,
796785
first=first,
797786
)
798-
799787
return Schedule(self._http, data)
800788

801789
async def fetch_channel_teams(self):
@@ -835,11 +823,6 @@ async def fetch_polls(self, token: str, poll_ids: Optional[List[str]] = None, fi
835823
"""
836824
from .models import Poll
837825

838-
if poll_ids and len(poll_ids) > 100:
839-
raise ValueError("poll_ids can only have up to 100 entries")
840-
if first and (first > 25 or first < 1):
841-
raise ValueError("first can only be between 1 and 20")
842-
843826
data = await self._http.get_polls(broadcaster_id=str(self.id), token=token, poll_ids=poll_ids, first=first)
844827
return [Poll(self._http, x) for x in data["data"]] if data["data"] else None
845828

@@ -883,20 +866,6 @@ async def create_poll(
883866
"""
884867
from .models import Poll
885868

886-
if len(title) > 60:
887-
raise ValueError("title must be less than or equal to 60 characters")
888-
if len(choices) < 2 or len(choices) > 5:
889-
raise ValueError("You must have between 2 and 5 choices")
890-
for c in choices:
891-
if len(c) > 25:
892-
raise ValueError("choice title must be less than or equal to 25 characters")
893-
if duration < 15 or duration > 1800:
894-
raise ValueError("duration must be between 15 and 1800 seconds")
895-
if bits_per_vote and bits_per_vote > 10000:
896-
raise ValueError("bits_per_vote must bebetween 0 and 10000")
897-
if channel_points_per_vote and channel_points_per_vote > 1000000:
898-
raise ValueError("channel_points_per_vote must bebetween 0 and 1000000")
899-
900869
data = await self._http.post_poll(
901870
broadcaster_id=str(self.id),
902871
token=token,

0 commit comments

Comments
 (0)