diff --git a/Airly.Net/Airly.Net.csproj b/Airly.Net/Airly.Net.csproj index 090daab..8c69737 100644 --- a/Airly.Net/Airly.Net.csproj +++ b/Airly.Net/Airly.Net.csproj @@ -7,13 +7,13 @@ AirlyNet true AirlyNet - 0.1.2 + 0.1.8 Mondonno en-US Mondonno https://github.com/Mondonno/Airly.Net - The pre-release of our wrapper - airly airly.net c# csharp + All changelog informations are avaible on our github repository + airly airly.net c# csharp api client api-client Airly.Net Wrapper for the Airly API written 100% in C# https://cdn.airly.org/assets/brand/icon/primary/airly-32.png diff --git a/Airly.Net/Airly.cs b/Airly.Net/Airly.cs index 6aaf264..6cc9814 100644 --- a/Airly.Net/Airly.cs +++ b/Airly.Net/Airly.cs @@ -3,7 +3,7 @@ using AirlyNet.Interactions; using AirlyNet.Rest; using AirlyNet.Utilities; -using AirlyNet.Models; +using AirlyNet.Models; namespace AirlyNet { diff --git a/Airly.Net/common/handling/Handlings.cs b/Airly.Net/common/handling/Handlings.cs index f551cd2..09aad4b 100644 --- a/Airly.Net/common/handling/Handlings.cs +++ b/Airly.Net/common/handling/Handlings.cs @@ -28,7 +28,6 @@ public class ErrorInformation public class ErrorDeserializer { - private Utils util { get; set; } = new(); protected HttpResponseMessage HttpResponse { get; set; } private string Json { get; set; } @@ -41,7 +40,7 @@ public ErrorDeserializer(HttpResponseMessage httpResponse,string json) private string GetSuccesor(HttpHeaders httpHeaders) { - var header = util.GetHeader(httpHeaders, "Location"); + var header = RestUtil.GetHeader(httpHeaders, "Location"); string value = header != null ? header : null; return value; } @@ -103,12 +102,12 @@ public static void MakeRateLimitError(string limits, string all, string customMe throw error; } - public static void MakeRateLimitError(RawRestResponse res, Utils utils, string customMessage = null) + public static void MakeRateLimitError(RawRestResponse res, string customMessage = null) { var headers = res.HttpResponse.Headers; - string limits = utils.GetHeader(headers, "X-RateLimit-Limit-day"); - string all = utils.CalculateRateLimit(headers).ToString(); + string limits = RestUtil.GetHeader(headers, "X-RateLimit-Limit-day"); + string all = RatelimitsUtil.CalculateRateLimit(headers).ToString(); MakeRateLimitError(limits, all, customMessage); } @@ -164,7 +163,6 @@ protected void InternalHandleResponseCode(int? responseCode = null) int statusCode = responseCode ?? (int) HttpResponseCode; string rawJson = ResponseJson; - Utils utils = new(); JsonErrorHandler handler = new(rawJson); HttpResponseHeaders headers = ResponseHeaders; @@ -175,9 +173,9 @@ protected void InternalHandleResponseCode(int? responseCode = null) return; }; - int? limit = utils.CalculateRateLimit(headers); + int? limit = RatelimitsUtil.CalculateRateLimit(headers); - if (limit == 0) throw new AirlyError($"Get ratelimited by airly api\n{utils.CalculateRateLimit(headers)}"); + if (limit == 0) throw new AirlyError($"Get ratelimited by airly api\n{RatelimitsUtil.CalculateRateLimit(headers)}"); if (statusCode > 200 && statusCode <= 300) { if (statusCode == 301) @@ -192,7 +190,7 @@ protected void InternalHandleResponseCode(int? responseCode = null) if (statusCode == 401) throw new AirlyError("The provided API Key is not valid"); if (statusCode == 429) { - RateLimitThrower.MakeRateLimitError(utils.GetHeader(headers, "X-RateLimit-Limit-day"), $"{utils.CalculateRateLimit(headers)}", ""); + RateLimitThrower.MakeRateLimitError(RestUtil.GetHeader(headers, "X-RateLimit-Limit-day"), $"{RatelimitsUtil.CalculateRateLimit(headers)}", ""); return; } handler.HandleMalformed(); diff --git a/Airly.Net/common/handling/RateLimitInfo.cs b/Airly.Net/common/handling/RateLimitInfo.cs index 81cdbb2..b8f9b7d 100644 --- a/Airly.Net/common/handling/RateLimitInfo.cs +++ b/Airly.Net/common/handling/RateLimitInfo.cs @@ -9,8 +9,6 @@ namespace AirlyNet.Handling { public class RateLimitInfo { - private Utils Util { get; set; } - public bool IsRateLimited { get; set; } public int? Limit { get; set; } public int? Remain { get; set; } @@ -18,12 +16,12 @@ public class RateLimitInfo public RateLimitInfo(HttpHeaders httpHeaders) { - string limit = Util.GetHeader(httpHeaders, XLimitName) ?? null; - string remain = Util.GetHeader(httpHeaders, XRemainingName) ?? null; + string limit = RestUtil.GetHeader(httpHeaders, XLimitName) ?? null; + string remain = RestUtil.GetHeader(httpHeaders, XRemainingName) ?? null; Limit = limit != null ? Convert.ToInt32(limit) : null; Remain = remain != null ? Convert.ToInt32(remain) : null; - Diffrence = Util.CalculateRateLimit(Remain, Limit); + Diffrence = RatelimitsUtil.CalculateRateLimit(Remain, Limit); IsRateLimited = Diffrence == 0 || Diffrence == null; } diff --git a/Airly.Net/rest/RestManager.cs b/Airly.Net/rest/RestManager.cs index fe3693e..ff16ac6 100644 --- a/Airly.Net/rest/RestManager.cs +++ b/Airly.Net/rest/RestManager.cs @@ -56,9 +56,8 @@ public async Task Request(string end, string method = null, RequestOptions } public async Task Request(string end, string method = null, dynamic query = null, RequestOptions options = null) { - Utils util = new(); RequestOptions requestOptions = options ?? (new()); - if (query != null) requestOptions.Query = util.ParseQuery(query); + if (query != null) requestOptions.Query = Utils.ParseQuery(query); return await Request(end, method, requestOptions); } diff --git a/Airly.Net/rest/api/RequestModule.cs b/Airly.Net/rest/api/RequestModule.cs index 2d82c53..6a69428 100644 --- a/Airly.Net/rest/api/RequestModule.cs +++ b/Airly.Net/rest/api/RequestModule.cs @@ -23,7 +23,6 @@ public class DefaultRestRequest : IRequest, IDisposable public RequestOptions RestOptions { get; set; } public AirlyConfiguration RestConfiguration { get; set; } private HttpClient HttpClient { get; set; } - public Utils Util { get; set; } = new Utils(); public Dictionary DefaultHeaders = new Dictionary() { @@ -70,7 +69,7 @@ public DefaultRestRequest(RESTManager rest, string end, string method, RequestOp query += string.Format("{0}={1}&", segment[0], segment[1]); query = query.EndsWith("&") ? query.Remove(query.Length - 1, 1) : query; - query = Util.FormatQuery(query); + query = Utils.FormatQuery(query); } url = !string.IsNullOrEmpty(query) ? url + query : url; diff --git a/Airly.Net/rest/queue/RequestQueuer.cs b/Airly.Net/rest/queue/RequestQueuer.cs index db59a87..a50f019 100644 --- a/Airly.Net/rest/queue/RequestQueuer.cs +++ b/Airly.Net/rest/queue/RequestQueuer.cs @@ -61,7 +61,6 @@ private JToken ConvertJsonString(string json) private async Task Make(RestRequest request) { - Utils utils = new(); RawRestResponse res; try { res = await request.InvokeRequest(handle: true); } @@ -71,7 +70,7 @@ private async Task Make(RestRequest request) if (RateLimited) { var details = new RateLimitInfo(res.HttpResponse); - if (details.IsRateLimited) RateLimitThrower.MakeRateLimitError(res, utils, null); + if (details.IsRateLimited) RateLimitThrower.MakeRateLimitError(res, null); else { RateLimited = false; System.Diagnostics. @@ -83,7 +82,7 @@ private async Task Make(RestRequest request) HttpResponseHeaders headers = res.HttpResponse.Headers; int statusCode = (int) res.HttpResponse.StatusCode; - string rawDate = utils.GetHeader(headers, "Date"); + string rawDate = RestUtil.GetHeader(headers, "Date"); RestResponse constructedResponse = new() { diff --git a/Airly.Net/utils/Utils.cs b/Airly.Net/utils/Utils.cs index e47cc54..c57817c 100644 --- a/Airly.Net/utils/Utils.cs +++ b/Airly.Net/utils/Utils.cs @@ -12,108 +12,17 @@ namespace AirlyNet.Utilities { - public sealed class Utils + public static class Utils { - // Checking if the ratelimit is reached - private bool GetRateLimitBase(string XRemaining, string XLimit) - { - int rateLimitRemaining; - int rateLimitAll; - int rateLimit; - - bool rateLimitCheck = false; - - bool headersExists = XRemaining == null || XRemaining == "" || XLimit == null || XLimit == ""; - - if (!headersExists) - { - rateLimitRemaining = Convert.ToInt32(XRemaining); - rateLimitAll = Convert.ToInt32(XLimit); - - // Like - // 100 (ratelimit) - 23 (uses) = 77 - rateLimit = rateLimitAll - rateLimitRemaining; - - // If the ratelimit is reached the rateLimit value is 100 (any avaible request can now be sent after ratelimit reset) - rateLimitCheck = rateLimit == 100; - - return rateLimitCheck; - } - - return rateLimitCheck; - } - - // Getting and calculating the ratelimits for the headers - public bool GetRatelimit(HttpResponseHeaders headers) - { - string XRemaining = GetHeader(headers, RateLimitInfo.XRemainingName); - string XLimit = GetHeader(headers, RateLimitInfo.XLimitName); - - object rateLimit = GetRateLimitBase(XRemaining, XLimit); - return (bool)rateLimit; - } - - // The method to get the ratlimits from the response message - public bool GetRatelimit(RestResponse response) - { - HttpResponseHeaders headers = response.ResponseHeaders; - - string XRemaining = GetHeader(headers, RateLimitInfo.XRemainingName); - string XLimit = GetHeader(headers, RateLimitInfo.XLimitName); - - object rateLimit = GetRateLimitBase(XRemaining, XLimit); - return (bool)rateLimit; - } - - public int? CalculateRateLimit(int? XRemaining, int? XLimit) - { - if (XRemaining == null || XLimit == null) return null; - - int? calculated = XLimit - (XLimit - XRemaining); - return calculated; - } - - // Calculating the ratelimits diffrents - public int? CalculateRateLimit(RestResponse res) => CalculateRateLimit(res.ResponseHeaders); - public int? CalculateRateLimit(HttpResponseHeaders responseHeaders) - { - var headers = responseHeaders; - - string XRemaining = GetHeader(headers, RateLimitInfo.XRemainingName); - string XLimit = GetHeader(headers, RateLimitInfo.XLimitName); - - if (XRemaining == null || XLimit == null) return null; - - int cnv1 = Convert.ToInt32(XRemaining); - int cnv2 = Convert.ToInt32(XLimit); - - return CalculateRateLimit(cnv1, cnv2); - } - - // For response headers - // Getting the header first value because the headers can have multiple values (Airly API always return one value headers) - private string GetHeaderBase(IEnumerable values) => this.GetFirstEnumarable(values); - public string GetHeader(HttpHeaders headers, string key) - { - string values = null; - try - { - values = GetHeaderBase(headers.GetValues(key)); - } - catch (Exception) - { } - return values; - } - // Simple coping the one dictonary to another without the overwriting - public void CopyDictonaryValues(ref IDictionary target, IDictionary toCopyInto) + public static void CopyDictonaryValues(ref IDictionary target, IDictionary toCopyInto) { foreach (var pair in toCopyInto) target.Add(pair.Key, pair.Value); } // Simple converting JTokens to the JObjects by exclipting the C# types - public JObject[] ConvertTokens(JToken[] tokens) + public static JObject[] ConvertTokens(JToken[] tokens) { JObject[] jObjects = new JObject[0]; foreach (var token in tokens) @@ -124,7 +33,7 @@ public JObject[] ConvertTokens(JToken[] tokens) } // Formmatting web query - public string FormatQuery(string query) + public static string FormatQuery(string query) { if (query.StartsWith("?")) query = query.Remove(0, 1); @@ -151,7 +60,7 @@ public NormalizedProperty(string name, object value) } // Klasy anonimowe sa odczytywane jako normalne klasy bez dziedziczenia oraz bez konstruktora. system wartoĊ›ci PropertyInfo jest identyczny - public List GetClassProperties(T classObject) + public static List GetClassProperties(T classObject) { Type classType = classObject.GetType(); PropertyInfo[] props = classType.GetProperties(); @@ -177,7 +86,7 @@ public List GetClassProperties(T classObject) return normalizedProperties; } - public bool IsDouble(object obj) + public static bool IsDouble(object obj) { double? result; try @@ -192,7 +101,7 @@ public bool IsDouble(object obj) else return false; } - public string[][] ParseQuery(dynamic query) + public static string[][] ParseQuery(dynamic query) { NumberFormatInfo numberInfo = new NumberFormatInfo() { @@ -220,9 +129,9 @@ public string[][] ParseQuery(dynamic query) return convertedQuery; } - public T GetFirstEnumarable(IEnumerable enumarable) => enumarable.First((e) => true); + public static T GetFirstEnumarable(IEnumerable enumarable) => enumarable.First((e) => true); - public object InvokeMethod(T obj, string methodName, object[] parameters) + public static object InvokeMethod(T obj, string methodName, object[] parameters) { Type type = obj.GetType(); MethodInfo method = type.GetMethod(methodName); @@ -231,7 +140,7 @@ public object InvokeMethod(T obj, string methodName, object[] parameters) return result; } - public bool IsPropertyExists(T obj, string name) + public static bool IsPropertyExists(T obj, string name) { Type type = obj.GetType(); PropertyInfo result = type.GetProperty(name); @@ -281,7 +190,7 @@ public static string GetRoute(string url) } [Obsolete] - public string ReplaceDashUpper(string str) + public static string ReplaceDashUpper(string str) { string finalString = ""; string[] strs = str.Split('-'); @@ -295,6 +204,104 @@ public string ReplaceDashUpper(string str) } } + public static class RatelimitsUtil + { + // Checking if the ratelimit is reached + private static bool GetRateLimitBase(string XRemaining, string XLimit) + { + int rateLimitRemaining; + int rateLimitAll; + int rateLimit; + + bool rateLimitCheck = false; + + bool headersExists = XRemaining == null || XRemaining == "" || XLimit == null || XLimit == ""; + + if (!headersExists) + { + rateLimitRemaining = Convert.ToInt32(XRemaining); + rateLimitAll = Convert.ToInt32(XLimit); + + // Like + // 100 (ratelimit) - 23 (uses) = 77 + rateLimit = rateLimitAll - rateLimitRemaining; + + // If the ratelimit is reached the rateLimit value is 100 (any avaible request can now be sent after ratelimit reset) + rateLimitCheck = rateLimit == 100; + + return rateLimitCheck; + } + + return rateLimitCheck; + } + + // Getting and calculating the ratelimits for the headers + public static bool GetRatelimit(HttpResponseHeaders headers) + { + string XRemaining = RestUtil.GetHeader(headers, RateLimitInfo.XRemainingName); + string XLimit = RestUtil.GetHeader(headers, RateLimitInfo.XLimitName); + + object rateLimit = GetRateLimitBase(XRemaining, XLimit); + return (bool)rateLimit; + } + + // The method to get the ratlimits from the response message + public static bool GetRatelimit(RestResponse response) + { + HttpResponseHeaders headers = response.ResponseHeaders; + + string XRemaining = RestUtil.GetHeader(headers, RateLimitInfo.XRemainingName); + string XLimit = RestUtil.GetHeader(headers, RateLimitInfo.XLimitName); + + object rateLimit = GetRateLimitBase(XRemaining, XLimit); + return (bool)rateLimit; + } + + public static int? CalculateRateLimit(int? XRemaining, int? XLimit) + { + if (XRemaining == null || XLimit == null) return null; + + int? calculated = XLimit - (XLimit - XRemaining); + return calculated; + } + + // Calculating the ratelimits diffrents + public static int? CalculateRateLimit(RestResponse res) => CalculateRateLimit(res.ResponseHeaders); + public static int? CalculateRateLimit(HttpResponseHeaders responseHeaders) + { + var headers = responseHeaders; + + string XRemaining = RestUtil.GetHeader(headers, RateLimitInfo.XRemainingName); + string XLimit = RestUtil.GetHeader(headers, RateLimitInfo.XLimitName); + + if (XRemaining == null || XLimit == null) return null; + + int cnv1 = Convert.ToInt32(XRemaining); + int cnv2 = Convert.ToInt32(XLimit); + + return CalculateRateLimit(cnv1, cnv2); + } + } + + public static class RestUtil + { + + // For response headers + // Getting the header first value because the headers can have multiple values (Airly API always return one value headers) + private static string GetHeaderBase(IEnumerable values) => Utils.GetFirstEnumarable(values); + public static string GetHeader(HttpHeaders headers, string key) + { + string values = null; + try + { + values = GetHeaderBase(headers.GetValues(key)); + } + catch (Exception) + { } + return values; + } + } + public static class ParamsValidator { public static double InfinityToDouble(double inifnity)