예제 #1
0
        private StringBuilder GenTimeline(Twitter twitter, StringBuilder sb)
        {
            sb.Append("Say something<br />");
            sb.Append("<form method=\"post\">");
            sb.Append("<input type=\"text\" name=\"message\"></input>");
            sb.Append("<input type=\"submit\" name=\"action\" value=\"tweet\" /> <br />");
            sb.Append("</form>");
            sb.Append("Follow someone<br />");
            sb.Append("<form method=\"post\">");
            sb.Append("<input type=\"text\" name=\"following\"></input>");
            sb.Append("<input type=\"submit\" name=\"action\" value=\"following\" /> <br />");
            sb.Append("</form>");
            sb.Append(String.Format("<h3><b>{0}</b>'s timeline</h3><br />", twitter.GetUsername()));
            List <Tweet> tweets = twitter.GetUserTimeline();

            foreach (Tweet tweet in tweets)
            {
                sb.Append("[" + tweet.DateCreated + "]" + tweet.User + ":" + tweet.Message + "<br />");
            }
            sb.Append("<br /><br />");
            sb.Append("<h3>Following timeline</h3><br />");
            tweets = twitter.GetFollowingTimeline();
            if (tweets == null)
            {
                sb.Append("Your following list is empty, follow someone!");
            }
            else
            {
                foreach (Tweet tweet in tweets)
                {
                    sb.Append("[" + tweet.DateCreated + "] " + tweet.User + ":" + tweet.Message + "<br />");
                }
            }
            return(sb);
        }
예제 #2
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response  = new HTTPResponse(200);
            string       user      = request.getRequestByKey("user");
            string       password  = request.getRequestByKey("password");
            string       following = request.getRequestByKey("following");
            string       message   = request.getRequestByKey("message");

            string[] u = request.Filename.Split("?");
            if (u[0] == "users")
            {
                if (request.Method == "GET")
                {
                    string x = JsonConvert.SerializeObject(GetUser());
                    response.body = Encoding.UTF8.GetBytes(x);
                }
                if (request.Method == "POST")
                {
                    Twitter.AddUser(user, password);
                    response.body = Encoding.UTF8.GetBytes("Susceed");
                }
                if (request.Method == "DELETE")
                {
                    Twitter.DeleteUser(user);
                    response.body = Encoding.UTF8.GetBytes("Susceed");
                }
            }
            if (u[0] == "follow")
            {
                if (request.Method == "GET")
                {
                    string x = JsonConvert.SerializeObject(Twitter.GetfollowUser(user));
                    response.body = Encoding.UTF8.GetBytes(x);
                }
                if (request.Method == "POST")
                {
                    Twitter ntwitter = new Twitter(user);
                    ntwitter.AddFollowing(following);
                    response.body = Encoding.UTF8.GetBytes("Following Susceed");
                }

                /*if (request.Method == "DELETE")
                 * {
                 *  Twitter.DeleteUser(following);
                 *  response.body = Encoding.UTF8.GetBytes("Susceed");
                 * }*/
            }
            if (u[0] == "tweet")
            {
                Twitter ntweet = new Twitter(user);
                if (request.Method == "GET")
                {
                    string timeline = request.getRequestByKey("timeline");
                    if (timeline == "follow")
                    {
                        string x = JsonConvert.SerializeObject(ntweet.GetFollowingTimeline());
                        response.body = Encoding.UTF8.GetBytes(x);
                    }
                    else
                    {
                        string x = JsonConvert.SerializeObject(ntweet.GetUserTimeline());
                        response.body = Encoding.UTF8.GetBytes(x);
                    }
                }
                if (request.Method == "POST")
                {
                    ntweet.PostTweet(message);
                    response.body = Encoding.UTF8.GetBytes("Post Susceed");
                }
            }

            return(response);
        }
예제 #3
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response = new HTTPResponse(200);

            string user          = request.getRequestByKey("user");
            string password      = request.getRequestByKey("password");
            string following     = request.getRequestByKey("following");
            string fllw_timeline = request.getRequestByKey("timeline");
            string msg           = request.getRequestByKey("msg");

            string [] url = request.Filename.Split("?");
            try
            {
                if (url[0] == "users")
                {
                    if (request.Method == "GET")
                    {
                        string js = JsonConvert.SerializeObject(GetUser());
                        response.body = Encoding.UTF8.GetBytes(js);
                    }
                    else if (request.Method == "POST")
                    {
                        if (user != null && password != null)
                        {
                            Twitter.AddUser(user, password);
                        }
                    }
                    else if (request.Method == "DELETE")
                    {
                        if (user != null)
                        {
                            Twitter.RemoveUser(user);
                        }
                    }
                }
                else if (url[0] == "following")
                {
                    if (request.Method == "GET")
                    {
                        Twitter tw = new Twitter(user);
                        string  js = JsonConvert.SerializeObject(tw.GetFollowing());
                        response.body = Encoding.UTF8.GetBytes(js);
                    }
                    else if (request.Method == "POST")
                    {
                        if (user != null && following != null)
                        {
                            Twitter tw = new Twitter(user);
                            tw.AddFollowing(following);
                        }
                    }
                    else if (request.Method == "DELETE")
                    {
                        if (user != null && following != null)
                        {
                            Twitter tw = new Twitter(user);
                            tw.RemoveFollowing(following);
                        }
                    }
                }
                else if (url[0] == "tweets")
                {
                    if (user != null)
                    {
                        if (request.Method == "GET")
                        {
                            Twitter tw = new Twitter(user);
                            string  js = JsonConvert.SerializeObject(tw.GetUserTimeline()); //userself timeline TO TEST using ?user=a
                            response.body = Encoding.UTF8.GetBytes(js);
                            if (fllw_timeline != null)                                      //if following timeline not equal null that means select following timeline TO TEST using ?user=w&timeline=a
                            {
                                string js1 = JsonConvert.SerializeObject(tw.GetFollowingTimeline());
                                response.body = Encoding.UTF8.GetBytes(js1);
                            }
                        }
                        else if (request.Method == "POST")
                        {
                            Twitter tw = new Twitter(user);
                            tw.PostTweet(msg);
                        }
                    }
                }
            }catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                Console.WriteLine(ex.ToString());
                sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message));
                response.body = Encoding.UTF8.GetBytes(sb.ToString());
                return(response);
            }
            response.type = "application/json; charset=UTF-8";
            return(response);
        }
