/// <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.PostToTwitterAsync<User>(
                    destroyUrl,
                    createParams,
                    cancelToken)
                    .ConfigureAwait(false);

            return reqProc.ProcessActionResult(RawResult, FriendshipAction.Create);
        }
Пример #2
0
        public void ProcessResults_Parses_Show_Response()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship> {
                Type = FriendshipType.Show
            };

            List <Friendship> friends = friendReqProc.ProcessResults(RelationshipResponse);

            Assert.NotNull(friends);
            Assert.Single(friends);
            var friend = friends.Single();

            Assert.NotNull(friend);
            var tgtRel = friend.TargetRelationship;

            Assert.NotNull(tgtRel);
            Assert.Equal("JoeMayo", tgtRel.ScreenName);
            Assert.True(tgtRel.FollowedBy);
            Assert.Equal("15411837", tgtRel.ID);
            Assert.False(tgtRel.Following);
            var srcRel = friend.SourceRelationship;

            Assert.NotNull(srcRel);
            Assert.Equal("Linq2Tweeter", srcRel.ScreenName);
            Assert.True(srcRel.RetweetsWanted);
            Assert.False(srcRel.AllReplies);
            Assert.False(srcRel.MarkedSpam);
            Assert.False(srcRel.FollowedBy);
            Assert.Equal("16761255", srcRel.ID);
            Assert.False((bool)srcRel.Blocking);
            Assert.True((bool)srcRel.NotificationsEnabled);
            Assert.True(srcRel.Following);
            Assert.False(srcRel.CanDm);
        }
        void TestParsingIds(FriendshipType friendType)
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship> { Type = friendType };

            List<Friendship> friends = friendReqProc.ProcessResults(IdsResponse);

            Assert.IsNotNull(friends);
            Assert.IsNotNull(friends.SingleOrDefault());
            var friend = friends.Single();
            Assert.IsNotNull(friend);
            var cursor = friend.CursorMovement;
            Assert.IsNotNull(cursor);
            Assert.AreEqual(2, cursor.Previous);
            Assert.AreEqual(1, cursor.Next);
            var idList = friend.IDInfo;
            Assert.IsNotNull(idList);
            var idsCursor = idList.CursorMovement;
            Assert.IsNotNull(idsCursor);
            Assert.AreEqual(2, idsCursor.Previous);
            Assert.AreEqual(1, idsCursor.Next);
            var ids = idList.IDs;
            Assert.IsNotNull(ids);
            Assert.IsTrue(ids.Any());
            var id = ids.First();
            Assert.AreEqual(5676142ul, id);
        }
        public void ProcessResults_Parses_Show_Response()
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship> { Type = FriendshipType.Show };

            List<Friendship> friends = friendReqProc.ProcessResults(RelationshipResponse);

            Assert.NotNull(friends);
            Assert.Single(friends);
            var friend = friends.Single();
            Assert.NotNull(friend);
            var tgtRel = friend.TargetRelationship;
            Assert.NotNull(tgtRel);
            Assert.Equal("JoeMayo", tgtRel.ScreenName);
            Assert.True(tgtRel.FollowedBy);
            Assert.Equal("15411837", tgtRel.ID);
            Assert.False(tgtRel.Following);
            var srcRel = friend.SourceRelationship;
            Assert.NotNull(srcRel);
            Assert.Equal("Linq2Tweeter", srcRel.ScreenName);
            Assert.True(srcRel.RetweetsWanted);
            Assert.False(srcRel.AllReplies);
            Assert.False(srcRel.MarkedSpam);
            Assert.False(srcRel.FollowedBy);
            Assert.Equal("16761255", srcRel.ID);
            Assert.False((bool)srcRel.Blocking);
            Assert.True((bool)srcRel.NotificationsEnabled);
            Assert.True(srcRel.Following);
            Assert.False(srcRel.CanDm);
        }
Пример #5
0
        void TestParsingIds(FriendshipType friendType)
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship> {
                Type = friendType
            };

            List <Friendship> friends = friendReqProc.ProcessResults(IdsResponse);

            Assert.NotNull(friends);
            Assert.Single(friends);
            var friend = friends.Single();

            Assert.NotNull(friend);
            var idList = friend.IDInfo;

            Assert.NotNull(idList);
            var cursor = idList.CursorMovement;

            Assert.NotNull(cursor);
            Assert.Equal("2", cursor.Previous);
            Assert.Equal("1", cursor.Next);
            var ids = idList.IDs;

            Assert.NotNull(ids);
            Assert.NotEmpty(ids);
            var id = ids.First();

            Assert.Equal(5676142ul, id);
        }
