Ejemplo n.º 1
0
 public static void Handler(HttpListenerContext Context, ref ResponseObject ResponseObject)
 {
     string[] SegmentedURL = Context.Request.RawUrl.Split("/".ToCharArray()); // Split the url at all / so /All/Users becomes {"","All","Users"} so we can reference each position in the url with greater ease
     if (SegmentedURL[1] == "user")
     {
         if (SegmentedURL.Length != 3)
         {
             ResponseObject.Message = "Missing Parameter"; ResponseObject.Status = 401; return;
         }                                                                               // Check how many positions are present in the array, if it isnt 3, then the UserID is missing
         try { int.Parse(SegmentedURL[2]); } catch { ResponseObject.Message = "Invalid Parameter"; ResponseObject.Status = 500; return; } // Check if where UserID should be, a number is present, otherwise, we indicate an error and return
         Data.Objects.User User = Data.Objects.User.FromId(uint.Parse(SegmentedURL[2])); // Fetch the User object indicated by the UserID present in the URL
         if (User == null)
         {
             ResponseObject.Message = "User doesnt exist"; ResponseObject.Status = 405; return;
         }                                       // If User is null we indicate the error and return
         ResponseObject.Data    = User.ToJson(); // Set the ResponseObject's data to the JSON version of the User Object
         ResponseObject.Message = "Got User";    // Set a message and status to indicate success
         ResponseObject.Status  = 200;
     }
     else if (SegmentedURL[1] == "all" && SegmentedURL[2] == "users")
     {
         Data.Objects.User[] User = Data.Objects.User.AllUsers();               // Get an array of all User Objects
         ResponseObject.Data    = Newtonsoft.Json.Linq.JToken.FromObject(User); // Set the ResponseObject's data to the JSON version of the Array of User Objects
         ResponseObject.Message = "Got All Users";                              // Set a message and status to indicate success
         ResponseObject.Status  = 200;
     }
     else if (SegmentedURL[1] == "account")
     {
         if (SegmentedURL.Length != 3)
         {
             ResponseObject.Message = "Missing Parameter"; ResponseObject.Status = 401; return;
         }                                                                                        // Check how many positions are present in the array, if it isnt 3, then the AccountID is missing
         try { int.Parse(SegmentedURL[2]); } catch { ResponseObject.Message = "Invalid Parameter"; ResponseObject.Status = 500; return; }// Check if where AccountID should be, a number is present, otherwise, we indicate an error and return
         Data.Objects.Account Account = Data.Objects.Account.FromId(uint.Parse(SegmentedURL[2])); // Fetch the Account Object indicated by the AccountID present in the URL
         if (Account == null)
         {
             ResponseObject.Message = "Account doesnt exist"; ResponseObject.Status = 405; return;
         }                                          // If Account is null we indicate the error and return
         ResponseObject.Data    = Account.ToJson(); // Set the ResponseObject's data to the JSON version of the Account Object
         ResponseObject.Message = "Got Account";    // Set a message and status to indicate success
         ResponseObject.Status  = 200;
     }
     else if (SegmentedURL[1] == "all" && SegmentedURL[2] == "accounts")
     {
         Data.Objects.Account[] Accounts = Data.Objects.Account.AllAccounts();      // Get an array of all Account Objects
         ResponseObject.Data    = Newtonsoft.Json.Linq.JToken.FromObject(Accounts); // Set the ResponseObject's data to the JSON version of the Array of Account Objects
         ResponseObject.Message = "Got All Accounts";                               // Set a message and status to indicate success
         ResponseObject.Status  = 200;
     }
     else
     {
         ResponseObject.Message = "Path Not Found"; ResponseObject.Status = 404;
     }                                                                                // Indicate that the URL did not match any API paths
 }
