Пример #1
0
        /// <summary>
        /// Updates a channel's status. Requires user authorization.
        /// </summary>
        /// <param name="status">The new status</param>
        /// <param name="game">The new game</param>
        /// <returns></returns>
        public async Task <Channel> UpdateChannel(string status, string game)
        {
            if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.ChannelEditor))
            {
                Uri uri;
                uri = new Uri("https://api.twitch.tv/kraken/channels/" + name);
                JObject content = new JObject();
                content["channel"]           = new JObject();
                content["channel"]["status"] = status;
                content["channel"]["game"]   = game;
                string responseString = await Twixel.PutWebData(uri, accessToken, content.ToString());

                if (Twixel.GoodStatusCode(responseString))
                {
                    return(twixel.LoadChannel(JObject.Parse(responseString)));
                }
                else
                {
                    twixel.CreateError(responseString);
                    return(null);
                }
            }
            else
            {
                if (!authorized)
                {
                    twixel.CreateError(name + " is not authorized");
                }
                else if (!authorizedScopes.Contains(TwitchConstants.Scope.ChannelEditor))
                {
                    twixel.CreateError(name + " has not given channel_editor permissions");
                }
                return(null);
            }
        }
Пример #2
0
        /// <summary>
        /// Blocks a user. Requires user authorization.
        /// </summary>
        /// <param name="username">The name of the user to block</param>
        /// <returns>The current list of blocked users</returns>
        public async Task <List <User> > BlockUser(string username)
        {
            if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.UserBlocksEdit))
            {
                Uri uri;
                uri = new Uri("https://api.twitch.tv/kraken/users/" + name + "/blocks/" + username);
                string responseString = await Twixel.PutWebData(uri, accessToken, "");

                User temp = twixel.LoadUser((JObject)JObject.Parse(responseString)["user"]);
                if (!ContainsBlockedUser(temp.name))
                {
                    blockedUsers.Add(temp);
                }

                return(blockedUsers);
            }
            else
            {
                if (!authorized)
                {
                    twixel.CreateError(name + " is not authorized");
                }
                else if (!authorizedScopes.Contains(TwitchConstants.Scope.UserBlocksEdit))
                {
                    twixel.CreateError(name + " has not given user_blocks_edit permissions");
                }
                return(null);
            }
        }
Пример #3
0
 /// <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);
         }
     }
 }
Пример #4
0
 /// <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);
         }
     }
 }
Пример #5
0
 /// <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);
         }
     }
 }
Пример #6
0
        /// <summary>
        /// Follows a channel. Requires user authorization.
        /// </summary>
        /// <param name="channel">The name of the channel</param>
        /// <returns>A channel object</returns>
        public async Task <Channel> FollowChannel(string channel)
        {
            if (authorized && authorizedScopes.Contains(TwitchConstants.Scope.UserFollowsEdit))
            {
                Uri uri;
                uri = new Uri("https://api.twitch.tv/kraken/users/" + name + "/follows/channels/" + channel);
                string responseString = await Twixel.PutWebData(uri, accessToken, "");

                if (responseString != "422")
                {
                    Channel temp = twixel.LoadChannel((JObject)JObject.Parse(responseString)["channel"]);
                    return(temp);
                }
                else if (responseString == "422")
                {
                    twixel.CreateError("Could not follow " + channel);
                    return(null);
                }
                else
                {
                    twixel.CreateError(responseString);
                    return(null);
                }
            }
            else
            {
                if (!authorized)
                {
                    twixel.CreateError(name + " is not authorized");
                }
                else if (!authorizedScopes.Contains(TwitchConstants.Scope.UserFollowsEdit))
                {
                    twixel.CreateError(name + " has not given user_follows_edit permissions");
                }
                return(null);
            }
        }