Пример #6
0
        public void GetParametersTest()
        {
            FriendshipRequestProcessor target = new FriendshipRequestProcessor()
            {
                BaseUrl = "http://twitter.com/"
            };
            Expression <Func <Friendship, bool> > expression =
                friend =>
                friend.Type == FriendshipType.Exists &&
                friend.SubjectUser == "123" &&
                friend.FollowingUser == "456";
            LambdaExpression lambdaExpression = expression as LambdaExpression;

            var queryParams = target.GetParameters(lambdaExpression);

            Assert.IsTrue(
                queryParams.Contains(
                    new KeyValuePair <string, string>("Type", ((int)FriendshipType.Exists).ToString())));
            Assert.IsTrue(
                queryParams.Contains(
                    new KeyValuePair <string, string>("SubjectUser", "123")));
            Assert.IsTrue(
                queryParams.Contains(
                    new KeyValuePair <string, string>("FollowingUser", "456")));
        }
Пример #7
0
        void TestParsingIds(FriendshipType friendType)
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship> {
                Type = friendType
            };

            List <Friendship> friends = friendReqProc.ProcessResults(IdsResponse);

            Assert.IsNotNull(friends);
            Assert.IsNotNull(friends.SingleOrDefault());
            var friend = friends.Single();

            Assert.IsNotNull(friend);
            var cursor = friend.CursorMovement;

            Assert.IsNotNull(cursor);
            Assert.AreEqual(2, cursor.Previous);
            Assert.AreEqual(1, cursor.Next);
            var idList = friend.IDInfo;

            Assert.IsNotNull(idList);
            var idsCursor = idList.CursorMovement;

            Assert.IsNotNull(idsCursor);
            Assert.AreEqual(2, idsCursor.Previous);
            Assert.AreEqual(1, idsCursor.Next);
            var ids = idList.IDs;

            Assert.IsNotNull(ids);
            Assert.IsTrue(ids.Any());
            var id = ids.First();

            Assert.AreEqual(5676142ul, id);
        }
Пример #8
0
        public void GetParameters_Returns_Parameters()
        {
            FriendshipRequestProcessor <Friendship> friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1/"
            };
            Expression <Func <Friendship, bool> > expression =
                friend =>
                friend.Type == FriendshipType.Show &&
                friend.SourceUserID == "1" &&
                friend.SourceScreenName == "Name" &&
                friend.TargetUserID == "2" &&
                friend.TargetScreenName == "Name" &&
                friend.Count == 20 &&
                friend.Cursor == "-1" &&
                friend.ScreenName == "twitter,joemayo" &&
                friend.UserID == "123,456" &&
                friend.SkipStatus == true &&
                friend.IncludeUserEntities == true;
            var lambdaExpression = expression as LambdaExpression;

            var queryParams = friendReqProc.GetParameters(lambdaExpression);

            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("Type", ((int)FriendshipType.Show).ToString())));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("SourceUserID", "1")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("SourceScreenName", "Name")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("TargetUserID", "2")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("TargetScreenName", "Name")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("Count", "20")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("Cursor", "-1")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("ScreenName", "twitter,joemayo")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("UserID", "123,456")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("SkipStatus", "True")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair <string, string>("IncludeUserEntities", "True")));
        }
Пример #9
0
        public void ProcessResultsTest()
        {
            FriendshipRequestProcessor <Friendship> target = new FriendshipRequestProcessor <Friendship>();
            XElement twitterResponse = XElement.Parse(m_testQueryResponse);
            bool     expected        = true;
            IList    actual          = target.ProcessResults(twitterResponse);
            var      isFriend        = actual.Cast <Friendship>().First().IsFriend;

            Assert.AreEqual(expected, actual.Cast <Friendship>().First().IsFriend);
        }
Пример #10
0
        public void ProcessActionResult_Translates_Relationships_From_UpdateSettings()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship>();

            Friendship friend = friendReqProc.ProcessActionResult(RelationshipResponse, FriendshipAction.Update);

            var srcRel = friend.SourceRelationship;

            Assert.Equal(true, srcRel.RetweetsWanted);
            Assert.Equal(true, srcRel.NotificationsEnabled);
        }
Пример #11
0
        public void ProcessResults_Returns_Empty_Collection_When_Empty_Results()
        {
            var reqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "http://api.twitter.com/1/"
            };

            List <Friendship> friendships = reqProc.ProcessResults(string.Empty);

            Assert.Empty(friendships);
        }
