Ejemplo n.º 1
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);
        }
Ejemplo n.º 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 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);
        }
Ejemplo n.º 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       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);
        }