/// <summary> /// Starts a commercial on this user's live stream. /// Requires authorization. /// Requires Twitch partnership. /// Requires channel_commercial. /// </summary> /// <param name="length">The length of the commercial</param> /// <returns> /// Returns true if the request succeeded. /// Throws an exception if the user is not partnered. /// </returns> public async Task <bool> StartCommercial(TwitchConstants.CommercialLength length) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelCommercial; if (authorized && authorizedScopes.Contains(relevantScope) && partnered) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "commercial"); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.PostWebData(uri, accessToken, "length=" + TwitchConstants.LengthToInt(length).ToString(), version); } catch (TwitchException ex) { if (ex.Status == 422) { throw new TwixelException(ex.Message, ex); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } if (string.IsNullOrEmpty(responseString)) { return(true); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else if (!partnered) { throw new TwixelException(NotPartneredError()); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Starts a commercial on this user's live stream. Requires user authorization. Requires Twitch partnership /// </summary> /// <param name="length">The length of the commercial</param> /// <returns>If the request succeeded</returns> public async Task <bool> StartCommercial(TwitchConstants.Length length) { if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.ChannelCommercial)) { Uri uri; uri = new Uri("https://api.twitch.tv/kraken/channels/test_user1/commercial"); string responseString = await Twixel.PostWebData(uri, accessToken, "length=" + TwitchConstants.LengthToInt(length).ToString()); if (responseString == "") { return(true); } else if (responseString == "422") { twixel.CreateError("You are not partnered so you cannot run commercials"); return(false); } else { twixel.CreateError(responseString); return(false); } } else { if (!authorized) { twixel.CreateError(name + " is not authorized"); } else if (!authorizedScopes.Contains(TwitchConstants.Scope.ChannelCommercial)) { twixel.CreateError(name + " has not given channel_commercial permissions"); } return(false); } }