Skip to content

Commit

Permalink
Splited the Utils to some more categorized classes. Fromatted and bea…
Browse files Browse the repository at this point in the history
…utified the code.
  • Loading branch information
Mondonno committed Jul 2, 2021
1 parent 35d69ea commit e3fa577
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 127 deletions.
6 changes: 3 additions & 3 deletions Airly.Net/Airly.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<AssemblyName>AirlyNet</AssemblyName>
<PackOnBuild>true</PackOnBuild>
<PackageId>AirlyNet</PackageId>
<PackageVersion>0.1.2</PackageVersion>
<PackageVersion>0.1.8</PackageVersion>
<Authors>Mondonno</Authors>
<NeutralLanguage>en-US</NeutralLanguage>
<Owners>Mondonno</Owners>
<PackageProjectUrl>https://github.com/Mondonno/Airly.Net</PackageProjectUrl>
<PackageReleaseNotes>The pre-release of our wrapper</PackageReleaseNotes>
<PackageTags>airly airly.net c# csharp</PackageTags>
<PackageReleaseNotes>All changelog informations are avaible on our github repository</PackageReleaseNotes>
<PackageTags>airly airly.net c# csharp api client api-client</PackageTags>
<Title>Airly.Net</Title>
<Description>Wrapper for the Airly API written 100% in C#</Description>
<PackageIconUrl>https://cdn.airly.org/assets/brand/icon/primary/airly-32.png</PackageIconUrl>
Expand Down
2 changes: 1 addition & 1 deletion Airly.Net/Airly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using AirlyNet.Interactions;
using AirlyNet.Rest;
using AirlyNet.Utilities;
using AirlyNet.Models;
using AirlyNet.Models;

namespace AirlyNet
{
Expand Down
16 changes: 7 additions & 9 deletions Airly.Net/common/handling/Handlings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;

Expand All @@ -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)
Expand All @@ -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();
Expand Down
8 changes: 3 additions & 5 deletions Airly.Net/common/handling/RateLimitInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ 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; }
public int? Diffrence { get; set; }

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;
}

Expand Down
3 changes: 1 addition & 2 deletions Airly.Net/rest/RestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ public async Task<T> Request<T>(string end, string method = null, RequestOptions
}
public async Task<T> Request<T>(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<T>(end, method, requestOptions);
}
Expand Down
3 changes: 1 addition & 2 deletions Airly.Net/rest/api/RequestModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> DefaultHeaders = new Dictionary<string, string>()
{
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions Airly.Net/rest/queue/RequestQueuer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ private JToken ConvertJsonString(string json)

private async Task<RestResponse> Make(RestRequest request)
{
Utils utils = new();
RawRestResponse res;

try { res = await request.InvokeRequest(handle: true); }
Expand All @@ -71,7 +70,7 @@ private async Task<RestResponse> 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.
Expand All @@ -83,7 +82,7 @@ private async Task<RestResponse> 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()
{
Expand Down
Loading

0 comments on commit e3fa577

Please sign in to comment.