Skip to content

Commit 2b84111

Browse files
committed
Conversion Class
1 parent 572a651 commit 2b84111

File tree

10 files changed

+579
-27
lines changed

10 files changed

+579
-27
lines changed

.vs/ScriptBloxAPI/v17/.suo

-8 KB
Binary file not shown.

Methods/Authorization.cs

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using ScriptBloxAPI.DataTypes;
22
using System.Net.Http;
33
using ScriptBloxAPI.Backend_Functions;
4+
using System.Threading.Tasks;
45

56
namespace ScriptBloxAPI.Methods
67
{
78
public class Authorization
89
{
10+
#region Non Async
11+
912
/// <summary>
1013
/// Sends a ping request to the idle endpoint.
1114
/// </summary>
@@ -48,7 +51,7 @@ public static BoolStatus ChangePassword(string authorization, string oldPassword
4851

4952
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password updated!"), response.Content.ReadAsStringAsync().Result);
5053
}
51-
54+
5255
/// <summary>
5356
/// Updates the bio of the user.
5457
/// </summary>
@@ -101,7 +104,7 @@ public static BoolStatus SendPasswordReset(string emailToReset)
101104

102105
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password reset link has been sent to your email!"), response.Content.ReadAsStringAsync().Result);
103106
}
104-
107+
105108
internal static HttpResponseMessage SendRequest(string url, string authorization, string data, bool withAuth = true)
106109
{
107110
if (withAuth)
@@ -115,5 +118,121 @@ internal static HttpResponseMessage SendRequest(string url, string authorization
115118
return response;
116119
}
117120

121+
#endregion
122+
#region Async Methods
123+
124+
/// <summary>
125+
/// Sends a ping request to the idle endpoint asynchronously.
126+
/// </summary>
127+
/// <param name="authorization">The authorization token.</param>
128+
/// <returns>A Task BoolStatus representing the asynchronous operation, indicating if the request was successful and the response status code.</returns>
129+
public static async Task<BoolStatus> PingIdleAsync(string authorization)
130+
{
131+
MiscFunctions.HttpClient.DefaultRequestHeaders.Add("authorization", authorization);
132+
HttpResponseMessage response = await MiscFunctions.HttpClient.GetAsync("https://scriptblox.com/api/ping/idle");
133+
MiscFunctions.HttpClient.DefaultRequestHeaders.Remove("authorization");
134+
135+
return new BoolStatus(response.IsSuccessStatusCode, response.StatusCode.ToString());
136+
}
137+
138+
/// <summary>
139+
/// Changes the username of the user asynchronously.
140+
/// </summary>
141+
/// <param name="authorization">The authorization token.</param>
142+
/// <param name="newUsername">The new username.</param>
143+
/// <returns>A Task BoolStatus representing the asynchronous operation, indicating if the username change was successful and the response text.</returns>
144+
public static async Task<BoolStatus> ChangeUsernameAsync(string authorization, string newUsername)
145+
{
146+
if (await UserMethods.IsUsernameTakenAsync(newUsername))
147+
return new BoolStatus(false, "Username taken.");
148+
149+
HttpResponseMessage response = await SendRequestAsync("https://scriptblox.com/api/user/update", authorization, $"{{\"username\":\"{newUsername}\"}}");
150+
151+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password updated!"), response.Content.ReadAsStringAsync().Result);
152+
}
153+
154+
155+
/// <summary>
156+
/// Changes the password of the user asynchronously.
157+
/// </summary>
158+
/// <param name="authorization">The authorization token.</param>
159+
/// <param name="oldPassword">The old password.</param>
160+
/// <param name="newPassword">The new password.</param>
161+
/// <returns>A Task BoolStatus representing the asynchronous operation, indicating if the password change was successful and the response text.</returns>
162+
public static async Task<BoolStatus> ChangePasswordAsync(string authorization, string oldPassword, string newPassword)
163+
{
164+
HttpResponseMessage response = await SendRequestAsync("https://scriptblox.com/api/user/change-password", authorization, $"{{\"oldPassword\":\"{oldPassword}\",\"newPassword\":\"{newPassword}\"}}");
165+
166+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password updated!"), response.Content.ReadAsStringAsync().Result);
167+
}
168+
169+
/// <summary>
170+
/// Updates the bio of the user asynchronously.
171+
/// </summary>
172+
/// <param name="authorization">The authorization token.</param>
173+
/// <param name="newBio">The new bio.</param>
174+
/// <returns>A Task BoolStatus representing the asynchronous operation, indicating if the bio update was successful and the response text.</returns>
175+
public static async Task<BoolStatus> UpdateBioAsync(string authorization, string newBio)
176+
{
177+
HttpResponseMessage response = await SendRequestAsync("https://scriptblox.com/api/user/update", authorization, $"{{\"bio\":\"{newBio}\"}}");
178+
179+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("User updated!"), response.Content.ReadAsStringAsync().Result);
180+
}
181+
182+
/// <summary>
183+
/// Follows a user on ScriptBlox asynchronously.
184+
/// </summary>
185+
/// <param name="authorization">The authorization token.</param>
186+
/// <param name="username">The username of the user to follow.</param>
187+
/// <returns>A Task BoolStatus representing the asynchronous operation, indicating if the follow operation was successful and the response text.</returns>
188+
public static async Task<BoolStatus> FollowUserAsync(string authorization, string username)
189+
{
190+
string userId = await UserMethods.GetUserIdFromNameAsync(username);
191+
HttpResponseMessage response = await SendRequestAsync("https://scriptblox.com/api/user/follow", authorization, $"{{\"userId\":\"{userId}\"}}");
192+
193+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("You're now following "), response.Content.ReadAsStringAsync().Result);
194+
}
195+
196+
197+
/// <summary>
198+
/// Unfollows a user on ScriptBlox asynchronously.
199+
/// </summary>
200+
/// <param name="authorization">The authorization token.</param>
201+
/// <param name="username">The username of the user to unfollow.</param>
202+
/// <returns>A Task BoolStatus representing the asynchronous operation, indicating if the unfollow operation was successful and the response text.</returns>
203+
public static async Task<BoolStatus> UnFollowUserAsync(string authorization, string username)
204+
{
205+
string userId = await UserMethods.GetUserIdFromNameAsync(username);
206+
HttpResponseMessage response = await SendRequestAsync("https://scriptblox.com/api/user/unfollow", authorization, $"{{\"userId\":\"{userId}\"}}");
207+
208+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Unfollowed user "), response.Content.ReadAsStringAsync().Result);
209+
}
210+
211+
/// <summary>
212+
/// Sends a password reset request asynchronously.
213+
/// </summary>
214+
/// <param name="emailToReset">The email address of the user to reset the password for.</param>
215+
/// <returns>A Task BoolStatus representing the asynchronous operation, indicating if the password reset request was successful and the response text.</returns>
216+
public static async Task<BoolStatus> SendPasswordResetAsync(string emailToReset)
217+
{
218+
HttpResponseMessage response = await SendRequestAsync("https://scriptblox.com/api/user/reset-password", "", $"{{\"email\":\"{emailToReset}\"}}", false);
219+
220+
return new BoolStatus(response.Content.ReadAsStringAsync().Result.Contains("Password reset link has been sent to your email!"), response.Content.ReadAsStringAsync().Result);
221+
}
222+
223+
internal static async Task<HttpResponseMessage> SendRequestAsync(string url, string authorization, string data, bool withAuth = true)
224+
{
225+
if (withAuth)
226+
MiscFunctions.HttpClient.DefaultRequestHeaders.Add("authorization", authorization);
227+
228+
HttpResponseMessage response = await MiscFunctions.HttpClient.PostAsync(url, new StringContent(data));
229+
230+
if (withAuth)
231+
MiscFunctions.HttpClient.DefaultRequestHeaders.Remove("authorization");
232+
233+
return response;
234+
}
235+
236+
#endregion
118237
}
119238
}