예제 #4
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response = new HTTPResponse(200);

            string u        = request.getRequestByKey("user");
            string p        = request.getRequestByKey("password");
            string f        = request.getRequestByKey("following");
            string text     = request.getRequestByKey("text");
            string timeline = request.getRequestByKey("timeline");

            string [] path = request.Filename.Split("?");
            if (path[0] == "user")
            {
                if (request.Method == "GET")
                {
                    string temp = JsonConvert.SerializeObject(GetUser());
                    response.body = Encoding.UTF8.GetBytes(temp);
                }
                if (request.Method == "POST")
                {
                    Twitter.AddUser(u, p);
                    response.body = Encoding.UTF8.GetBytes("ADD Success");
                }
                if (request.Method == "DELETE")
                {
                    Twitter.DeleteUser(u);
                    response.body = Encoding.UTF8.GetBytes("Delete Success");
                }
            }
            if (path[0] == "follow")
            {
                Twitter twitter = new Twitter(u);
                if (request.Method == "GET")
                {
                    string temp = JsonConvert.SerializeObject(GetFollow(u));
                    response.body = Encoding.UTF8.GetBytes(temp);
                }
                if (request.Method == "POST")
                {
                    Twitter follow = new Twitter(u);
                    follow.AddFollowing(f);
                    response.body = Encoding.UTF8.GetBytes("Success Following");
                }
            }
            if (path[0] == "Tweet")
            {
                Twitter twitter = new Twitter(u);
                if (request.Method == "GET")
                {
                    if (timeline == "user")
                    {
                        string temp = JsonConvert.SerializeObject(twitter.GetUserTimeline());
                        response.body = Encoding.UTF8.GetBytes(temp);
                    }
                    if (timeline == "follow")
                    {
                        string temp = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                        response.body = Encoding.UTF8.GetBytes(temp);
                    }
                }
                if (request.Method == "POST")
                {
                    twitter.PostTweet(text);
                    response.body = Encoding.UTF8.GetBytes("Post Success");
                }
            }
            return(response);
        }
예제 #5
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response = new HTTPResponse(200);

            string user          = request.getRequestByKey("user");
            string password      = request.getRequestByKey("password");
            string following     = request.getRequestByKey("following");
            string fllw_timeline = request.getRequestByKey("timeline");
            string text          = request.getRequestByKey("text");
            string msg           = request.getRequestByKey("message");

            string[] path = request.Filename.Split("?");
            try
            {
                if (path[0] == "name")
                {
                    if (request.Method == "GET")
                    {
                        string js = JsonConvert.SerializeObject(GetUser());
                        response.body = Encoding.UTF8.GetBytes(js);
                    }
                    else if (request.Method == "POST")
                    {
                        if (user != null && password != null)
                        {
                            Twitter.AddUser(user, password);
                        }
                    }
                    else if (request.Method == "DELETE")
                    {
                        if (user != null)
                        {
                            Twitter.RemoveUser(user);
                        }
                    }
                }
                else if (path[0] == "following")
                {
                    Twitter twitter = new Twitter(user);
                    if (request.Method == "GET")
                    {
                        string temp = JsonConvert.SerializeObject(GetFollowing(user));
                        response.body = Encoding.UTF8.GetBytes(temp);
                    }
                    if (request.Method == "POST")
                    {
                        Twitter follow = new Twitter(user);
                        follow.AddFollowing(following);
                        response.body = Encoding.UTF8.GetBytes("Success Following");
                    }
                }
                else if (path[0] == "tweets")
                {
                    Twitter twitter = new Twitter(user);
                    if (request.Method == "GET")
                    {
                        if (fllw_timeline == "user")
                        {
                            string temp = JsonConvert.SerializeObject(twitter.GetUserTimeline());
                            response.body = Encoding.UTF8.GetBytes(temp);
                        }
                        if (fllw_timeline == "follow")
                        {
                            string temp = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                            response.body = Encoding.UTF8.GetBytes(temp);
                        }
                    }
                    if (request.Method == "POST")
                    {
                        twitter.PostTweet(text);
                        response.body = Encoding.UTF8.GetBytes("Post Success");
                    }
                }
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                Console.WriteLine(ex.ToString());
                sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message));
                response.body = Encoding.UTF8.GetBytes(sb.ToString());
                return(response);
            }
            response.type = "application/json; charset=UTF-8";
            return(response);
        }