Пример #12
0
        public void BuildUrl_Throws_On_Null_Parameters()
        {
            FriendshipRequestProcessor <Friendship> friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1/"
            };
            Dictionary <string, string> parameters = null;

            var ex = Assert.Throws <ArgumentException>(() => friendReqProc.BuildUrl(parameters));

            Assert.Equal("Type", ex.ParamName);
        }
 public void BuildIncomingURLTest()
 {
     FriendshipRequestProcessor<Friendship> target = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1/" };
     Dictionary<string, string> parameters =
         new Dictionary<string, string>
         {
             { "Type", FriendshipType.Incoming.ToString() }
         };
     string expected = "https://api.twitter.com/1/friendships/incoming.xml";
     string actual;
     actual = target.BuildURL(parameters);
     Assert.AreEqual(expected, actual);
 }
Пример #14
0
        public void BuildUrl_Throws_When_Missing_Type()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1.1/"
            };
            var parameters = new Dictionary <string, string> {
            };

            var ex = L2TAssert.Throws <ArgumentException>(() => friendReqProc.BuildUrl(parameters));

            Assert.AreEqual("Type", ex.ParamName);
        }
 public void BuildExistsURLTest()
 {
     FriendshipRequestProcessor<Friendship> target = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "http://twitter.com/" };
     Dictionary<string, string> parameters =
         new Dictionary<string, string>
         {
             { "Type", FriendshipType.Exists.ToString() },
             { "SubjectUser", "123" },
             { "FollowingUser", "456" }
         };
     string expected = "http://twitter.com/friendships/exists.xml?user_a=123&user_b=456";
     string actual;
     actual = target.BuildURL(parameters);
     Assert.AreEqual(expected, actual);
 }
Пример #16
0
        public void BuildUrl_Throws_Without_FriendsReqProcUrl()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1/"
            };
            var parameters = new Dictionary <string, string>
            {
                { "Type", FriendshipType.Show.ToString() },
                { "SourceUserID", "123" },
                { "SourceScreenName", "JoeMayo" },
            };

            Assert.Throws <ArgumentException>(() => friendReqProc.BuildUrl(parameters));
        }
Пример #17
0
 public void BuildShowWithoutTargetURLTest()
 {
     FriendshipRequestProcessor <Friendship> target = new FriendshipRequestProcessor <Friendship>()
     {
         BaseUrl = "http://twitter.com/"
     };
     Dictionary <string, string> parameters =
         new Dictionary <string, string>
     {
         { "Type", FriendshipType.Show.ToString() },
         { "SourceUserID", "123" },
         { "SourceScreenName", "JoeMayo" },
     };
     string actual = target.BuildURL(parameters);
 }
Пример #18
0
        public void BuildUrl_Creates_No_Retweet_Url()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "http://api.twitter.com/1.1/"
            };
            var parameters = new Dictionary <string, string>
            {
                { "Type", FriendshipType.NoRetweetIDs.ToString() }
            };
            string expected = "http://api.twitter.com/1.1/friendships/no_retweets/ids.json";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.AreEqual(expected, req.FullUrl);
        }
Пример #19
0
        public void BuildUrl_Constructs_Outgoing_Url()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1.1/"
            };
            var parameters = new Dictionary <string, string>
            {
                { "Type", FriendshipType.Outgoing.ToString() }
            };
            string expected = "https://api.twitter.com/1.1/friendships/outgoing.json";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.AreEqual(expected, req.FullUrl);
        }
Пример #20
0
        public void BuildLookupUrl_Requires_ScreenName_Or_UserID()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1.1/"
            };
            var parameters = new Dictionary <string, string>
            {
                { "Type", FriendshipType.Lookup.ToString() },
                //{ "ScreenName", "twitter,joemayo" }
            };

            var ex = L2TAssert.Throws <ArgumentNullException>(() => friendReqProc.BuildUrl(parameters));

            Assert.AreEqual("ScreenNameOrUserID", ex.ParamName);
        }
Пример #21
0
        public void BuildUrl_Throws_Without_Source_Url()
        {
            FriendshipRequestProcessor <Friendship> friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1/"
            };
            Dictionary <string, string> parameters =
                new Dictionary <string, string>
            {
                { "Type", FriendshipType.Show.ToString() },
                { "friendReqProcUserID", "456" },
                { "friendReqProcScreenName", "LinqToTweeter" }
            };

            Assert.Throws <ArgumentException>(() => friendReqProc.BuildUrl(parameters));
        }