Methods/Commenting.cs

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
using System.Collections.Generic;
44
using System.Net.Http;
55
using ScriptBloxAPI.Backend_Functions;
6+
using System.Threading.Tasks;
67

78
namespace ScriptBloxAPI.Methods
89
{
910
public class Commenting
1011
{
12+
#region Non Async
13+
1114
/// <summary>
1215
/// Adds a comment to the specified script using ScriptBlox.
1316
/// </summary>
@@ -38,8 +41,8 @@ public static CommentObject AddComment(ScriptObject script, string authorization
3841
UserMethods.GetUserFromUserId(commentData?.Value<string>("commentBy"))
3942
);
4043

41-
}
42-
44+
}
45+
4346
/// <summary>
4447
/// Deletes a comment with the specified comment ID using the ScriptBlox API.
4548
/// </summary>
@@ -55,6 +58,7 @@ public static BoolStatus DeleteComment(string authorization, string commentId)
5558
return new BoolStatus(response.IsSuccessStatusCode, response.Content.ReadAsStringAsync().Result);
5659
}
5760

61+
5862
/// <summary>
5963
/// Retrieves the comments associated with a script from ScriptBlox.
6064
/// </summary>
@@ -89,7 +93,7 @@ public static List<CommentObject> GetCommentsFromScript(ScriptObject script)
8993