예제 #6
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response = new HTTPResponse(200);

            string user      = request.getRequestByKey("user");
            string password  = request.getRequestByKey("password");
            string following = request.getRequestByKey("follow");
            string timeline  = request.getRequestByKey("timeline");
            string message   = request.getRequestByKey("message");

            string[] at = request.Filename.Split("?");
            if (at[0] == "users")
            {
                if (request.Method == "GET")
                {
                    string j = JsonConvert.SerializeObject(ListUser());
                    response.body = Encoding.UTF8.GetBytes(j);
                }
                else if (request.Method == "POST")
                {
                    if (user != null && password != null)
                    {
                        Twitter.AddUser(user, password);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    else
                    {
                        response.status = 403;
                        response.body   = Encoding.UTF8.GetBytes("403 User invalid");
                    }
                }
                else if (request.Method == "DELETE")
                {
                    try
                    {
                        Twitter.DeleteUser(user);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
            }
            else if (at[0] == "following")
            {
                if (request.Method == "GET")
                {
                    Twitter follow = new Twitter(user);
                    string  temp   = JsonConvert.SerializeObject(GetFollow(user));
                    response.body = Encoding.UTF8.GetBytes(temp);
                }
                else if (request.Method == "POST")
                {
                    if (user != null && following != null)
                    {
                        Twitter twitter = new Twitter(user);
                        twitter.AddFollowing(following);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    else
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
                else if (request.Method == "DELETE")
                {
                    try
                    {
                        Twitter twitter = new Twitter(user);
                        twitter.RemoveFollowing(following);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
            }
            if (at[0] == "tweets")
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")
                {
                    {
                        if (timeline == "users")
                        {
                            string temp = JsonConvert.SerializeObject(twitter.GetUserTimeline());
                            response.body = Encoding.UTF8.GetBytes(temp);
                        }
                        if (timeline == "follow")
                        {
                            string temp = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                            response.body = Encoding.UTF8.GetBytes(temp);
                        }
                    }
                    if (request.Method == "POST")
                    {
                        twitter.PostTweet(message);
                        response.body = Encoding.UTF8.GetBytes("202 OK");
                    }
                }
                return(response);
            }

            return(response);
        }
예제 #7
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response = new HTTPResponse(200);

            string username  = request.getRequestByKey("user");
            string password  = request.getRequestByKey("password");
            string following = request.getRequestByKey("following");
            string timeline  = request.getRequestByKey("timline");
            string message   = request.getRequestByKey("message");

            string [] url = request.Filename.split("?");

            try{
                if (url[0] == "users")
                {
                    if (request.Method == "GET")
                    {//taught by 600611001
                        string js = JsonConvert.SerializeObject(GetUser());
                        response.body = Encoding.UTF8.GetBytes(js);
                    }
                    else if (request.Method == "POST")
                    {
                        if (username != null && password != null)
                        {
                            Twitter.AddUser(username, password);
                        }
                    }
                    else if (request.Method == "DELETE")
                    {
                        if (username != null && following != null)
                        {
                            Twitter account = new Twitter(username);
                            account.RemoveFollowing(following);
                        }
                    }
                }
                else if (url[0] == "tweets")
                {
                    if (username != null)
                    {
                        if (request.Method == "GET")
                        {
                            //taught by 600611001
                            Twitter account = new Twitter(username);
                            string  js      = JsonConvert.SerializeObject(account.GetUserTimeline());
                            response.body = Encoding.UTF8.GetBytes(js);
                            if (timeline != null)
                            {
                                string js_1 = JsonConvert.SerializeObject(account.GetFollowingTimeline());
                                response.body = Encoding.UTF8.GetBytes(js_1);
                            }
                        }
                        else if (request.Method == "POST")
                        {
                            Twitter account = new Twitter(user);
                            account.PostTweet(message);
                        }
                    }
                }
            }
            catch (Exception except)
            //taught by 600611001
            {
                StringBuilder sb = new StringBuilder();
                Console.WriteLine(except.ToString());
                sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message));
                response.body = Encoding.UTF8.GetBytes(sb.ToString());
                return(response);
            }
            response.type = "application/json; charset=UTF-8";
            return(response);
        }
예제 #8
0
파일: TwitterAPI.cs 프로젝트: zNext666/DNWS
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse  response = null;
            StringBuilder sb       = new StringBuilder();

            string[] path = request.Filename.Split("?");
            if (path[0].ToLower() == "user")
            {
                if (request.Status == 200)
                {
                    if (request.Method.ToLower() == "get")//GET
                    {
                        try{
                            response      = new HTTPResponse(200);
                            response.type = "json";
                            response.body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(GetListUser())); //get list user
                        }catch (Exception) {
                            response = new HTTPResponse(400);
                        }
                    }
                    else if (request.Method.ToLower() == "post") //POST
                    {
                        try{
                            Twitter.AddUser(request.getRequestByKey("user").ToLower(), request.getRequestByKey("password").ToLower());//add user
                            sb.Append("<html><head><title>TwitterAPI</title></head><body>");
                            sb.Append("Added User : "******"user").ToLower() + " Complete!");
                            sb.Append("</body></html");
                            response      = new HTTPResponse(200);
                            response.body = Encoding.UTF8.GetBytes(sb.ToString());
                        }catch (Exception) {
                            response = new HTTPResponse(400);
                        }
                    }
                    else if (request.Method.ToLower() == "delete") //DELETE
                    {
                        try{
                            Twitter t = new Twitter(request.getRequestByKey("user"));
                            t.DeleteUser(request.getRequestByKey("user"));
                            sb.Append("<html><head><title>TwitterAPI</title></head><body>");
                            sb.Append("Delete User : "******"user").ToLower() + " Complete!");
                            sb.Append("</body></html");
                            response      = new HTTPResponse(200);
                            response.body = Encoding.UTF8.GetBytes(sb.ToString());
                        }catch (Exception) {
                            response = new HTTPResponse(400);
                        }
                    }
                    else  //OTHER ELSE
                    {
                        response = new HTTPResponse(400);
                    }
                }
                else
                {
                    response = new HTTPResponse(400);
                }
            }
            else if (path[0].ToLower() == "following")
            {
                if (request.Status == 200)
                {
                    if (request.Method.ToLower() == "get")
                    {
                        try{
                            response      = new HTTPResponse(200);
                            response.type = "json";
                            response.body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(GetListFollowing(request.getRequestByKey("user")))); //get following user
                        }catch (Exception) {
                            response = new HTTPResponse(400);
                        }
                    }
                    else if (request.Method.ToLower() == "post")
                    {
                        try
                        {
                            Twitter t = new Twitter(request.getRequestByKey("user"));
                            t.AddFollowing(request.getRequestByKey("follow"));
                            sb.Append("<html><head><title>TwitterAPI</title></head><body>");
                            sb.Append("User : "******"user").ToLower() + " Start Following : " + request.getRequestByKey("follow") + " Complete!");
                            sb.Append("</body></html");
                            response      = new HTTPResponse(200);
                            response.body = Encoding.UTF8.GetBytes(sb.ToString());
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                            response = new HTTPResponse(400);
                        }
                    }
                    else if (request.Method.ToLower() == "delete")
                    {
                        try
                        {
                            Twitter t = new Twitter(request.getRequestByKey("user"));
                            t.RemoveFollowing(request.getRequestByKey("follow"));
                            sb.Append("<html><head><title>TwitterAPI</title></head><body>");
                            sb.Append("User : "******"user").ToLower() + " Unfollowing : " + request.getRequestByKey("follow") + " Complete!");
                            sb.Append("</body></html");
                            response      = new HTTPResponse(200);
                            response.body = Encoding.UTF8.GetBytes(sb.ToString());
                        }
                        catch (Exception)
                        {
                            response = new HTTPResponse(400);
                        }
                    }
                    else
                    {
                        response = new HTTPResponse(400);
                    }
                }
                else
                {
                    response = new HTTPResponse(400);
                }
            }
            else if (path[0].ToLower() == "tweet")
            {
                if (request.Status == 200)
                {
                    if (request.Method.ToLower() == "get")
                    {
                        if (request.getRequestByKey("getlist").ToLower() == "user")
                        {
                            try
                            {
                                Twitter t = new Twitter(request.getRequestByKey("user"));
                                response      = new HTTPResponse(200);
                                response.type = "json";
                                response.body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(t.GetUserTimeline()));
                            }
                            catch (Exception)
                            {
                                response = new HTTPResponse(400);
                            }
                        }
                        else if (request.getRequestByKey("getlist").ToLower() == "following")
                        {
                            try
                            {
                                Twitter t = new Twitter(request.getRequestByKey("user"));
                                response      = new HTTPResponse(200);
                                response.type = "json";
                                response.body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(t.GetFollowingTimeline()));
                            }
                            catch (Exception ex)
                            {
                                Console.Write(ex.ToString());
                                response = new HTTPResponse(400);
                            }
                        }
                        else
                        {
                            response = new HTTPResponse(400);
                        }
                    }
                    else if (request.Method.ToLower() == "post")
                    {
                        try
                        {
                            Twitter t = new Twitter(request.getRequestByKey("user"));
                            t.PostTweet(request.getRequestByKey("tweet"));
                            sb.Append("<html><head><title>TwitterAPI</title></head><body>");
                            sb.Append("User : "******"user").ToLower() + " Tweet : " + request.getRequestByKey("tweet"));
                            sb.Append("</body></html");
                            response      = new HTTPResponse(200);
                            response.body = Encoding.UTF8.GetBytes(sb.ToString());
                        }
                        catch (Exception)
                        {
                            response = new HTTPResponse(400);
                        }
                    }
                    else
                    {
                        response = new HTTPResponse(400);
                    }
                }
                else
                {
                    response = new HTTPResponse(400);
                }
            }
            else
            {
                sb.Append("<html><head><title>TwitterAPI</title></head><body>");
                sb.Append("<h1>TwitterAPI</h1>");
                sb.Append("</body></html");
                response      = new HTTPResponse(200);
                response.body = Encoding.UTF8.GetBytes(sb.ToString());
            }
            return(response);
        }