Пример #22
0
        public void ProcessResults_Retains_Original_Input_Params()
        {
            const string SrcUsrID            = "1";
            const string SrcScrNm            = "JoeMayo";
            const string TgtUsrID            = "2";
            const string TgtScrNm            = "MayMayo";
            const int    Count               = 20;
            const string Cursor              = "123";
            const string ScrNm               = "JoeMayo,MayMayo";
            const string UsrID               = "1,2";
            const bool   SkipStatus          = true;
            const bool   IncludeUserEntities = true;
            var          friendReqProc       = new FriendshipRequestProcessor <Friendship>
            {
                Type                = FriendshipType.Lookup,
                SourceUserID        = SrcUsrID,
                SourceScreenName    = SrcScrNm,
                TargetUserID        = TgtUsrID,
                TargetScreenName    = TgtScrNm,
                Count               = Count,
                Cursor              = Cursor,
                ScreenName          = ScrNm,
                UserID              = UsrID,
                SkipStatus          = SkipStatus,
                IncludeUserEntities = IncludeUserEntities
            };

            List <Friendship> friendships = friendReqProc.ProcessResults(LookupResponse);

            Assert.NotNull(friendships);
            Assert.NotEmpty(friendships);
            var friendship = friendships.First();

            Assert.NotNull(friendship);
            Assert.Equal(FriendshipType.Lookup, friendship.Type);
            Assert.Equal(SrcUsrID, friendship.SourceUserID);
            Assert.Equal(SrcScrNm, friendship.SourceScreenName);
            Assert.Equal(TgtUsrID, friendship.TargetUserID);
            Assert.Equal(TgtScrNm, friendship.TargetScreenName);
            Assert.Equal(Count, friendship.Count);
            Assert.Equal(Cursor, friendship.Cursor);
            Assert.Equal(ScrNm, friendship.ScreenName);
            Assert.Equal(UsrID, friendship.UserID);
            Assert.Equal(SkipStatus, friendship.SkipStatus);
            Assert.Equal(IncludeUserEntities, friendship.IncludeUserEntities);
        }
Пример #23
0
        public void BuildUrl_With_Cursor_Param_Constructs_Incoming_Url()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1.1/"
            };
            var parameters = new Dictionary <string, string>
            {
                { "Type", FriendshipType.Incoming.ToString() },
                { "Cursor", "-1" }
            };
            string expected = "https://api.twitter.com/1.1/friendships/incoming.json?cursor=-1";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.AreEqual(expected, req.FullUrl);
        }
Пример #24
0
        public void BuildLookupUrl_Returns_Url_For_ScreenName()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1.1/"
            };
            var parameters = new Dictionary <string, string>
            {
                { "Type", FriendshipType.Lookup.ToString() },
                { "ScreenName", "twitter,joemayo" }
            };
            string expected = "https://api.twitter.com/1.1/friendships/lookup.json?screen_name=twitter%2Cjoemayo";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.AreEqual(expected, req.FullUrl);
        }
Пример #25
0
        public void BuildLookupUrl_Returns_Url_For_UserID()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1.1/"
            };
            var parameters = new Dictionary <string, string>
            {
                { "Type", FriendshipType.Lookup.ToString() },
                { "UserID", "123,456" }
            };
            string expected = "https://api.twitter.com/1.1/friendships/lookup.json?user_id=123%2C456";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.AreEqual(expected, req.FullUrl);
        }
Пример #26
0
        public void BuildOutgoingURLTest()
        {
            FriendshipRequestProcessor <Friendship> target = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1/"
            };
            Dictionary <string, string> parameters =
                new Dictionary <string, string>
            {
                { "Type", FriendshipType.Outgoing.ToString() }
            };
            string expected = "https://api.twitter.com/1/friendships/outgoing.xml";
            string actual;

            actual = target.BuildURL(parameters);
            Assert.AreEqual(expected, actual);
        }
Пример #27
0
        public void BuildIncomingWithCursorURLTest()
        {
            FriendshipRequestProcessor <Friendship> target = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1/"
            };
            Dictionary <string, string> parameters =
                new Dictionary <string, string>
            {
                { "Type", FriendshipType.Incoming.ToString() },
                { "Cursor", "-1" }
            };
            string expected = "https://api.twitter.com/1/friendships/incoming.xml?cursor=-1";
            string actual;

            actual = target.BuildURL(parameters);
            Assert.AreEqual(expected, actual);
        }
        public void ProcessResults_Translates_Relationships_From_LookupQuery()
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship> { Type = FriendshipType.Lookup };

            List<Friendship> friends = friendReqProc.ProcessResults(LookupResponse);

            Assert.NotNull(friends);
            Assert.Single(friends);
            var relations = friends.Single().Relationships;
            Assert.NotNull(relations);
            Assert.Single(relations);
            var connections = relations.Single().Connections;
            Assert.NotNull(connections);
            Assert.Single(connections);
            var connection = connections.First();
            Assert.NotNull(connection);
            Assert.Equal("following", connection);
        }
