|
6 | 6 | using System.Linq;
|
7 | 7 | using System.Reactive.Linq;
|
8 | 8 | using System.Threading.Tasks;
|
| 9 | +using System.Text.RegularExpressions; |
9 | 10 |
|
10 | 11 | namespace RedditSharp.Things
|
11 | 12 | {
|
@@ -143,9 +144,20 @@ public async Task<Comment> CommentAsync(string message)
|
143 | 144 | thing_id = FullName,
|
144 | 145 | api_type = "json"
|
145 | 146 | }).ConfigureAwait(false);
|
146 |
| - if (json["json"]["ratelimit"] != null) |
147 |
| - throw new RateLimitException(TimeSpan.FromSeconds(json["json"]["ratelimit"].ValueOrDefault<double>())); |
148 |
| - return new Comment(WebAgent, json["json"]["data"]["things"][0], this); |
| 147 | + if (json["errors"].Any()) |
| 148 | + { |
| 149 | + if (json["errors"][0].Any(x => x.ToString() == "RATELIMIT" || x.ToString() == "ratelimit")) |
| 150 | + { |
| 151 | + var timeToReset = TimeSpan.FromMinutes(Convert.ToDouble(Regex.Match(json["errors"][0].ElementAt(1).ToString(), @"\d+").Value)); |
| 152 | + throw new RateLimitException(timeToReset); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + throw new Exception(json["errors"][0][0].ToString()); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return new Comment(WebAgent, json["data"]["things"][0], this); |
149 | 161 | }
|
150 | 162 |
|
151 | 163 | private async Task<JToken> SimpleActionToggleAsync(string endpoint, bool value, bool requiresModAction = false)
|
@@ -204,7 +216,7 @@ public async Task EditTextAsync(string newText)
|
204 | 216 | text = newText,
|
205 | 217 | thing_id = FullName
|
206 | 218 | }).ConfigureAwait(false);
|
207 |
| - if (json["json"].ToString().Contains("\"errors\": []")) |
| 219 | + if (!json["errors"].Any()) |
208 | 220 | SelfText = newText;
|
209 | 221 | else
|
210 | 222 | throw new Exception("Error editing text.");
|
@@ -319,7 +331,7 @@ public IAsyncEnumerable<Comment> EnumerateCommentTreeAsync(int limitPerRequest =
|
319 | 331 | {
|
320 | 332 | return new CommentsEnumarable(WebAgent, this, limitPerRequest);
|
321 | 333 | }
|
322 |
| - #region Static Operations |
| 334 | +#region Static Operations |
323 | 335 | /// <summary>
|
324 | 336 | /// Sets flair of given post by <paramref name="fullname"/>
|
325 | 337 | /// </summary>
|
|
0 commit comments