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); }
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); }
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); }
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); }
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); }
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); }
public HTTPResponse GetResponse(HTTPRequest request) { HTTPResponse response = new HTTPResponse(200); StringBuilder sb = new StringBuilder(); string session = request.GetRequestByKey("session"); string action = request.GetRequestByKey("action"); if (action != null) { action = action.ToLower(); } string username = request.GetRequestByKey("username"); string password = request.GetRequestByKey("password"); string following = request.GetRequestByKey("following"); string message = request.GetRequestByKey("message"); if (session == null) // no session? show login screen { if (action == null) { sb.Append("<h1>Twitter</h1>"); sb = GenLoginPage(sb); } else { if (action.Equals("newuser")) { if (username != null && password != null && username != "" && password != "") { try { Twitter.AddUser(username, password); sb.Append("User added successfully, please go back to <a href=\"/twitter\">login page</a> to login"); } catch (Exception ex) { sb.Append(String.Format("Error adding user with error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message)); } } } else if (action.Equals("login")) { if (username != null && password != null && username != "" && password != "") { if (Twitter.IsValidUser(username, password)) { string newSession = Twitter.GenSession(username); if (newSession != null) { response.Status = 301; response.AddCustomHeader("Location", "/twitter?session=" + newSession); return(response); } response.Status = 500; return(response); } else { sb.Append("Error login, please go back to <a href=\"/twitter\">login page</a> to try again"); } } } } } else // session is not null { User user = Twitter.GetUserFromSession(session); if (user == null) { response.Status = 404; return(response); } Twitter twitter = new Twitter(user.Name); if (action == null) // No action? go to homepage { try { sb.Append(String.Format("<h1>{0}'s Twitter</h1>", user.Name)); sb = GenTimeline(twitter, sb); } catch (Exception ex) { sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message)); } } else //action is not null { sb.Append(String.Format("<h1>{0}'s Twitter</h1>", user.Name)); if (action.Equals("following")) { try { twitter.AddFollowing(following); sb = GenTimeline(twitter, sb); } catch (Exception ex) { sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message)); } } else if (action.Equals("tweet")) { try { twitter.PostTweet(message); sb = GenTimeline(twitter, sb); } catch (Exception ex) { 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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
public virtual HTTPResponse GetResponse(HTTPRequest request) { HTTPResponse response = new HTTPResponse(200); StringBuilder sb = new StringBuilder(); string user = request.getRequestByKey("user"); string password = request.getRequestByKey("password"); string action = request.getRequestByKey("action"); string following = request.getRequestByKey("following"); string message = request.getRequestByKey("message"); if (user == null) // no user? show login screen { sb.Append("<h1>Twitter</h1>"); sb = GenLoginPage(sb); } else { if (action == null) // No action? go to homepage { try { Twitter twitter = new Twitter(user); sb.Append(String.Format("<h1>{0}'s Twitter</h1>", user)); sb = GenTimeline(twitter, sb); } catch (Exception ex) { sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message)); } } else { if (action.Equals("newuser")) { if (user != null && password != null && user != "" && password != "") { try { Twitter.AddUser(user, password); sb.Append("User added successfully, please go back to <a href=\"/twitter\">login page</a> to login"); } catch (Exception ex) { sb.Append(String.Format("Error adding user with error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message)); } } } else if (action.Equals("login")) { if (user != null && password != null && user != "" && password != "") { if (Twitter.IsValidUser(user, password)) { sb.Append(String.Format("Welcome {0}, please go back to <a href=\"/twitter?user={0}\">tweet page</a> to begin", user)); } else { sb.Append("Error login, please go back to <a href=\"/twitter\">login page</a> to try again"); } } } else { Twitter twitter = new Twitter(user); sb.Append(String.Format("<h1>{0}'s Twitter</h1>", user)); if (action.Equals("following")) { try { twitter.AddFollowing(following); sb = GenTimeline(twitter, sb); } catch (Exception ex) { sb.Append(String.Format("Error [{0}], please go back to <a href=\"/twitter\">login page</a> to try again", ex.Message)); } } else if (action.Equals("tweet")) { try { twitter.PostTweet(message); sb = GenTimeline(twitter, sb); } catch (Exception ex) { 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); }
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); }