Пример #29
0
        public void BuildURLTest()
        {
            FriendshipRequestProcessor target = new FriendshipRequestProcessor()
            {
                BaseUrl = "http://twitter.com/"
            };
            Dictionary <string, string> parameters =
                new Dictionary <string, string>
            {
                { "SubjectUser", "123" },
                { "FollowingUser", "456" }
            };
            string expected = "http://twitter.com/friendships/exists.xml?user_a=123&user_b=456";
            string actual;

            actual = target.BuildURL(parameters);
            Assert.AreEqual(expected, actual);
        }
Пример #30
0
        public void BuildFollowerListUrl_Requires_ScreenName_Or_UserID()
        {
            var friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1.1/"
            };
            Dictionary <string, string> parameters =
                new Dictionary <string, string>
            {
                { "Type", FriendshipType.FriendsList.ToString() },
                //{ "UserID", "1" },
                //{ "ScreenName", "abc" }
            };

            var ex = Assert.Throws <ArgumentNullException>(() => friendReqProc.BuildUrl(parameters));

            Assert.Equal("ScreenNameOrUserID", ex.ParamName);
        }
Пример #31
0
        public void BuildUrl_Defaults_Cursor_When_Not_Specified()
        {
            const string ExpectedUrl   = "https://api.twitter.com/1.1/friends/ids.json?screen_name=JoeMayo&cursor=-1";
            var          friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1.1/"
            };
            Dictionary <string, string> parameters =
                new Dictionary <string, string>
            {
                { "Type", ((int)FriendshipType.FriendIDs).ToString() },
                { "ScreenName", "JoeMayo" },
            };

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.AreEqual(ExpectedUrl, req.FullUrl);
        }
Пример #32
0
        public void NullParametersTest()
        {
            FriendshipRequestProcessor <Friendship> target = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "http://twitter.com/"
            };
            Dictionary <string, string> parameters = null;
            string actual;

            try
            {
                actual = target.BuildURL(parameters);
                Assert.Fail("Expected ArgumentException.");
            }
            catch (ArgumentException ae)
            {
                Assert.AreEqual <string>("Type", ae.ParamName);
            }
        }
Пример #33
0
        public void BuildUrl_Constructs_FriendIDs_Url()
        {
            const string ExpectedUrl   = "https://api.twitter.com/1.1/friends/ids.json?user_id=123&screen_name=456&cursor=1&count=1";
            var          friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1.1/"
            };
            var parameters = new Dictionary <string, string>
            {
                { "Type", ((int)FriendshipType.FriendIDs).ToString() },
                { "UserID", "123" },
                { "ScreenName", "456" },
                { "Cursor", "1" },
                { "Count", "1" }
            };

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.AreEqual(ExpectedUrl, req.FullUrl);
        }
Пример #34
0
        public void BuildUrl_Constructs_Show_Url()
        {
            FriendshipRequestProcessor <Friendship> friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1/"
            };
            Dictionary <string, string> parameters =
                new Dictionary <string, string>
            {
                { "Type", FriendshipType.Show.ToString() },
                { "SourceUserID", "123" },
                { "SourceScreenName", "JoeMayo" },
                { "TargetUserID", "456" },
                { "TargetScreenName", "LinqToTweeter" }
            };
            string expected = "https://api.twitter.com/1/friendships/show.json?source_id=123&source_screen_name=JoeMayo&target_id=456&target_screen_name=LinqToTweeter";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.Equal(expected, req.FullUrl);
        }
Пример #35
0
        public void BuildShowURLTest()
        {
            FriendshipRequestProcessor <Friendship> target = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "http://twitter.com/"
            };
            Dictionary <string, string> parameters =
                new Dictionary <string, string>
            {
                { "Type", FriendshipType.Show.ToString() },
                { "SourceUserID", "123" },
                { "SourceScreenName", "JoeMayo" },
                { "TargetUserID", "456" },
                { "TargetScreenName", "LinqToTweeter" }
            };
            string expected = "http://twitter.com/friendships/show.xml?source_id=123&source_screen_name=JoeMayo&target_id=456&target_screen_name=LinqToTweeter";
            string actual;

            actual = target.BuildURL(parameters);
            Assert.AreEqual(expected, actual);
        }
