diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-13 20:25:05 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-13 20:25:05 +0200 |
commit | 712ad189c99570f686ab779782b2a873e172428e (patch) | |
tree | 6102e4719416e71522e9143fa4e06951258bd77c /MatrixRoomUtils.Core/Extensions | |
parent | Fix passwords being visible during editing (diff) | |
download | MatrixUtils-712ad189c99570f686ab779782b2a873e172428e.tar.xz |
Change syntax style
Diffstat (limited to 'MatrixRoomUtils.Core/Extensions')
5 files changed, 45 insertions, 31 deletions
diff --git a/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs b/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs index cce71dd..c51baec 100644 --- a/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs @@ -1,15 +1,13 @@ namespace MatrixRoomUtils.Core.Extensions; -public static class DictionaryExtensions -{ - public static bool ChangeKey<TKey, TValue>(this IDictionary<TKey, TValue> dict, - TKey oldKey, TKey newKey) - { +public static class DictionaryExtensions { + public static bool ChangeKey<TKey, TValue>(this IDictionary<TKey, TValue> dict, + TKey oldKey, TKey newKey) { TValue value; if (!dict.Remove(oldKey, out value)) return false; - dict[newKey] = value; // or dict.Add(newKey, value) depending on ur comfort + dict[newKey] = value; // or dict.Add(newKey, value) depending on ur comfort return true; } } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs b/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs index 8eb0226..47b3121 100644 --- a/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs @@ -1,19 +1,40 @@ +using System.Text.Json; + namespace MatrixRoomUtils.Core.Extensions; -public static class HttpClientExtensions -{ - public static async Task<bool> CheckSuccessStatus(this HttpClient hc, string url) - { +public static class HttpClientExtensions { + public static async Task<bool> CheckSuccessStatus(this HttpClient hc, string url) { //cors causes failure, try to catch - try - { + try { var resp = await hc.GetAsync(url); return resp.IsSuccessStatusCode; } - catch (Exception e) - { + catch (Exception e) { Console.WriteLine($"Failed to check success status: {e.Message}"); return false; } } +} + +public class MatrixHttpClient : HttpClient { + public override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + var a = await base.SendAsync(request, cancellationToken); + if (!a.IsSuccessStatusCode) { + Console.WriteLine($"Failed to send request: {a.StatusCode}"); + var content = await a.Content.ReadAsStringAsync(cancellationToken); + if (content.StartsWith('{')) { + var ex = JsonSerializer.Deserialize<MatrixException>(content); + if (ex?.RetryAfterMs != null) { + await Task.Delay(ex.RetryAfterMs.Value, cancellationToken); + return await SendAsync(request, cancellationToken); + } + + throw ex!; + } + + throw new InvalidDataException("Encountered invalid data:\n" + content); + } + + return a; + } } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs b/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs index 725c832..b007136 100644 --- a/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/JsonElementExtensions.cs @@ -5,18 +5,16 @@ using System.Text.Json.Serialization; namespace MatrixRoomUtils.Core.Extensions; -public static class JsonElementExtensions -{ - public static void FindExtraJsonFields([DisallowNull] this JsonElement? res, Type t) - { +public static class JsonElementExtensions { + public static void FindExtraJsonFields([DisallowNull] this JsonElement? res, Type t) { var props = t.GetProperties(); var unknownPropertyFound = false; - foreach (var field in res.Value.EnumerateObject()) - { + foreach (var field in res.Value.EnumerateObject()) { if (props.Any(x => x.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name == field.Name)) continue; Console.WriteLine($"[!!] Unknown property {field.Name} in {t.Name}!"); unknownPropertyFound = true; } - if(unknownPropertyFound) Console.WriteLine(res.Value.ToJson()); + + if (unknownPropertyFound) Console.WriteLine(res.Value.ToJson()); } } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs b/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs index 5aa9645..812c81c 100644 --- a/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/ObjectExtensions.cs @@ -1,15 +1,14 @@ +using System.Text.Encodings.Web; using System.Text.Json; namespace MatrixRoomUtils.Core.Extensions; -public static class ObjectExtensions -{ - public static string ToJson(this object obj, bool indent = true, bool ignoreNull = false, bool unsafeContent = false) - { +public static class ObjectExtensions { + public static string ToJson(this object obj, bool indent = true, bool ignoreNull = false, bool unsafeContent = false) { var jso = new JsonSerializerOptions(); - if(indent) jso.WriteIndented = true; - if(ignoreNull) jso.IgnoreNullValues = true; - if(unsafeContent) jso.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping; + if (indent) jso.WriteIndented = true; + if (ignoreNull) jso.IgnoreNullValues = true; + if (unsafeContent) jso.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping; return JsonSerializer.Serialize(obj, jso); } } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Extensions/StringExtensions.cs b/MatrixRoomUtils.Core/Extensions/StringExtensions.cs index 8fadc6d..b81d59f 100644 --- a/MatrixRoomUtils.Core/Extensions/StringExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/StringExtensions.cs @@ -1,7 +1,6 @@ namespace MatrixRoomUtils.Core.Extensions; -public static class StringExtensions -{ +public static class StringExtensions { // public static async Task<string> GetMediaUrl(this string MxcUrl) // { // //MxcUrl: mxc://rory.gay/ocRVanZoUTCcifcVNwXgbtTg @@ -11,5 +10,4 @@ public static class StringExtensions // var mediaId = MxcUrl.Split('/')[3]; // return $"{(await new RemoteHomeServer(server).Configure()).FullHomeServerDomain}/_matrix/media/v3/download/{server}/{mediaId}"; // } - } \ No newline at end of file |