/// <summary> /// Blocks a user. /// Requires authorization. /// Requires user_blocks_edit. /// </summary> /// <param name="username">The name of the user to block</param> /// <returns>The blocked user</returns> public async Task <Block> BlockUser(string username) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.UserBlocksEdit; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("users", name, "blocks", username); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.PutWebData(uri, accessToken, "", version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } return(HelperMethods.LoadBlock(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Gets the list of users that can edit this user's channel. /// Requires authorization. /// Requires channel_read. /// </summary> /// <returns>A list of users</returns> public async Task <List <User> > RetrieveChannelEditors() { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelRead; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "editors"); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } return(HelperMethods.LoadUsers(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Get a Total object containing a list of Subscription of type User /// Requires authorization. /// Requires Twitch partnership. /// Requires channel_subscriptions. /// </summary> /// <param name="offset">Object offset for pagination. Default is 0.</param> /// <param name="limit">How many subscriptions to get at one time. Default is 25. Maximum is 100</param> /// <param name="direction">Creation date sorting direction. Default is Ascending.</param> /// <returns>A Total object containing a list of Subscription objects of type User</returns> public async Task <Total <List <Subscription <User> > > > RetriveSubscribers(int offset = 0, int limit = 25, TwitchConstants.Direction direction = TwitchConstants.Direction.Ascending) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelSubscriptions; if (authorized && authorizedScopes.Contains(relevantScope) && partnered) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "subscriptions"); if (limit <= 100) { url.SetQueryParam("limit", limit); } else { url.SetQueryParam("limit", 100); } url.SetQueryParams(new { offset = offset, direction = TwitchConstants.DirectionToString(direction) }); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } JObject responseObject = JObject.Parse(responseString); List <Subscription <User> > subs = HelperMethods.LoadUserSubscriptions(JObject.Parse(responseString), version); return(HelperMethods.LoadTotal(responseObject, subs, version)); } 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 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> /// Unfollows a channel. /// Requires authorization. /// Requires user_follows_edit. /// </summary> /// <param name="channel">The name of the channel</param> /// <returns> /// Returns true the request succeeded. /// Throws an exception if the channel was not being followed. /// Throws an exception if the channel could not be unfollowed. /// </returns> public async Task <bool> UnfollowChannel(string channel) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.UserFollowsEdit; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("users", name, "follows", "channels", channel); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.DeleteWebData(uri, accessToken, version); } catch (TwitchException ex) { if (ex.Status == 404) { throw new TwixelException(channel + " was not being followed.", ex); } else if (ex.Status == 422) { throw new TwixelException(channel + " could not be unfollowed. Try again.", 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 { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Updates a channel's status. /// Updates channel object. /// Requires authorization. /// Requires Twitch partnership if setting a delay above 0. /// Requires channel_editor. /// </summary> /// <param name="status">The new status</param> /// <param name="game">The new game</param> /// <param name="delay">Delay, requires Twitch partnership if above 0</param> /// <returns> /// Returns the channel if the request succeeded. /// Throws an exception if the user is not allowed to use delay. /// </returns> public async Task <Channel> UpdateChannel(string status = "", string game = "", int delay = 0) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelEditor; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name); Uri uri = new Uri(url.ToString()); JObject content = new JObject(); content["channel"] = new JObject(); content["channel"]["status"] = status; content["channel"]["game"] = game; if (version == Twixel.APIVersion.v3) { content["channel"]["delay"] = delay; } string responseString; try { responseString = await Twixel.PutWebData(uri, accessToken, content.ToString(), version); } catch (TwitchException ex) { if (ex.Status == 422) { throw new TwixelException(name + " is not allowed to use delay.", ex); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } channel = HelperMethods.LoadChannel(JObject.Parse(responseString), version); return(channel); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Checks to see if this user is subcribed to a specified channel. /// Requires authorization. /// Requires user_subscriptions. /// </summary> /// <param name="channel">The name of the channel</param> /// <returns> /// A subscription object of type Channel if the request succeeds. /// Throws an exception if the user is not subscribed to the channel. /// Throws an exception if the channel is not partnered.</returns> public async Task <Subscription <Channel> > RetrieveSubscription(string channel) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.UserSubcriptions; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("users", name, "subscriptions", channel); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { if (ex.Status == 404) { throw new TwixelException("You are not subscribed to " + channel, ex); } else if (ex.Status == 422) { throw new TwixelException(channel + " has no subscription program", ex); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } return(HelperMethods.LoadChannelSubscription(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Checks to see if a specified user is subscribed to this user. /// Requires authorization. /// Requires Twitch partnership. /// Requires channel_check_subscription. /// </summary> /// <param name="username">The name of the user</param> /// <returns>A Subscription object of type User if the user is subscribed</returns> public async Task <Subscription <User> > RetrieveSubsciber(string username) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelCheckSubscription; if (authorized && authorizedScopes.Contains(relevantScope) && partnered) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "subcriptions", username); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { if (ex.Status == 404) { throw new TwixelException(username + " is not subscribed.", ex); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } return(HelperMethods.LoadUserSubscription(JObject.Parse(responseString), version)); } 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> /// Resets this user's stream key. /// Updates channel object. /// Requires authorization. /// Requires channel_stream. /// </summary> /// <returns>The new stream key</returns> public async Task <string> ResetStreamKey() { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelStream; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("channels", name, "stream_key"); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.DeleteWebData(uri, accessToken, version); } catch (TwitchException ex) { if (ex.Status == 422) { throw new TwixelException("Error resetting stream key.", ex); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } JObject responseObject = JObject.Parse(responseString); channel = HelperMethods.LoadChannel(responseObject, version); streamKey = (string)responseObject["stream_key"]; return(streamKey); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Gets a list of users that the user has blocked. /// Requires authorization. /// Requires user_blocks_read. /// </summary> /// <param name="offset">Object offset for pagination. Default is 0.</param> /// <param name="limit">Maximum number of objects in array. Default is 25. Maximum is 100.</param> /// <returns>A list of blocked users</returns> public async Task <List <Block> > RetrieveBlockedUsers(int offset = 0, int limit = 25) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.UserBlocksRead; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("users", name, "blocks"); if (limit <= 100) { url.SetQueryParam("limit", limit); } else { url.SetQueryParam("limit", 100); } url.SetQueryParam("offset", offset); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } return(HelperMethods.LoadBlocks(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Follows a channel. /// Requires authorization. /// Requires user_follows_edit. /// </summary> /// <param name="channel">The name of the channel</param> /// <param name="notifications"> /// Whether :user should receive email/push notifications /// (depending on their notification settings) when the specified channel goes live. /// Default is false. /// </param> /// <returns> /// A Follow object of type Channel if the request succeeds. /// Throws an exception if the request was not processed. /// </returns> public async Task <Follow <Channel> > FollowChannel(string channel, bool notifications = false) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.UserFollowsEdit; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("users", name, "follows", "channels", channel).SetQueryParam("notifications", notifications); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.PutWebData(uri, accessToken, "", version); } catch (TwitchException ex) { if (ex.Status == 422) { throw new TwixelException("Could not follow " + channel); } else { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } } return(HelperMethods.LoadChannelFollow(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
/// <summary> /// Get a list of videos from the channels this user follows /// </summary> /// <param name="offset">Object offset for pagination. Default is 0.</param> /// <param name="limit">How many videos to get at once. Default is 25. Maximum is 100.</param> /// <returns>A list of videos.</returns> public async Task <List <Video> > RetrieveFollowedVideos(int offset = 0, int limit = 10) { if (version == Twixel.APIVersion.v3) { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.UserRead; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegments("videos", "followed"); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } return(HelperMethods.LoadVideos(JObject.Parse(responseString), version)); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } } else { throw new TwixelException(TwitchConstants.v2UnsupportedErrorString); } }
/// <summary> /// Gets the channel object for this user. Also retrieves their stream key. /// Updates channel object. /// Requires authorization. /// Requires channel_read. /// </summary> /// <returns>A channel</returns> public async Task <Channel> RetrieveChannel() { TwitchConstants.Scope relevantScope = TwitchConstants.Scope.ChannelRead; if (authorized && authorizedScopes.Contains(relevantScope)) { Url url = new Url(TwitchConstants.baseUrl).AppendPathSegment("channel"); Uri uri = new Uri(url.ToString()); string responseString; try { responseString = await Twixel.GetWebData(uri, accessToken, version); } catch (TwitchException ex) { throw new TwixelException(TwitchConstants.twitchAPIErrorString, ex); } JObject responseObject = JObject.Parse(responseString); streamKey = (string)responseObject["stream_key"]; channel = HelperMethods.LoadChannel(responseObject, version); return(channel); } else { if (!authorized) { throw new TwixelException(NotAuthedError()); } else if (!authorizedScopes.Contains(relevantScope)) { throw new TwixelException(MissingPermissionError(relevantScope)); } else { throw new TwixelException(TwitchConstants.unknownErrorString); } } }
private string MissingPermissionError(TwitchConstants.Scope scope) { return(name + " has not given " + TwitchConstants.ScopeToString(scope) + " permissions."); }