Пример #36
0
        public void BuildUrl_Constructs_FollowersList_Url()
        {
            const string ExpectedUrl   = "https://api.twitter.com/1.1/followers/list.json?user_id=1&screen_name=abc&count=20&cursor=def&skip_status=true&include_user_entities=false";
            var          friendReqProc = new FriendshipRequestProcessor <Friendship>()
            {
                BaseUrl = "https://api.twitter.com/1.1/"
            };
            var parameters = new Dictionary <string, string>
            {
                { "Type", FriendshipType.FollowersList.ToString() },
                { "UserID", "1" },
                { "ScreenName", "abc" },
                { "Count", "20" },
                { "Cursor", "def" },
                { "SkipStatus", true.ToString() },
                { "IncludeUserEntities", false.ToString() }
            };

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.Equal(ExpectedUrl, req.FullUrl);
        }
        void TestParsingIds(FriendshipType friendType)
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship> { Type = friendType };

            List<Friendship> friends = friendReqProc.ProcessResults(IdsResponse);

            Assert.NotNull(friends);
            Assert.Single(friends);
            var friend = friends.Single();
            Assert.NotNull(friend);
            var idList = friend.IDInfo;
            Assert.NotNull(idList);
            var cursor = idList.CursorMovement;
            Assert.NotNull(cursor);
            Assert.Equal("2", cursor.Previous);
            Assert.Equal("1", cursor.Next);
            var ids = idList.IDs;
            Assert.NotNull(ids);
            Assert.NotEmpty(ids);
            var id = ids.First();
            Assert.Equal(5676142ul, id);
        }
Пример #38
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;
        }
Пример #39
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;
        }
