Example #1
0
        /// <summary>
        /// Uses the handler for only one query / work also for cursor queries
        /// </summary>
        /// <param name="token"></param>
        static void execute_query_error_handler(Token token)
        {
            Token.WebExceptionHandlingDelegate del = delegate(WebException wex)
            {
                Console.WriteLine("You received an execute_query_error!");
                Console.WriteLine(wex.Message);
            };

            dynamic timeline = token.ExecuteQuery("https://api.twitter.com/1/users/contributors.json?user_id=700562792", del);
        }
Example #2
0
        /// <summary>
        /// Get the list of contributors to the account of the current user
        /// Update the matching attribute of the current user if the parameter is true
        /// Return the list of contributors
        /// </summary>
        /// <param name="createContributorList">False by default. Indicates if the _contributors attribute needs to be updated with the result</param>
        /// <returns>The list of contributors to the account of the current user</returns>
        public List <User> GetContributors(bool createContributorList = false)
        {
            // Specific error handler
            // Manage the error 400 thrown when contributors are not enabled by the current user
            Token.WebExceptionHandlingDelegate del = delegate(WebException wex)
            {
                int      indexOfStatus = wex.Response.Headers.AllKeys.ToList().IndexOf("Status");
                string   statusValue   = wex.Response.Headers.Get(indexOfStatus);
                char[]   t             = new char[] { ' ' };
                string[] statusContent = statusValue.Split(t);
                if (statusContent != null && statusContent.Length > 0)
                {
                    switch (statusContent[0])
                    {
                    case "400":
                        // Don't need to do anything, the method will return null
                        Console.WriteLine("Contributors are not enabled for this user");
                        break;

                    default:
                        // Other errors are not managed
                        throw wex;
                    }
                }
                else
                {
                    throw wex;
                }
            };

            List <User> result = null;

            // Contributors can be researched according to the user's id or screen_name
            if (this.id != null)
            {
                result = getContributionObjects(this.token, String.Format(this.query_user_contributors_from_id, this.id), del);
            }
            else if (this.screen_name != null)
            {
                result = getContributionObjects(this.token, String.Format(this.query_user_contributors_from_name, this.screen_name), del);
            }

            // Update the attribute _contributors if required
            if (createContributorList)
            {
                _contributors = result;
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// Update the exception handler attribute with the 3rd parameter
        /// Get the list of users matching the Twitter request url (contributors or contributees)
        /// </summary>
        /// <param name="token"> Current user token to access the Twitter API</param>
        /// <param name="url">Twitter requets URL</param>
        /// <param name="exceptionHandlerDelegate">Delegate method to handle Twitter request exceptions</param>
        /// <returns>Null if the token parameter is null or if the Twitter request fails. A list of users otherwise (contributors or contributees).</returns>
        private List <User> getContributionObjects(Token token, String url, Token.WebExceptionHandlingDelegate exceptionHandlerDelegate = null)
        {
            //
            if (token == null)
            {
                Console.WriteLine("User's token is needed");
                return(null);
            }

            // Update the exception handler
            //token.Integrated_Exception_Handler = false;
            token.ExceptionHandler = exceptionHandlerDelegate;


            dynamic webRequestResult = token.ExecuteQuery(url);

            List <User> result = null;

            if (webRequestResult != null)
            {
                // Create and fill a user list with the data retrieved from Twitter (Dictionary<String, object>[])
                result = new User[] { }.ToList();
                if (webRequestResult is object[])
                {
                    object[] retrievedContributors = webRequestResult as object[];
                    foreach (object rc in retrievedContributors)
                    {
                        if (rc is Dictionary <String, object> )
                        {
                            Dictionary <String, object> userInfo = rc as Dictionary <String, object>;
                            User u = new User();
                            u.fillUser(userInfo);
                            result.Add(u);
                        }
                    }
                }
            }

            return(result);
        }