Exemplo n.º 1
0
        /// <summary>
        /// Lets logged-in user follow another user.
        /// </summary>
        /// <param name="screenName">Screen name of user to follow</param>
        /// <param name="follow">Receive notifications for the followed friend</param>
        /// <returns>followed friend user info</returns>
        public async Task <User?> CreateFriendshipAsync(string screenName, bool follow, CancellationToken cancelToken = default(CancellationToken))
        {
            if (string.IsNullOrWhiteSpace(screenName))
            {
                throw new ArgumentException("screenName is a required parameter.", "screenName");
            }

            string destroyUrl = BaseUrl + "friendships/create.json";

            var createParams = new Dictionary <string, string?>
            {
                { "screen_name", screenName }
            };

            // If follow exists in the parameter list, Twitter will
            // always treat it as true, even if the value is false;
            // Therefore, only add follow if it is true.
            if (follow)
            {
                createParams.Add("follow", "true");
            }

            var reqProc = new FriendshipRequestProcessor <User>();

            RawResult =
                await TwitterExecutor.PostFormUrlEncodedToTwitterAsync <User>(
                    HttpMethod.Post.ToString(),
                    destroyUrl,
                    createParams,
                    cancelToken)
                .ConfigureAwait(false);

            return(reqProc.ProcessActionResult(RawResult, FriendshipAction.Create));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Lets logged-in user follow another user.
        /// </summary>
        /// <param name="userID">ID of user to follow</param>
        /// <param name="follow">Receive notifications for the followed friend</param>
        /// <returns>followed friend user info</returns>
        public async Task <User> CreateFriendshipAsync(ulong userID, bool follow, CancellationToken cancelToken = default(CancellationToken))
        {
            if (userID == 0)
            {
                throw new ArgumentException("userID is a required parameter.", "userID");
            }

            string destroyUrl = BaseUrl + "friendships/create.json";

            var createParams = new Dictionary <string, string>
            {
                { "user_id", userID.ToString() }
            };

            // If follow exists in the parameter list, Twitter will
            // always treat it as true, even if the value is false;
            // Therefore, only add follow if it is true.
            if (follow)
            {
                createParams.Add("follow", "true");
            }

            var reqProc = new FriendshipRequestProcessor <User>();

            RawResult =
                await TwitterExecutor.PostToTwitterAsync <User>(
                    destroyUrl,
                    createParams,
                    cancelToken)
                .ConfigureAwait(false);

            return(reqProc.ProcessActionResult(RawResult, FriendshipAction.Create));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Lets logged-in user set retweets and/or device notifications for a follower.
        /// </summary>
        /// <param name="userID">Twitter's ID for user</param>
        /// <param name="screenName">screen name of user to update</param>
        /// <param name="retweets">Enable retweets</param>
        /// <param name="device">Receive notifications</param>
        /// <returns>updated friend user info</returns>
        async Task <Friendship?> UpdateFriendshipSettingsAsync(ulong userID, string?screenName, bool retweets, bool device, CancellationToken cancelToken = default(CancellationToken))
        {
            var parms = new Dictionary <string, string?>
            {
                { "retweets", retweets.ToString().ToLower() },
                { "device", device.ToString().ToLower() }
            };

            if (screenName != null)
            {
                parms.Add("screen_name", screenName);
            }
            if (userID > 0)
            {
                parms.Add("user_id", userID.ToString());
            }

            string updateUrl = BaseUrl + "friendships/update.json";

            var reqProc = new FriendshipRequestProcessor <Friendship>();

            RawResult =
                await TwitterExecutor.PostFormUrlEncodedToTwitterAsync <Friendship>(
                    HttpMethod.Post.ToString(),
                    updateUrl,
                    parms,
                    cancelToken)
                .ConfigureAwait(false);

            return(reqProc.ProcessActionResult(RawResult, FriendshipAction.Update));
        }
Exemplo n.º 4
0
        /// <summary>
        /// lets logged-in user follow another user
        /// </summary>
        /// <param name="userID">Numeric ID of user to follow</param>
        /// <param name="screenName">Screen name of user to follow</param>
        /// <param name="follow">Receive notifications for the followed friend</param>
        /// <param name="callback">Async Callback used in Silverlight queries</param>
        /// <returns>followed friend user info</returns>
        public static User CreateFriendship(this TwitterContext ctx, string userID, string screenName, bool follow, Action <TwitterAsyncResponse <User> > callback)
        {
            if (string.IsNullOrEmpty(userID) &&
                string.IsNullOrEmpty(screenName))
            {
                throw new ArgumentException("Either userID or screenName is a required parameter.", "UserIDOrScreenName");
            }

            string destroyUrl = ctx.BaseUrl + "friendships/create.json";

            var createParams = new Dictionary <string, string>
            {
                { "user_id", userID },
                { "screen_name", screenName }
            };

            // If follow exists in the parameter list, Twitter will
            // always treat it as true, even if the value is false;
            // Therefore, only add follow if it is true.
            if (follow)
            {
                createParams.Add("follow", "true");
            }

            var reqProc = new FriendshipRequestProcessor <User>();

            ITwitterExecute twitExe = ctx.TwitterExecutor;

            twitExe.AsyncCallback = callback;
            var resultsJson =
                twitExe.PostToTwitter(
                    destroyUrl,
                    createParams,
                    response => reqProc.ProcessActionResult(response, FriendshipAction.Create));

            User results = reqProc.ProcessActionResult(resultsJson, FriendshipAction.Create);

            return(results);
        }
Exemplo n.º 5
0
        /// <summary>
        /// lets logged-in user set retweets and/or device notifications for a follower
        /// </summary>
        /// <param name="userID">Twitter's ID for user</param>
        /// <param name="screenName">screen name of user to update</param>
        /// <param name="retweets">Enable retweets</param>
        /// <param name="device">Receive notifications</param>
        /// <param name="callback">Async Callback used in Silverlight queries</param>
        /// <returns>updated friend user info</returns>
        public static Friendship UpdateFriendshipSettings(this TwitterContext ctx, ulong userID, string screenName, bool retweets, bool device, Action <TwitterAsyncResponse <Friendship> > callback)
        {
            if (string.IsNullOrEmpty(screenName) && userID <= 0)
            {
                throw new ArgumentNullException("screenNameOrUserID", "Either screenName or UserID is a required parameter.");
            }

            var parms = new Dictionary <string, string>
            {
                { "retweets", retweets.ToString().ToLower() },
                { "device", device.ToString().ToLower() }
            };

            if (screenName != null)
            {
                parms.Add("screen_name", screenName);
            }
            if (userID > 0)
            {
                parms.Add("user_id", userID.ToString());
            }

            string updateUrl = ctx.BaseUrl + "friendships/update.json";

            var reqProc = new FriendshipRequestProcessor <Friendship>();

            ITwitterExecute twitExe = ctx.TwitterExecutor;

            twitExe.AsyncCallback = callback;
            var resultsJson =
                twitExe.PostToTwitter(
                    updateUrl,
                    parms,
                    response => reqProc.ProcessActionResult(response, FriendshipAction.Update));

            Friendship results = reqProc.ProcessActionResult(resultsJson, FriendshipAction.Update);

            return(results);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Lets logged-in user un-follow another user.
        /// </summary>
        /// <param name="userID">ID of user to unfollow</param>
        /// <returns>followed friend user info</returns>
        public async Task <User> DestroyFriendshipAsync(ulong userID, CancellationToken cancelToken = default(CancellationToken))
        {
            if (userID == 0)
            {
                throw new ArgumentException("userID is a required parameter.", "userID");
            }

            string destroyUrl = BaseUrl + "friendships/destroy.json";

            var reqProc = new FriendshipRequestProcessor <User>();

            RawResult =
                await TwitterExecutor.PostToTwitterAsync <User>(
                    destroyUrl,
                    new Dictionary <string, string>
            {
                { "user_id", userID.ToString() }
            },
                    cancelToken)
                .ConfigureAwait(false);

            return(reqProc.ProcessActionResult(RawResult, FriendshipAction.Destroy));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Lets logged-in user un-follow another user.
        /// </summary>
        /// <param name="screenName">Screen name of user to unfollow</param>
        /// <returns>followed friend user info</returns>
        public async Task <User> DestroyFriendshipAsync(string screenName, CancellationToken cancelToken = default(CancellationToken))
        {
            if (string.IsNullOrWhiteSpace(screenName))
            {
                throw new ArgumentException("screenName is a required parameter.", "screenName");
            }

            string destroyUrl = BaseUrl + "friendships/destroy.json";

            var reqProc = new FriendshipRequestProcessor <User>();

            RawResult =
                await TwitterExecutor.PostToTwitterAsync <User>(
                    destroyUrl,
                    new Dictionary <string, string>
            {
                { "screen_name", screenName }
            },
                    cancelToken)
                .ConfigureAwait(false);

            return(reqProc.ProcessActionResult(RawResult, FriendshipAction.Destroy));
        }
Exemplo n.º 8
0
        /// <summary>
        /// lets logged-in user follow another user
        /// </summary>
        /// <param name="userID">Numeric ID of user to unfollow</param>
        /// <param name="screenName">Screen name of user to unfollow</param>
        /// <param name="callback">Async Callback used in Silverlight queries</param>
        /// <returns>followed friend user info</returns>
        public static User DestroyFriendship(this TwitterContext ctx, string userID, string screenName, Action <TwitterAsyncResponse <User> > callback)
        {
            if (string.IsNullOrEmpty(userID) &&
                string.IsNullOrEmpty(screenName))
            {
                throw new ArgumentException("Either id, userID, or screenName is a required parameter.", "UserIDOrScreenName");
            }

            string destroyUrl = ctx.BaseUrl + "friendships/destroy.json";

            var parms = new Dictionary <string, string>();

            if (screenName != null)
            {
                parms.Add("screen_name", screenName);
            }
            if (userID != null)
            {
                parms.Add("user_id", userID);
            }

            var reqProc = new FriendshipRequestProcessor <User>();

            ITwitterExecute twitExe = ctx.TwitterExecutor;

            twitExe.AsyncCallback = callback;
            var resultsJson =
                twitExe.PostToTwitter(
                    destroyUrl,
                    parms,
                    response => reqProc.ProcessActionResult(response, FriendshipAction.Destroy));

            User results = reqProc.ProcessActionResult(resultsJson, FriendshipAction.Destroy);

            return(results);
        }
Exemplo n.º 9
0
        protected internal IRequestProcessor <T> CreateRequestProcessor <T>(string requestType)
            where T : class
        {
            var baseUrl = BaseUrl;
            IRequestProcessor <T> req;

            switch (requestType)
            {
            case "Account":
                req = new AccountRequestProcessor <T>();
                break;

            case "Blocks":
                req = new BlocksRequestProcessor <T>();
                break;

            case "ControlStream":
                req = new ControlStreamRequestProcessor <T>
                {
                    SiteStreamUrl = SiteStreamUrl
                };
                break;

            case "DirectMessage":
                req = new DirectMessageRequestProcessor <T>();
                break;

            case "Favorites":
                req = new FavoritesRequestProcessor <T>();
                break;

            case "Friendship":
                req = new FriendshipRequestProcessor <T>();
                break;

            case "Geo":
                req = new GeoRequestProcessor <T>();
                break;

            case "Help":
                req = new HelpRequestProcessor <T>();
                break;

            case "List":
                req = new ListRequestProcessor <T>();
                break;

            case "Mute":
                req = new MuteRequestProcessor <T>();
                break;

            case "Raw":
                req = new RawRequestProcessor <T>();
                break;

            case "SavedSearch":
                req = new SavedSearchRequestProcessor <T>();
                break;

            case "Search":
                req = new SearchRequestProcessor <T>();
                break;

            case "Status":
                req = new StatusRequestProcessor <T>();
                break;

            case "Streaming":
                baseUrl = StreamingUrl;
                req     = new StreamingRequestProcessor <T>
                {
                    UserStreamUrl   = UserStreamUrl,
                    SiteStreamUrl   = SiteStreamUrl,
                    TwitterExecutor = TwitterExecutor
                };
                break;

            case "Trend":
                req = new TrendRequestProcessor <T>();
                break;

            case "User":
                req = new UserRequestProcessor <T>();
                break;

            case "Vine":
                req = new VineRequestProcessor <T>
                {
                    VineUrl = VineUrl
                };
                break;

            default:
                throw new ArgumentException("Type, " + requestType + " isn't a supported LINQ to Twitter entity.", "requestType");
            }

            if (baseUrl != null)
            {
                req.BaseUrl = baseUrl;
            }

            return(req);
        }
Exemplo n.º 10
0
        protected internal IRequestProcessor <T> CreateRequestProcessor <T>(string requestType)
            where T : class
        {
            string baseUrl = BaseUrl;
            IRequestProcessor <T> req;

            switch (requestType)
            {
            case nameof(Account):
                req = new AccountRequestProcessor <T>();
                break;

            case nameof(AccountActivity):
                req = new AccountActivityRequestProcessor <T>();
                break;

            case nameof(Blocks):
                req = new BlocksRequestProcessor <T>();
                break;

            case nameof(ControlStream):
                req = new ControlStreamRequestProcessor <T>
                {
                    SiteStreamUrl = SiteStreamUrl
                };
                break;

            case nameof(DirectMessage):
                req = new DirectMessageRequestProcessor <T>();
                break;

            case nameof(DirectMessageEvents):
                req = new DirectMessageEventsRequestProcessor <T>();
                break;

            case nameof(Favorites):
                req = new FavoritesRequestProcessor <T>();
                break;

            case nameof(Friendship):
                req = new FriendshipRequestProcessor <T>();
                break;

            case nameof(Geo):
                req = new GeoRequestProcessor <T>();
                break;

            case nameof(Help):
                req = new HelpRequestProcessor <T>();
                break;

            case nameof(List):
                req = new ListRequestProcessor <T>();
                break;

            case nameof(Media):
                req = new MediaRequestProcessor <T>
                {
                    UploadUrl = UploadUrl
                };
                break;

            case nameof(Mute):
                req = new MuteRequestProcessor <T>();
                break;

            case nameof(Raw):
                req = new RawRequestProcessor <T>();
                break;

            case nameof(SavedSearch):
                req = new SavedSearchRequestProcessor <T>();
                break;

            case nameof(Search):
                req = new SearchRequestProcessor <T>();
                break;

            case nameof(Status):
                req = new StatusRequestProcessor <T>();
                break;

            case nameof(Streaming):
                baseUrl = StreamingUrl;
                req     = new StreamingRequestProcessor <T>
                {
                    UserStreamUrl   = UserStreamUrl,
                    SiteStreamUrl   = SiteStreamUrl,
                    TwitterExecutor = TwitterExecutor
                };
                break;

            case nameof(Trend):
                req = new TrendRequestProcessor <T>();
                break;

            case nameof(User):
                req = new UserRequestProcessor <T>();
                break;

            case nameof(Vine):
                req = new VineRequestProcessor <T>
                {
                    VineUrl = VineUrl
                };
                break;

            case nameof(WelcomeMessage):
                req = new WelcomeMessageRequestProcessor <T>();
                break;

            default:
                throw new ArgumentException($"Type, {requestType} isn't a supported LINQ to Twitter entity.", nameof(requestType));
            }

            if (baseUrl != null)
            {
                req.BaseUrl = baseUrl;
            }

            return(req);
        }
Exemplo n.º 11
0
        /// <summary>
        /// factory method for returning a request processor
        /// </summary>
        /// <typeparam name="T">type of request</typeparam>
        /// <returns>request processor matching type parameter</returns>
        private IRequestProcessor CreateRequestProcessor(Expression expression, bool isEnumerable)
        {
            string requestType = string.Empty;

            if (expression != null)
            {
                if (isEnumerable)
                {
                    requestType = expression.Type.GetGenericArguments()[0].Name;
                }
                else
                {
                    requestType = expression.Type.Name;
                }
            }

            IRequestProcessor req;

            switch (requestType)
            {
            case "Account":
                req = new AccountRequestProcessor()
                {
                    BaseUrl = BaseUrl
                };
                break;

            case "Blocks":
                req = new BlocksRequestProcessor()
                {
                    BaseUrl = BaseUrl
                };
                break;

            case "DirectMessage":
                req = new DirectMessageRequestProcessor()
                {
                    BaseUrl = BaseUrl
                };
                break;

            case "Favorites":
                req = new FavoritesRequestProcessor()
                {
                    BaseUrl = BaseUrl
                };
                break;

            case "Friendship":
                req = new FriendshipRequestProcessor()
                {
                    BaseUrl = BaseUrl
                };
                break;

            case "SocialGraph":
                req = new SocialGraphRequestProcessor()
                {
                    BaseUrl = BaseUrl
                };
                break;

            case "Search":
                req = new SearchRequestProcessor()
                {
                    BaseUrl = SearchUrl
                };
                break;

            case "Status":
                req = new StatusRequestProcessor()
                {
                    BaseUrl = BaseUrl
                };
                break;

            case "Trend":
                req = new TrendRequestProcessor()
                {
                    BaseUrl = SearchUrl
                };
                break;

            case "User":
                req = new UserRequestProcessor()
                {
                    BaseUrl = BaseUrl
                };
                break;

            default:
                req = new StatusRequestProcessor()
                {
                    BaseUrl = BaseUrl
                };
                break;
            }

            Debug.Assert(req != null, "You you must assign a value to req.");

            return(req);
        }