9094
return comments;
9195
}
92-
96+
9397
internal static HttpResponseMessage SendRequest(string url, string authorization, string data, bool withAuth = true)
9498
{
9599
if (withAuth)
@@ -102,5 +106,105 @@ internal static HttpResponseMessage SendRequest(string url, string authorization
102106

103107
return response;
104108
}
109+
110+
#endregion
111+
#region Async
112+
113+
/// <summary>
114+
/// Retrieves the comments associated with a script from ScriptBlox.
115+
/// </summary>
116+
/// <param name="script">The ScriptObject representing the script.</param>
117+
/// <returns>A list of CommentObject representing the comments associated with the script.</returns>
118+
/// <exception cref="ScriptBloxException">
119+
/// Thrown when an error occurs while fetching the JSON, when the API returns an error message, or when a backend error occurs.
120+
/// </exception>
121+
public static async Task<List<CommentObject>> GetCommentsFromScriptAsync(ScriptObject script)
122+
{
123+
List<CommentObject> comments = new List<CommentObject>();
124+
125+
JToken jsonReturn = JToken.Parse(await MiscFunctions.HttpClient.GetStringAsync($"https://scriptblox.com/api/comment/{script.Id}?page=1&max=999"));
126+
127+
if (jsonReturn == null)
128+
throw new ScriptBloxException("An error has occurred while fetching the JSON, please submit a bug report.");
129+
if (jsonReturn["message"] != null)
130+
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
131+
if (jsonReturn["comments"] == null)
132+
throw new ScriptBloxException("Backend error occurred.");
133+
134+
foreach (JToken comment in jsonReturn["comments"])
135+
{
136+
comments.Add(new CommentObject(
137+
comment.Value<string>("_id"),
138+
comment.Value<string>("text"),
139+
comment.Value<int>("likeCount"),
140+
comment.Value<int>("dislikeCount"),
141+
await UserMethods.GetUserFromUsernameAsync(comment["commentBy"]?.Value<string>("username"))
142+
));
143+
}
144+
145+
return comments;
146+
}
147+
148+
/// <summary>
149+
/// Deletes a comment with the specified comment ID using the ScriptBlox API.
150+
/// </summary>
151+
/// <param name="authorization">The authorization token.</param>
152+
/// <param name="commentId">The ID of the comment to delete.</param>
153+
/// <returns>A BoolStatus indicating whether the comment deletion was successful.</returns>
154+
public static async Task<BoolStatus> DeleteCommentAsync(string authorization, string commentId)
155+
{
156+
MiscFunctions.HttpClient.DefaultRequestHeaders.Add("authorization", authorization);
157+
HttpResponseMessage response = await MiscFunctions.HttpClient.DeleteAsync($"https://scriptblox.com/api/comment/{commentId}");
158+
MiscFunctions.HttpClient.DefaultRequestHeaders.Remove("authorization");
159+
160+
return new BoolStatus(response.IsSuccessStatusCode, await response.Content.ReadAsStringAsync());
161+
}
162+
163+
/// <summary>
164+
/// Adds a comment to the specified script using ScriptBlox.
165+
/// </summary>
166+
/// <param name="script">The script object to add the comment to.</param>
167+
/// <param name="authorization">The authorization token.</param>
168+
/// <param name="comment">The text of the comment.</param>
169+
/// <returns>The newly added CommentObject.</returns>
170+
public static async Task<CommentObject> AddCommentAsync(ScriptObject script, string authorization, string comment)
171+
{
172+
HttpResponseMessage response = await SendRequestAsync("https://scriptblox.com/api/comment/add", authorization, $"{{\"scriptId\":\"{script.Id}\",\"text\":\"{comment}\"}}");
173+
174+
JToken jsonReturn = JToken.Parse(await response.Content.ReadAsStringAsync());
175+
176+
if (jsonReturn == null)
177+
throw new ScriptBloxException("An error has occurred while fetching the JSON, please submit a bug report.");
178+
if (jsonReturn["message"] == null)
179+
throw new ScriptBloxException("An error has occurred while adding comments: message <null>");
180+
if (jsonReturn.Value<string>("message") != "Commented!")
181+
throw new ScriptBloxException(jsonReturn.Value<string>("message"));
182+
183+
JToken commentData = jsonReturn["comment"];
184+
185+
return new CommentObject(
186+
commentData?.Value<string>("_id") ?? "-1",
187+
commentData?.Value<string>("text") ?? "-1",
188+
0,
189+
0,
190+
await UserMethods.GetUserFromUserIdAsync(commentData?.Value<string>("commentBy"))
191+
);
192+
}
193+
194+
internal static async Task<HttpResponseMessage> SendRequestAsync(string url, string authorization, string data, bool withAuth = true)
195+
{
196+
if (withAuth)
197+
MiscFunctions.HttpClient.DefaultRequestHeaders.Add("authorization", authorization);
198+
199+
HttpResponseMessage response = await MiscFunctions.HttpClient.PostAsync(url, new StringContent(data));
200+
201+
if (withAuth)
202+
MiscFunctions.HttpClient.DefaultRequestHeaders.Remove("authorization");
203+
204+
return response;
205+
}
206+
207+
#endregion
208+
105209
}
106210
}