예제 #9
0
        public new HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse  response  = new HTTPResponse(200);
            string        user      = request.getRequestByKey("user");
            string        password  = request.getRequestByKey("Password");
            string        following = request.getRequestByKey("follow");
            string        message   = request.getRequestByKey("message");
            StringBuilder sb        = new StringBuilder();

            string[] url = request.Filename.Split("?");
            if (url[0] == "user")
            {
                if (request.Method == "GET")//get list's user
                {
                    string Json = JsonConvert.SerializeObject(GetUsers());
                    response.body = Encoding.UTF8.GetBytes(Json);
                }
                else if (request.Method == "POST") //create new user
                {
                    try {
                        Twitter.AddUser(user, password);
                    }
                    catch (Exception)
                    {
                        sb.Append("<h1>Error</h1>");
                    }
                }
                else if (request.Method == "Delete") //remove user
                {
                    try
                    {
                        Twitter.DeleteUser(user, password);
                    }
                    catch (Exception)
                    {
                        sb.Append("<h1>Error</h1>");
                    }
                }
            }
            else if (url[0] == "Follow")
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")//list following
                {
                    string Json = JsonConvert.SerializeObject(GetFollowings(user));
                    response.body = Encoding.UTF8.GetBytes(Json);
                }
                else if (request.Method == "POST")//add new following
                {
                    try
                    {
                        twitter.AddFollowing(following);
                    }
                    catch (Exception)
                    {
                        sb.Append("<h1>Error</h1>");
                    }
                }
                else if (request.Method == "DELETE")//delete following
                {
                    try
                    {
                        twitter.RemoveFollowing(following);
                    }
                    catch (Exception)
                    {
                        sb.Append("<h1>Error</h1>");
                    }
                }
            }
            else if (url[0] == "tweet")
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")//get usr's and following's timeline
                {
                    try
                    {
                        string timeline = request.getRequestByKey("timeline");
                        if (timeline == "follow")
                        {
                            string Json = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                            response.body = Encoding.UTF8.GetBytes(Json);
                        }
                        else
                        {
                            string Json = JsonConvert.SerializeObject(twitter.GetUserTimeline());
                            response.body = Encoding.UTF8.GetBytes(Json);
                        }
                    }
                    catch (Exception)
                    {
                        sb.Append("<h1>Error</h1>");
                    }
                }
                else if (request.Method == "POST")//add new tweet
                {
                    try
                    {
                        twitter.PostTweet(message);
                    }
                    catch (Exception)
                    {
                        sb.Append("<h1>Error</h1>");
                    }
                }
            }


            response.type = "application/json";
            return(response);
        }