Ejemplo n.º 2
0
 public static void Handler(HttpListenerContext Context, ref ResponseObject ResponseObject)
 {
     string[] SegmentedURL = Context.Request.RawUrl.Split("/".ToCharArray()); // Split the url at all / so /All/Users becomes {"","All","Users"} so we can reference each position in the url with greater ease
     if (SegmentedURL[1] == "create" && SegmentedURL[2] == "user")
     {
         if (!Misc.TokenValid(Context, ref ResponseObject))
         {
             return;
         }
         Data.Objects.NewUser NewUser = new Data.Objects.NewUser();
         if (Context.Request.Headers["DiscordId"] != null)
         {
             NewUser.DiscordId = Context.Request.Headers["DiscordId"];
         }
         if (Context.Request.Headers["TwitchId"] != null)
         {
             NewUser.TwitchId = Context.Request.Headers["TwitchId"];
         }
         if (!Data.Checks.IsValidID(NewUser.DiscordId) || Data.Checks.IsValidID(NewUser.TwitchId))
         {
             ResponseObject.Message = "Invalid ID"; ResponseObject.Status = 506;
         }
         if (Data.Objects.User.UserExists(NewUser.TwitchId, NewUser.DiscordId))
         {
             ResponseObject.Message = "A User Already Exists"; ResponseObject.Status = 505; return;
         }
         Data.Objects.NewUser.Save(NewUser);
         ResponseObject.Message = "Created User"; ResponseObject.Status = 200;
     }
     else if (SegmentedURL[1] == "user")
     {
         Data.Objects.NewUser NewUser = new Data.Objects.NewUser();
         if (Context.Request.Headers["DiscordId"] != null)
         {
             NewUser.DiscordId = Context.Request.Headers["DiscordId"];
         }
         if (Context.Request.Headers["TwitchId"] != null)
         {
             NewUser.TwitchId = Context.Request.Headers["TwitchId"];
         }
         Data.Objects.User User = Data.Objects.User.FromNewUser(NewUser);
         if (User == null)
         {
             ResponseObject.Message = "User doesnt exist"; ResponseObject.Status = 405; return;
         }
         ResponseObject.Data    = User.ToJson();
         ResponseObject.Message = "Got User";
         ResponseObject.Status  = 200;
     }
     else if (SegmentedURL[1] == "update" && SegmentedURL[2] == "user")
     {
         if (!Misc.TokenValid(Context, ref ResponseObject))
         {
             return;
         }
         string StreamString = new System.IO.StreamReader(Context.Request.InputStream).ReadToEnd();
         Newtonsoft.Json.Linq.JToken User = Newtonsoft.Json.Linq.JToken.Parse(StreamString);
         Data.Objects.User.Update(User.ToObject <Data.Objects.User>());
         ResponseObject.Message = "Updated User";
         ResponseObject.Status  = 200;
     }
     else if (SegmentedURL[1] == "delete" && SegmentedURL[2] == "user")
     {
         if (SegmentedURL.Length != 4)
         {
             ResponseObject.Message = "Missing Parameter"; ResponseObject.Status = 401; return;
         }
         try { uint.Parse(SegmentedURL[3]); } catch { ResponseObject.Message = "Invalid Parameter"; ResponseObject.Status = 500; return; }
         Data.Objects.User User = Data.Objects.User.FromId(uint.Parse(SegmentedURL[3]));
         if (User == null)
         {
             ResponseObject.Message = "User doesnt exist"; ResponseObject.Status = 405; return;
         }
         Data.Objects.User.Delete(User);
         ResponseObject.Data    = User.ToJson();
         ResponseObject.Message = "Deleted User";
         ResponseObject.Status  = 200;
     }
     else if (SegmentedURL[1] == "account" && SegmentedURL[2] == "give")
     {
         if (!Misc.TokenValid(Context, ref ResponseObject))
         {
             return;
         }
         try { uint.Parse(SegmentedURL[3]); uint.Parse(Context.Request.Headers["Value"]); } catch { ResponseObject.Message = "Invalid Parameter"; ResponseObject.Status = 500; return; }
         Data.Objects.User User = Data.Objects.User.FromId(uint.Parse(SegmentedURL[3]));
         if (User == null)
         {
             ResponseObject.Message = "User doesnt exist"; ResponseObject.Status = 405; return;
         }
         User.Account.Balance += uint.Parse(Context.Request.Headers["Value"]);
         Data.Objects.Account.Update(User.Account);
         ResponseObject.Message = "Adjusted Users Balance"; ResponseObject.Status = 200;
     }
     else if (SegmentedURL[1] == "account" && SegmentedURL[2] == "take")
     {
         if (!Misc.TokenValid(Context, ref ResponseObject))
         {
             return;
         }
         try { uint.Parse(SegmentedURL[3]); int.Parse(Context.Request.Headers["Value"]); } catch { ResponseObject.Message = "Invalid Parameter"; ResponseObject.Status = 500; return; }
         Data.Objects.User User = Data.Objects.User.FromId(uint.Parse(SegmentedURL[3]));
         if (User == null)
         {
             ResponseObject.Message = "User doesnt exist"; ResponseObject.Status = 405; return;
         }
         uint ChangeBy = uint.Parse(Context.Request.Headers["Value"]);
         if (User.Account.Balance < ChangeBy)
         {
             ResponseObject.Message = "Insufficient Balance"; ResponseObject.Status = 205; return;
         }
         User.Account.Balance -= ChangeBy;
         Data.Objects.Account.Update(User.Account);
         ResponseObject.Message = "Adjusted Users Balance"; ResponseObject.Status = 200;
     }
     else if (SegmentedURL[1] == "account" && SegmentedURL[2] == "set")
     {
         if (!Misc.TokenValid(Context, ref ResponseObject))
         {
             return;
         }
         try { uint.Parse(SegmentedURL[3]); uint.Parse(Context.Request.Headers["Value"]); } catch { ResponseObject.Message = "Invalid Parameter"; ResponseObject.Status = 500; return; }
         Data.Objects.User User = Data.Objects.User.FromId(uint.Parse(SegmentedURL[3]));
         if (User == null)
         {
             ResponseObject.Message = "User doesnt exist"; ResponseObject.Status = 405; return;
         }
         User.Account.Balance = uint.Parse(Context.Request.Headers["Value"]);
         Data.Objects.Account.Update(User.Account);
         ResponseObject.Message = "Set Users Balance"; ResponseObject.Status = 200;
     }
     else if (SegmentedURL[1] == "auth" && SegmentedURL[2] == "check")
     {
         Misc.TokenValid(Context, ref ResponseObject);
     }
     else if (SegmentedURL[1] == "auth" && SegmentedURL[2] == "token")
     {
         Data.Objects.AuthToken AuthToken = Data.Objects.AuthToken.FromRefreshToken(Context.Request.Headers["RefreshToken"]);
         if (AuthToken != null)
         {
             ResponseObject.Data = AuthToken.ToJson(); ResponseObject.Message = "Succesfully performed 0Auth"; ResponseObject.Status = 200;
         }
         else
         {
             ResponseObject.Message = "That RefreshToken isnt valid"; ResponseObject.Status = 400;
         }
     }
     else if (SegmentedURL[1] == "log")
     {
         if (!Misc.TokenValid(Context, ref ResponseObject))
         {
             return;
         }
         ResponseObject.Message = "Read Log"; ResponseObject.Status = 200;
         ResponseObject.Data    = System.IO.File.ReadAllText("./Log.txt");
     }
     else
     {
         ResponseObject.Message = "Path Not Found"; ResponseObject.Status = 404;
     }
 }