Пример #40
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;
        }
        public void ProcessResults_Parses_NoRetweetIDs_Response()
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship> { Type = FriendshipType.NoRetweetIDs };

            List<Friendship> friends = friendReqProc.ProcessResults(NoRetweetIDsResponse);

            Assert.NotNull(friends);
            Assert.Single(friends);
            var friend = friends.Single();
            Assert.NotNull(friend);
            var idList = friend.IDInfo;
            Assert.NotNull(idList);
            var ids = idList.IDs;
            Assert.NotNull(ids);
            Assert.NotEmpty(ids);
            var id = ids.First();
            Assert.Equal(15411837ul, id);
        }
        public void ProcessResults_Returns_Empty_Collection_When_Empty_Results()
        {
            var reqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "http://api.twitter.com/1/" };

            List<Friendship> friendships = reqProc.ProcessResults(string.Empty);

            Assert.Empty(friendships);
        }
 public void ProcessResultsTest()
 {
     FriendshipRequestProcessor<Friendship> target = new FriendshipRequestProcessor<Friendship>();
     XElement twitterResponse = XElement.Parse(m_testQueryResponse);
     bool expected = true;
     IList actual = target.ProcessResults(twitterResponse);
     var isFriend = actual.Cast<Friendship>().First().IsFriend;
     Assert.AreEqual(expected, actual.Cast<Friendship>().First().IsFriend);
 }
        public void BuildUrl_Throws_Without_FriendsReqProcUrl()
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1/" };
            var parameters = new Dictionary<string, string>
            {
                { "Type", FriendshipType.Show.ToString() },
                { "SourceUserID", "123" },
                { "SourceScreenName", "JoeMayo" },
            };

            Assert.Throws<ArgumentException>(() => friendReqProc.BuildUrl(parameters));
        }
        public void BuildUrl_Creates_No_Retweet_Url()
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "http://api.twitter.com/" };
            Dictionary<string, string> parameters =
                new Dictionary<string, string>
                {
                    { "Type", FriendshipType.NoRetweetIDs.ToString() }
                };
            string expected = "http://api.twitter.com/friendships/no_retweet_ids.json";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.Equal(expected, req.FullUrl);
        }
        public void ProcessActionResult_Translates_Relationships_From_UpdateSettings()
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship>();

            Friendship friend = friendReqProc.ProcessActionResult(RelationshipResponse, FriendshipAction.Update);

            var srcRel = friend.SourceRelationship;
            Assert.Equal(true, srcRel.RetweetsWanted);
            Assert.Equal(true, srcRel.NotificationsEnabled);
        }
        public void BuildUrl_Constructs_FollowersList_Url()
        {
            const string ExpectedUrl = "https://api.twitter.com/1.1/followers/list.json?user_id=1&screen_name=abc&count=20&cursor=def&skip_status=true&include_user_entities=false";
            var friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1.1/" };
            var parameters = new Dictionary<string, string>
            {
                { "Type", FriendshipType.FollowersList.ToString() },
                { "UserID", "1" },
                { "ScreenName", "abc" },
                { "Count", "20" },
                { "Cursor", "def" },
                { "SkipStatus", true.ToString() },
                { "IncludeUserEntities", false.ToString() }
            };

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.Equal(ExpectedUrl, req.FullUrl);
        }
        public void GetParameters_Returns_Parameters()
        {
            FriendshipRequestProcessor<Friendship> friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1/" };
            Expression<Func<Friendship, bool>> expression =
                friend =>
                         friend.Type == FriendshipType.Show &&
                         friend.SourceUserID == "1" &&
                         friend.SourceScreenName == "Name" &&
                         friend.TargetUserID == "2" &&
                         friend.TargetScreenName == "Name" &&
                         friend.Count == 20 &&
                         friend.Cursor == "-1" &&
                         friend.ScreenName == "twitter,joemayo" &&
                         friend.UserID == "123,456" &&
                         friend.SkipStatus == true &&
                         friend.IncludeUserEntities == true;
            var lambdaExpression = expression as LambdaExpression;

            var queryParams = friendReqProc.GetParameters(lambdaExpression);

            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("Type", ((int)FriendshipType.Show).ToString())));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("SourceUserID", "1")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("SourceScreenName", "Name")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("TargetUserID", "2")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("TargetScreenName", "Name")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("Count", "20")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("Cursor", "-1")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("ScreenName", "twitter,joemayo")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("UserID", "123,456")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("SkipStatus", "True")));
            Assert.True(
                queryParams.Contains(
                    new KeyValuePair<string, string>("IncludeUserEntities", "True")));
        }
        public void BuildUrl_Constructs_Show_Url()
        {
            FriendshipRequestProcessor<Friendship> friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1/" };
            Dictionary<string, string> parameters =
                new Dictionary<string, string>
                {
                    { "Type", FriendshipType.Show.ToString() },
                    { "SourceUserID", "123" },
                    { "SourceScreenName", "JoeMayo" },
                    { "TargetUserID", "456" },
                    { "TargetScreenName", "LinqToTweeter" }
                };
            string expected = "https://api.twitter.com/1/friendships/show.json?source_id=123&source_screen_name=JoeMayo&target_id=456&target_screen_name=LinqToTweeter";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.Equal(expected, req.FullUrl);
        }
        public void BuildUrl_Throws_Without_Source_Url()
        {
            FriendshipRequestProcessor<Friendship> friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1/" };
            Dictionary<string, string> parameters =
                new Dictionary<string, string>
                {
                    { "Type", FriendshipType.Show.ToString() },
                    { "friendReqProcUserID", "456" },
                    { "friendReqProcScreenName", "LinqToTweeter" }
                };

            Assert.Throws<ArgumentException>(() => friendReqProc.BuildUrl(parameters));
        }
        public void BuildUrl_With_Cursor_Param_Constructs_Outgoing_Url()
        {
            FriendshipRequestProcessor<Friendship> friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1/" };
            Dictionary<string, string> parameters =
                new Dictionary<string, string>
                {
                    { "Type", FriendshipType.Outgoing.ToString() },
                    { "Cursor", "-1" }
                };
            string expected = "https://api.twitter.com/1/friendships/outgoing.json?cursor=-1";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.Equal(expected, req.FullUrl);
        }
        public void BuildLookupUrl_Returns_Url_For_UserID()
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1/" };
            Dictionary<string, string> parameters =
                new Dictionary<string, string>
                {
                    { "Type", FriendshipType.Lookup.ToString() },
                    { "UserID", "123,456" }
                };
            string expected = "https://api.twitter.com/1/friendships/lookup.json?user_id=123%2C456";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.Equal(expected, req.FullUrl);
        }
        public void FriendshipRequestProcessor_Works_With_Actions()
        {
            var freindReqProc = new FriendshipRequestProcessor<Friendship>();

            Assert.IsAssignableFrom<IRequestProcessorWithAction<Friendship>>(freindReqProc);
        }
        public void BuildLookupUrl_Returns_Url_For_ScreenName()
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1/" };
            Dictionary<string, string> parameters =
                new Dictionary<string, string>
                {
                    { "Type", FriendshipType.Lookup.ToString() },
                    { "ScreenName", "twitter,joemayo" }
                };
            string expected = "https://api.twitter.com/1/friendships/lookup.json?screen_name=twitter%2Cjoemayo";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.Equal(expected, req.FullUrl);
        }
        public void FriendshipRequestProcessor_Works_With_Json_Format_Data()
        {
            var freindReqProc = new FriendshipRequestProcessor<Friendship>();

            Assert.IsAssignableFrom<IRequestProcessorWantsJson>(freindReqProc);
        }
        public void BuildFollowerListUrl_Requires_ScreenName_Or_UserID()
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1.1/" };
            Dictionary<string, string> parameters =
                new Dictionary<string, string>
                {
                    { "Type", FriendshipType.FriendsList.ToString() },
                    //{ "UserID", "1" },
                    //{ "ScreenName", "abc" }
                };

            var ex = Assert.Throws<ArgumentNullException>(() => friendReqProc.BuildUrl(parameters));

            Assert.Equal("ScreenNameOrUserID", ex.ParamName);
        }
        public void ProcessResults_Retains_Original_Input_Params()
        {
            const string SrcUsrID = "1";
            const string SrcScrNm = "JoeMayo";
            const string TgtUsrID = "2";
            const string TgtScrNm = "MayMayo";
            const int Count = 20;
            const string Cursor = "123";
            const string ScrNm = "JoeMayo,MayMayo";
            const string UsrID = "1,2";
            const bool SkipStatus = true;
            const bool IncludeUserEntities = true;
            var friendReqProc = new FriendshipRequestProcessor<Friendship> 
            { 
                Type = FriendshipType.Lookup,
                SourceUserID = SrcUsrID,
                SourceScreenName = SrcScrNm,
                TargetUserID = TgtUsrID,
                TargetScreenName = TgtScrNm,
                Count = Count,
                Cursor = Cursor,
                ScreenName = ScrNm,
                UserID = UsrID,
                SkipStatus = SkipStatus,
                IncludeUserEntities = IncludeUserEntities
            };

            List<Friendship> friendships = friendReqProc.ProcessResults(LookupResponse);

            Assert.NotNull(friendships);
            Assert.NotEmpty(friendships);
            var friendship = friendships.First();
            Assert.NotNull(friendship);
            Assert.Equal(FriendshipType.Lookup, friendship.Type);
            Assert.Equal(SrcUsrID, friendship.SourceUserID);
            Assert.Equal(SrcScrNm, friendship.SourceScreenName);
            Assert.Equal(TgtUsrID, friendship.TargetUserID);
            Assert.Equal(TgtScrNm, friendship.TargetScreenName);
            Assert.Equal(Count, friendship.Count);
            Assert.Equal(Cursor, friendship.Cursor);
            Assert.Equal(ScrNm, friendship.ScreenName);
            Assert.Equal(UsrID, friendship.UserID);
            Assert.Equal(SkipStatus, friendship.SkipStatus);
            Assert.Equal(IncludeUserEntities, friendship.IncludeUserEntities);
        }
        public void ProcessResults_Parses_FriendsListOrFollowersList_Response()
        {
            var friendReqProc = new FriendshipRequestProcessor<Friendship> { Type = FriendshipType.FriendsList };

            List<Friendship> friendshipList = friendReqProc.ProcessResults(FreindsFollowersResponse);

            Assert.NotNull(friendshipList);
            Assert.Single(friendshipList);
            var friendship = friendshipList.Single();
            Assert.NotNull(friendship);
            var users = friendship.Users;
            Assert.NotNull(users);
            Assert.Equal(3, users.Count);
            var user = users.First();
            Assert.NotNull(user);
            Assert.Equal("93604443", user.Identifier.UserID);
         }
        public void BuildUrl_Constructs_Incoming_Url()
        {
            FriendshipRequestProcessor<Friendship> friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1/" };
            Dictionary<string, string> parameters =
                new Dictionary<string, string>
                {
                    { "Type", FriendshipType.Incoming.ToString() }
                };
            string expected = "https://api.twitter.com/1/friendships/incoming.json";

            Request req = friendReqProc.BuildUrl(parameters);

            Assert.Equal(expected, req.FullUrl);
        }
        public void BuildUrl_Throws_On_Null_Parameters()
        {
            FriendshipRequestProcessor<Friendship> friendReqProc = new FriendshipRequestProcessor<Friendship>() { BaseUrl = "https://api.twitter.com/1/" };
            Dictionary<string, string> parameters = null;

            var ex = Assert.Throws<ArgumentException>(() => friendReqProc.BuildUrl(parameters));

            Assert.Equal("Type", ex.ParamName);
        }