예제 #10
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response  = new HTTPResponse(200);
            string       user      = request.getRequestByKey("user");
            string       password  = request.getRequestByKey("password");
            string       following = request.getRequestByKey("following");
            string       message   = request.getRequestByKey("message");

            string[] Url_Path = request.Filename.Split("?"); // create url_path for checking
            if (Url_Path[0] == "users")                      // if user in users path
            {
                if (request.Method == "GET")                 // to show list of users
                {
                    string json = JsonConvert.SerializeObject(GetUsers());
                    response.body = Encoding.UTF8.GetBytes(json);
                }
                else if (request.Method == "POST")// add users
                {
                    try
                    {
                        Twitter.AddUser(user, password);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 403;
                        response.body   = Encoding.UTF8.GetBytes("403 User already exists");
                    }
                }
                else if (request.Method == "DELETE") // to delete user
                {
                    Twitter twitter = new Twitter(user);
                    try
                    {
                        twitter.Delete_User(user);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
            }
            else if (Url_Path[0] == "follow")// if user in follow path
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")// to show list of following
                {
                    string json = JsonConvert.SerializeObject(GetFollowing(user));
                    response.body = Encoding.UTF8.GetBytes(json);
                }
                else if (request.Method == "POST")// add following's user
                {
                    if (Twitter.Check(following))
                    {
                        twitter.AddFollowing(following);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    else
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
                else if (request.Method == "DELETE")//delete following's user there are something wrong
                {
                    try
                    {
                        twitter.RemoveFollowing(following);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
            }
            else if (Url_Path[0] == "tweets")//if user in tweets path
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")//show tweet of users in timeline
                {
                    try
                    {
                        string timeline = request.getRequestByKey("timeline");
                        if (timeline == "following")
                        {
                            string json = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                            response.body = Encoding.UTF8.GetBytes(json);
                        }
                        else
                        {
                            string json = JsonConvert.SerializeObject(twitter.GetUserTimeline());
                            response.body = Encoding.UTF8.GetBytes(json);
                        }
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not found");
                    }
                }
                else if (request.Method == "POST")//add new tweet in timeline
                {
                    try
                    {
                        twitter.PostTweet(message);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not found");
                    }
                }
            }
            response.type = "application/json";
            return(response);
        }
예제 #11
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response  = new HTTPResponse(200);
            string       user      = request.getRequestByKey("user");
            string       password  = request.getRequestByKey("password");
            string       following = request.getRequestByKey("following");
            string       message   = request.getRequestByKey("message");
            string       timeline  = request.getRequestByKey("timeline");

            string[] site = request.Filename.Split("?");

            try
            {
                if (site[0] == "users")
                {
                    if (request.Method == "GET")
                    {
                        //JSON is easy use to convert between JSON and .Net in these case the response code(200,400,...)
                        //Ref. https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
                        string js = JsonConvert.SerializeObject(GetUser());
                        response.body = Encoding.UTF8.GetBytes(js);
                    }
                    else if (request.Method == "POST")
                    {
                        if (user != null && password != null)
                        {
                            Twitter.AddUser(user, password);
                        }
                    }
                    else if (request.Method == "DELETE")
                    {
                        if (user != null)
                        {
                            Twitter.RemoveUser(user);
                        }
                    }
                }
                else if (site[0] == "following")
                {
                    if (request.Method == "GET")
                    {
                        Twitter twit = new Twitter(user);
                        string  js   = JsonConvert.SerializeObject(twit.GetFollowing());
                        response.body = Encoding.UTF8.GetBytes(js);
                    }
                    else if (request.Method == "POST")
                    {
                        if (user != null && password != null)
                        {
                            Twitter twit = new Twitter(user);
                            twit.AddFollowing(following);
                        }
                    }
                    else if (request.Method == "DELETE")
                    {
                        if (user != null && password != null)
                        {
                            Twitter twit = new Twitter(user);
                            twit.RemoveFollowing(following);
                        }
                    }
                    else if (site[0] == "tweets")
                    {
                        if (user != null)
                        {
                            if (request.Method == "GET")
                            {
                                Twitter twit = new Twitter(user);
                                string  js   = JsonConvert.SerializeObject(twit.GetUserTimeline());
                                response.body = Encoding.UTF8.GetBytes(js);
                                if (timeline != null)
                                {
                                    string json = JsonConvert.SerializeObject(twit.GetFollowingTimeline());
                                    response.body = Encoding.UTF8.GetBytes(json);
                                }
                            }
                            else if (request.Method == "POST")
                            {
                                Twitter twit = new Twitter(user);
                                twit.PostTweet(message);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                Console.WriteLine(ex.ToString());
                sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message));
                response.body = Encoding.UTF8.GetBytes(sb.ToString());
                return(response);
            }
            return(response);
        }
예제 #12
0
        public new HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse  response = new HTTPResponse(200);
            StringBuilder sb       = new StringBuilder();

            session = request.GetPropertyByKey("X-session");
            string[] urlToken = request.Url.Split("/");
            if (urlToken.Length > 2)
            {
                action = urlToken[2];
            }
            else
            {
                action = null;
            }
            requestMethod = request.Method;
            string username = request.GetRequestByKey("username");
            string password = request.GetRequestByKey("password");

            if (IsMethod(HTTP_OPTIONS))
            {
                return(response);
            }

            if (IsAction(USER_ACTION))
            {
                if (IsMethod(HTTP_POST))
                {
                    if (!IsNOE(username) && !IsNOE(password))
                    {
                        try
                        {
                            Twitter.AddUser(username, password);
                            response.Status = 201;
                        }
                        catch (Exception ex)
                        {
                            response.SetBody(ex.Message);
                            response.Status = 500;
                        }
                    }
                    else
                    {
                        response.SetBody("Username and password required");
                        response.Status = 400;
                        return(response);
                    }
                }
            }
            else if (IsAction(LOGIN_ACTION))
            {
                if (IsMethod(HTTP_POST))
                {
                    if (!IsNOE(username) && !IsNOE(password))
                    {
                        if (Twitter.IsValidUser(username, password))
                        {
                            string newSession = Twitter.GenSession(username);
                            if (!IsNOE(newSession))
                            {
                                response.Status = 201;
                                dynamic jobj = new JObject();
                                jobj.Session  = newSession;
                                response.Type = "application/json";
                                string resp = JsonConvert.SerializeObject(jobj);
                                response.Body = Encoding.UTF8.GetBytes(resp);
                                return(response);
                            }
                            response.SetBody("Can't create session");
                            response.Status = 500;
                            return(response);
                        }
                        else
                        {
                            response.SetBody("Invalid username/password");
                            response.Status = 404;
                            return(response);
                        }
                    }
                    else
                    {
                        response.SetBody("Username and password required");
                        response.Status = 400;
                        return(response);
                    }
                }
            }
            else if (!IsNOE(session))
            {
                User user = Twitter.GetUserFromSession(session);
                if (user == null)
                {
                    response.SetBody("User not found, please login again");
                    response.Status = 404;
                    return(response);
                }
                Twitter twitter = new Twitter(user.Name);
                if (IsNOE(action))
                {
                    if (IsMethod(HTTP_GET))
                    {
                        try
                        {
                            response      = new HTTPResponse(200);
                            response.Type = "application/json";
                            string resp = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                            if (IsNOE(resp))
                            {
                                response.SetBody("No post in timline");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.SetBody(ex.Message);
                            response.Status = 500;
                            return(response);
                        }
                    }
                }
                else if (IsAction(LOGOUT_ACTION))
                {
                    if (IsMethod(HTTP_POST))
                    {
                        try {
                            Twitter.RemoveSession(user.Name);
                            response.Status = 200;
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.SetBody(ex.Message);
                            response.Status = 500;
                            return(response);
                        }
                    }
                }
                else if (IsAction(TWEET_ACTION))
                {
                    if (IsMethod(HTTP_GET))
                    {
                        try
                        {
                            response      = new HTTPResponse(200);
                            response.Type = "application/json";
                            string resp = null;
                            if (IsNOE(urlToken[3]))
                            {
                                resp = JsonConvert.SerializeObject(twitter.GetTimeline(user));
                            }
                            else
                            {
                                User aUser = Twitter.GetUserFromName(urlToken[3]);
                                if (aUser == null)
                                {
                                    response.SetBody("User not found");
                                    response.Status = 404;
                                    return(response);
                                }
                                resp = JsonConvert.SerializeObject(twitter.GetTimeline(aUser));
                            }
                            if (IsNOE(resp))
                            {
                                response.SetBody("No post in timline");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.SetBody(ex.Message);
                            response.Status = 500;
                            return(response);
                        }
                    }
                    else if (IsMethod(HTTP_POST))
                    {
                        try
                        {
                            string message = request.GetRequestByKey("message");
                            if (IsNOE(message))
                            {
                                response.SetBody("Message required");
                                response.Status = 400;
                                return(response);
                            }
                            Tweet tweet = new Tweet();
                            tweet.User        = user.Name;
                            tweet.Message     = message;
                            tweet.DateCreated = DateTime.Now;
                            using (var context = new TweetContext())
                            {
                                context.Tweets.Add(tweet);
                                context.SaveChanges();
                            }
                            response.Status = 201;
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.SetBody(ex.Message);
                            response.Status = 500;
                            return(response);
                        }
                    }
                    else if (IsMethod(HTTP_DELETE))
                    {
                        try {
                            throw (new NotImplementedException());
                        } catch (Exception ex) {
                            response.SetBody(ex.Message);
                            response.Status = 501;
                            return(response);
                        }
                    }
                }
                //action following
                else if (IsAction(FOLLOWING_ACTION))
                {
                    if (IsMethod(HTTP_GET))
                    {
                        response      = new HTTPResponse(200);
                        response.Type = "application/json";
                        string resp = JsonConvert.SerializeObject(user.Following);
                        if (resp == null)
                        {
                            response.SetBody("No following");
                            response.Status = 404;
                            return(response);
                        }
                        response.Body = Encoding.UTF8.GetBytes(resp);
                        return(response);
                    }
                    //add new following
                    else if (IsMethod(HTTP_POST))
                    {
                        string following = request.GetRequestByKey("followingname");
                        try
                        {
                            twitter.AddFollowing(following);
                            response      = new HTTPResponse(201);
                            response.Type = "application/json";
                            string resp = JsonConvert.SerializeObject(user.Following);
                            if (resp == null)
                            {
                                response.SetBody("No folowing");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.Status = 400;
                            response.SetBody(ex.Message);
                            return(response);
                        }
                    }
                    else if (IsMethod(HTTP_DELETE))
                    {
                        string following = request.GetRequestByKey("followingname");
                        try
                        {
                            twitter.RemoveFollowing(following);
                            response      = new HTTPResponse(200);
                            response.Type = "application/json";
                            string resp = JsonConvert.SerializeObject(user.Following);
                            if (resp == null)
                            {
                                response.SetBody("No following");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.Status = 400;
                            response.SetBody(ex.Message);
                            return(response);
                        }
                    }
                }
                //add follow
                else if (IsAction(FOLLOW_ACTION))
                {
                    if (IsMethod(HTTP_POST))
                    {
                        string following = request.GetRequestByKey("followingname");
                        try
                        {
                            twitter.AddFollowing(following);
                            response      = new HTTPResponse(201);
                            response.Type = "application/json";
                            string resp = JsonConvert.SerializeObject(user.Following);
                            if (resp == null)
                            {
                                response.SetBody("No folowing");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }
                        catch (Exception ex)
                        {
                            response.Status = 400;
                            response.SetBody(ex.Message);
                            return(response);
                        }
                    }
                }
                //unfollow
                else if (IsAction(UNFOLLOW_ACTION))
                {
                    if (IsMethod(HTTP_DELETE))
                    {
                        try
                        {
                            twitter.RemoveFollowing(following);
                            response      = new HTTPResponse(200);
                            response.Type = "application/json";
                            string resp = JsonConvert.SerializeObject(user.Following);
                            if (resp == null)
                            {
                                response.SetBody("No following");
                                response.Status = 404;
                                return(response);
                            }
                            response.Body = Encoding.UTF8.GetBytes(resp);
                            return(response);
                        }

                        catch (Exception ex)
                        {
                            response.Status = 400;
                            response.SetBody(ex.Message);
                            return(response);
                        }
                    }
                }
            }
            response.Status = 400;
            return(response);
        }
예제 #13
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response  = new HTTPResponse(200);
            string       user      = request.getRequestByKey("user");
            string       password  = request.getRequestByKey("password");
            string       following = request.getRequestByKey("follow");
            string       message   = request.getRequestByKey("message");

            string[] link = request.Filename.Split("?");
            if (link[0] == "user")
            {
                if (request.Method == "GET")
                {
                    string json = JsonConvert.SerializeObject(GetUsers());
                    response.body = Encoding.UTF8.GetBytes(json);
                }
                else if (request.Method == "POST")
                {//create new user require new username and newpassword
                    if (user != null && password != null)
                    {
                        Twitter.AddUser(user, password);
                        response.body = Encoding.UTF8.GetBytes("user added");
                    }
                }
                else if (request.Method == "DELETE")
                {   //delete user require username and password
                    Twitter twitter = new Twitter(user);
                    if (user != null && password != null)
                    {
                        twitter.DeleteUser(user);
                        response.body = Encoding.UTF8.GetBytes("deleted");
                    }
                }
            }
            else if (link[0] == "follow")
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")
                {
                    string json = JsonConvert.SerializeObject(GetFollowing(user));
                    response.body = Encoding.UTF8.GetBytes(json);
                }
                else if (request.Method == "POST")
                {
                    if (Twitter.CheckUser(following))
                    {
                        twitter.AddFollowing(following);
                    }
                    else
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
                else if (request.Method == "DELETE")
                {
                    try
                    {
                        twitter.RemoveFollowing(following);
                        response.body = Encoding.UTF8.GetBytes("deleted");
                    }
                    catch
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
            }
            else if (link[0] == "tweet")
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")
                {
                    try
                    {
                        string timeline = request.getRequestByKey("timeline");
                        if (timeline == "follow")
                        {
                            string json = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                            response.body = Encoding.UTF8.GetBytes(json);
                        }
                        else
                        {
                            string json = JsonConvert.SerializeObject(twitter.GetUserTimeline());
                            response.body = Encoding.UTF8.GetBytes(json);
                        }
                    }
                    catch
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not found");
                    }
                }
                else if (request.Method == "POST")
                {
                    try
                    {
                        twitter.PostTweet(message);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not found");
                    }
                }
            }
            response.type = "application/json";
            return(response);
        }
예제 #14
0
        public override HTTPResponse GetResponse(HTTPRequest request) //OVERRIDE GetResponse function
        {
            HTTPResponse response = new HTTPResponse(200);
            //get request and store value
            string username  = request.getRequestByKey("user");
            string password  = request.getRequestByKey("password");
            string following = request.getRequestByKey("follow");
            string message   = request.getRequestByKey("message");

            string[] Choose = request.Filename.Split("?");
            //Determine between users and following
            if (Choose[0] == "users")
            {
                //cases for each method
                if (request.Method == "GET")
                {
                    string json = JsonConvert.SerializeObject(Get_All_User());
                    response.body = Encoding.UTF8.GetBytes(json);
                }
                if (request.Method == "POST")
                {
                    try
                    {
                        Twitter.AddUser(username, password);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 403;
                        response.body   = Encoding.UTF8.GetBytes("403 User already exists");
                    }
                }
                if (request.Method == "DELETE")
                {
                    Twitter twitter = new Twitter(username);
                    try
                    {
                        twitter.Del_User(username); //use Del_User to delete user
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
            }
            //Determine between users and following
            if (Choose[0] == "following")
            {
                Twitter twitter = new Twitter(username);
                //cases for each method
                if (request.Method == "GET")
                {
                    string json = JsonConvert.SerializeObject(GetFollowing(username));
                    response.body = Encoding.UTF8.GetBytes(json);
                }
                if (request.Method == "POST")
                {
                    if (Twitter.Check_Username(following))
                    {
                        twitter.AddFollowing(following);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    else
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
                if (request.Method == "DELETE")
                {
                    try
                    {
                        twitter.RemoveFollowing(following);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
            }
            if (Choose[0] == "tweets")
            {
                Twitter twitter = new Twitter(username);
                if (request.Method == "GET")
                {
                    try
                    {
                        string timeline = request.getRequestByKey("timeline");
                        if (timeline == "following")
                        {
                            string json = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                            response.body = Encoding.UTF8.GetBytes(json);
                        }
                        else
                        {
                            string json = JsonConvert.SerializeObject(twitter.GetUserTimeline());
                            response.body = Encoding.UTF8.GetBytes(json);
                        }
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not found");
                    }
                }
                if (request.Method == "POST")
                {
                    try
                    {
                        twitter.PostTweet(message);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not found");
                    }
                }
            }
            response.type = "application/json";
            return(response);
        }
예제 #15
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response  = new HTTPResponse(200);
            string       user      = request.getRequestByKey("user");
            string       password  = request.getRequestByKey("password");
            string       following = request.getRequestByKey("follow");
            string       message   = request.getRequestByKey("message");

            string[] path = request.Filename.Split("?");
            if (path[0] == "users")
            {
                if (request.Method == "GET")
                {
                    string json = JsonConvert.SerializeObject(GetAllUsers());
                    response.body = Encoding.UTF8.GetBytes(json);
                }
                else if (request.Method == "POST")
                {
                    try
                    {
                        Twitter.AddUser(user, password);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 403;
                        response.body   = Encoding.UTF8.GetBytes("403 User already exists");
                    }
                }
                else if (request.Method == "DELETE")
                {
                    try
                    {
                        Twitter twitter = new Twitter(user);
                        twitter.DeleteUser(user);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
            }
            else if (path[0] == "following")
            {
                if (request.Method == "GET")
                {
                    string json = JsonConvert.SerializeObject(GetFollowing(user));
                    response.body = Encoding.UTF8.GetBytes(json);
                }
                else if (request.Method == "POST")
                {
                    if (Twitter.CheckUser(following))
                    {
                        Twitter twitter = new Twitter(user);
                        twitter.AddFollowing(following);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    else
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
                else if (request.Method == "DELETE")
                {
                    try
                    {
                        Twitter twitter = new Twitter(user);
                        twitter.RemoveFollowing(following);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
            }
            else if (path[0] == "tweets")
            {
                if (request.Method == "GET")
                {
                    try
                    {
                        string timeline = request.getRequestByKey("timeline");
                        if (timeline == "following")
                        {
                            Twitter twitter = new Twitter(user);
                            string  json    = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                            response.body = Encoding.UTF8.GetBytes(json);
                        }
                        else
                        {
                            Twitter twitter = new Twitter(user);
                            string  json    = JsonConvert.SerializeObject(twitter.GetUserTimeline());
                            response.body = Encoding.UTF8.GetBytes(json);
                        }
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not found");
                    }
                }
                else if (request.Method == "POST")
                {
                    try
                    {
                        Twitter twitter = new Twitter(user);
                        twitter.PostTweet(message);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not found");
                    }
                }
            }
            response.type = "application/json";
            return(response);
        }
예제 #16
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response = new HTTPResponse(200);

            string user           = request.getRequestByKey("user");
            string password       = request.getRequestByKey("password");
            string Follow         = request.getRequestByKey("following");
            string followtimeline = request.getRequestByKey("timeline");
            string message        = request.getRequestByKey("message");

            string[] path = request.Filename.Split("?"); //teach dy 600611030

            if (path[0] == "User")
            {
                if (request.Method == "GET")
                {
                    string y = JsonConvert.SerializeObject(GetUser());
                    response.body = Encoding.UTF8.GetBytes(y);
                }
                if (request.Method == "POST")
                {
                    Twitter.AddUser(user, password);
                    response.body = Encoding.UTF8.GetBytes("Add user success");
                }
                if (request.Method == "DELETE")
                {
                    Twitter.DeleteUser(user, password);
                    response.body = Encoding.UTF8.GetBytes("Delete user success");
                }
            }
            else if (path[0] == "following")
            {
                //Twitter twitter = new Twitter(user);
                if (request.Method == "GET")
                {
                    Twitter twitters = new Twitter(user);
                    string  js       = JsonConvert.SerializeObject(twitters.GetFollowing());
                    response.body = Encoding.UTF8.GetBytes(js);
                }
                else if (request.Method == "POST")
                {
                    if (Twitter.Check_User(Follow))
                    {
                        Twitter twitter = new Twitter(user);
                        twitter.AddFollowing(Follow);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    else
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
                else if (request.Method == "DELETE")
                {
                    try
                    {
                        Twitter twitter = new Twitter(user);
                        twitter.RemoveFollowing(Follow);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                    }
                }
            }

            else if (request.Method == "DELETE")
            {
                try
                {
                    Twitter twitter = new Twitter(user);
                    twitter.RemoveFollowing(Follow);
                    response.body = Encoding.UTF8.GetBytes("200 OK");
                }
                catch (Exception)
                {
                    response.status = 404;
                    response.body   = Encoding.UTF8.GetBytes("404 User not exists");
                }
            }

            else if (path[0] == "Tweett")
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")
                {
                    try
                    {
                        string timeline = request.getRequestByKey("timeline");
                        if (timeline == "following")
                        {
                            string json = JsonConvert.SerializeObject(twitter.GetFollowingTimeline());
                            response.body = Encoding.UTF8.GetBytes(json);
                        }
                        else
                        {
                            string json = JsonConvert.SerializeObject(twitter.GetUserTimeline());
                            response.body = Encoding.UTF8.GetBytes(json);
                        }
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not found");
                    }
                }
                else if (request.Method == "POST")
                {
                    try
                    {
                        twitter.PostTweet(message);
                        response.body = Encoding.UTF8.GetBytes("200 OK");
                    }
                    catch (Exception)
                    {
                        response.status = 404;
                        response.body   = Encoding.UTF8.GetBytes("404 User not found");
                    }
                }
            }

            return(response);
        }
예제 #17
0
        public override HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse response  = new HTTPResponse(200);
            string       user      = request.getRequestByKey("user");
            string       password  = request.getRequestByKey("password");
            string       following = request.getRequestByKey("follow");
            string       message   = request.getRequestByKey("message");
            string       time      = request.getRequestByKey("timeline");

            string[] name = request.Filename.Split("?");        //split part

            if (name[0] == "API")
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")
                {
                    string json = JsonConvert.SerializeObject(ListUsers()); //show list user
                    response.body = Encoding.UTF8.GetBytes(json);
                }
                else if (request.Method == "POST")
                {
                    try
                    {
                        Twitter.AddUser(user, password); //add usersby use name and password.
                        response.body = Encoding.UTF8.GetBytes("SUCCESS");
                    }
                    catch (Exception)
                    {
                        response.status = 403;
                        response.body   = Encoding.UTF8.GetBytes("ERROR");
                    }
                }
                else if (request.Method == "DELETE")
                {
                    try
                    {
                        twitter.RemoveUser(user); //remove user by use name  but sill bug if user have following it can not delete.
                        response.body = Encoding.UTF8.GetBytes("OK^_^");
                    }
                    catch (Exception)
                    {
                        response.body = Encoding.UTF8.GetBytes("DELETE ERROR");
                    }
                }
            }
            else if (name[0] == "FOLLOW")
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")
                {
                    string json = JsonConvert.SerializeObject(ListFollow(user)); //list all following.
                    response.body = Encoding.UTF8.GetBytes(json);
                }
                else if (request.Method == "POST")
                {
                    try
                    {
                        if (twitter.chack(following))   //chack it have follow in list if not add it.
                        {
                            Twitter Friend = new Twitter(user);
                            Friend.AddFollowing(following); //AddFollowing
                            response.body = Encoding.UTF8.GetBytes("SUCCESS");
                        }
                    }
                    catch (Exception)
                    {
                        response.status = 403;
                        response.body   = Encoding.UTF8.GetBytes("ERROR");
                    }
                }
                else if (request.Method == "DELETE")
                {
                    try
                    {
                        twitter.RemoveFollowing(following);  //RemoveFollowing.
                        response.body = Encoding.UTF8.GetBytes("OK^_^");
                    }
                    catch (Exception)
                    {
                        response.body = Encoding.UTF8.GetBytes("DELETE ERROR");
                    }
                }
            }
            else if (name[0] == "TWEET")
            {
                Twitter twitter = new Twitter(user);
                if (request.Method == "GET")
                {
                    if (time == "FOLLOW")
                    {
                        string json = JsonConvert.SerializeObject(twitter.GetFollowingTimeline()); //get timeline Following if FOLLOW
                        response.body = Encoding.UTF8.GetBytes(json);
                    }
                    else
                    {
                        string json = JsonConvert.SerializeObject(twitter.GetFollowingTimeline()); //get timeline Following
                        response.body = Encoding.UTF8.GetBytes(json);
                    }
                }
                else if (request.Method == "POST")
                {
                    try
                    {
                        twitter.PostTweet(message);    //POst message
                        response.body = Encoding.UTF8.GetBytes("SUCCESS");
                    }
                    catch (Exception)
                    {
                        response.status = 403;
                        response.body   = Encoding.UTF8.GetBytes("ERROR");
                    }
                }
                else if (request.Method == "DELETE")
                {
                    try
                    {
                        response.body = Encoding.UTF8.GetBytes("OK^_^");
                    }
                    catch (Exception)
                    {
                        response.body = Encoding.UTF8.GetBytes("DELETE ERROR");
                    }
                }
            }
            response.type = "application/json";
            return(response);
        }