|
| 1 | +using ScriptBloxAPI.DataTypes; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace ScriptBloxAPI.Methods |
| 11 | +{ |
| 12 | + public class Authorization |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Sends a ping request to the idle endpoint. |
| 16 | + /// </summary> |
| 17 | + /// <param name="authorization">The authorization token.</param> |
| 18 | + /// <returns>A BoolStatus object indicating if the request was successful and the response status code.</returns> |
| 19 | + public static BoolStatus PingIdle(string authorization) |
| 20 | + { |
| 21 | + MiscFunctions.HttpClient.DefaultRequestHeaders.Add("authorization", authorization); |
| 22 | + HttpResponseMessage response = MiscFunctions.HttpClient.GetAsync("https://scriptblox.com/api/ping/idle").Result; |
| 23 | + MiscFunctions.HttpClient.DefaultRequestHeaders.Remove("authorization"); |
| 24 | + |
| 25 | + return new BoolStatus(response.IsSuccessStatusCode, response.StatusCode.ToString()); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Changes the username of the user. |
| 30 | + /// </summary> |
| 31 | + /// <param name="authorization">The authorization token.</param> |
| 32 | + /// <param name="newUsername">The new username.</param> |
| 33 | + /// <returns>A BoolStatus object indicating if the username change was successful and the response text.</returns> |
| 34 | + public static BoolStatus ChangeUsername(string authorization, string newUsername) |
| 35 | + { |
| 36 | + HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/update", authorization, $"{{\"username\":\"{newUsername}\"}}"); |
| 37 | + |
| 38 | + return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password updated!"), response.Content.ReadAsStringAsync().Result); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Changes the password of the user. |
| 43 | + /// </summary> |
| 44 | + /// <param name="authorization">The authorization token.</param> |
| 45 | + /// <param name="oldPassword">The old password.</param> |
| 46 | + /// <param name="newPassword">The new password.</param> |
| 47 | + /// <returns>A BoolStatus object indicating if the password change was successful and the response text.</returns> |
| 48 | + public static BoolStatus ChangePassword(string authorization, string oldPassword, string newPassword) |
| 49 | + { |
| 50 | + HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/change-password", authorization, $"{{\"oldPassword\":\"{oldPassword}\",\"newPassword\":\"{newPassword}\"}}"); |
| 51 | + |
| 52 | + return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password updated!"), response.Content.ReadAsStringAsync().Result); |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Updates the bio of the user. |
| 57 | + /// </summary> |
| 58 | + /// <param name="authorization">The authorization token.</param> |
| 59 | + /// <param name="newBio">The new bio.</param> |
| 60 | + /// <returns>A BoolStatus object indicating if the bio update was successful and the response text.</returns> |
| 61 | + public static BoolStatus UpdateBio(string authorization, string newBio) |
| 62 | + { |
| 63 | + HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/update", authorization, $"{{\"bio\":\"{newBio}\"}}"); |
| 64 | + |
| 65 | + return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("User updated!"), response.Content.ReadAsStringAsync().Result); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Follows a user on ScriptBlox. |
| 70 | + /// </summary> |
| 71 | + /// <param name="authorization">The authorization token.</param> |
| 72 | + /// <param name="username">The username of the user to follow.</param> |
| 73 | + /// <returns>A BoolStatus object indicating if the follow operation was successful and the response text.</returns> |
| 74 | + public static BoolStatus FollowUser(string authorization, string username) |
| 75 | + { |
| 76 | + string UserId = UserMethods.GetUserIdFromName(username); |
| 77 | + HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/follow", authorization, $"{{\"userId\":\"{UserId}\"}}"); |
| 78 | + |
| 79 | + return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("You're now following "), response.Content.ReadAsStringAsync().Result); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Unfollows a user on ScriptBlox. |
| 84 | + /// </summary> |
| 85 | + /// <param name="authorization">The authorization token.</param> |
| 86 | + /// <param name="username">The username of the user to unfollow.</param> |
| 87 | + /// <returns>A BoolStatus object indicating if the unfollow operation was successful and the response text.</returns> |
| 88 | + public static BoolStatus UnFollowUser(string authorization, string username) |
| 89 | + { |
| 90 | + string UserId = UserMethods.GetUserIdFromName(username); |
| 91 | + HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/unfollow", authorization, $"{{\"userId\":\"{UserId}\"}}"); |
| 92 | + |
| 93 | + return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Unfollowed user "), response.Content.ReadAsStringAsync().Result); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Sends a password reset request. |
| 98 | + /// </summary> |
| 99 | + /// <param name="emailToReset">The email address of the user to reset the password for.</param> |
| 100 | + /// <returns>A BoolStatus object indicating if the password reset request was successful and the response text.</returns> |
| 101 | + public static BoolStatus SendPasswordReset(string emailToReset) |
| 102 | + { |
| 103 | + HttpResponseMessage response = SendRequest("https://scriptblox.com/api/user/reset-password", "", $"{{\"email\":\"{emailToReset}\"}}", false); |
| 104 | + |
| 105 | + return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password reset link has been sent to your email!"), response.Content.ReadAsStringAsync().Result); |
| 106 | + } |
| 107 | + |
| 108 | + internal static HttpResponseMessage SendRequest(string url, string authorization, string data, bool WithAuth = true) |
| 109 | + { |
| 110 | + if (WithAuth) |
| 111 | + MiscFunctions.HttpClient.DefaultRequestHeaders.Add("authorization", authorization); |
| 112 | + |
| 113 | + HttpResponseMessage response = MiscFunctions.HttpClient.PostAsync(url, new StringContent(data)).Result; |
| 114 | + |
| 115 | + if (WithAuth) |
| 116 | + MiscFunctions.HttpClient.DefaultRequestHeaders.Remove("authorization"); |
| 117 | + |
| 118 | + return response; |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | +} |
0 commit comments