Methods/Converter.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Drawing;
3+
using System.IO;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using ScriptBloxAPI.Backend_Functions;
8+
9+
namespace ScriptBloxAPI.Methods
10+
{
11+
public class Converter
12+
{
13+
public static async Task<Bitmap> ConvertWebPToBitmap(string fileUrl)
14+
{
15+
try
16+
{
17+
byte[] imageBytes = await MiscFunctions.HttpClient.GetByteArrayAsync(fileUrl);
18+
19+
using StringContent imageJsonFormat = new StringContent(
20+
$"{{\"image\": \"{Convert.ToBase64String(imageBytes)}\"}}", Encoding.UTF8, "application/json");
21+
22+
HttpResponseMessage response =
23+
await MiscFunctions.HttpClient.PostAsync("https://scriptblox.com/api/user/image", imageJsonFormat);
24+
25+
Bitmap finalBitmap =
26+
new Bitmap(new MemoryStream(Convert.FromBase64String(response.Content.ReadAsStringAsync().Result)));
27+
28+
return response.IsSuccessStatusCode
29+
? finalBitmap
30+
: throw new ScriptBloxException(
31+
$"An error occurred while sending the request: {response.Content.ReadAsStringAsync().Result}");
32+
}
33+
catch (Exception ex)
34+
{
35+
throw new ScriptBloxException($"An error occurred while trying to handle conversion: {ex}");
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)