Skip to content

Commit

Permalink
Package update
Browse files Browse the repository at this point in the history
Updates RestSharp to 106.12.0 (latest)
Removed manual adding the contentType, and added DataFormat.Json to constructor call of RestRequest
  • Loading branch information
PeterHagen committed Aug 15, 2021
1 parent 2aeb6c0 commit 7e20839
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
52 changes: 32 additions & 20 deletions UniFiSharp/DefaultUniFiRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ internal class DefaultUniFiRestClient : RestClient, IUniFiRestClient
private string _username, _password, _code;
private bool _useModernApi;

internal DefaultUniFiRestClient(Uri baseUrl, string username, string password, string code, bool ignoreSslValidation, bool useModernApi) :
internal DefaultUniFiRestClient(Uri baseUrl, string username, string password, string code,
bool ignoreSslValidation, bool useModernApi) :
this(baseUrl, username, password, ignoreSslValidation, useModernApi)
{
_code = code;
}
internal DefaultUniFiRestClient(Uri baseUrl, string username, string password, bool ignoreSslValidation, bool useModernApi) : base(baseUrl)

internal DefaultUniFiRestClient(Uri baseUrl, string username, string password, bool ignoreSslValidation,
bool useModernApi) : base(baseUrl)
{
_username = username;
_password = password;
Expand Down Expand Up @@ -97,27 +100,25 @@ public async Task UnifiFileUpload(string url, string name, string fileName, stri

private async Task UniFiRequest(Method method, string url, object jsonBody = null)
{
var request = new RestRequest(url, method);
request.JsonSerializer.ContentType = $"application/json; charset={Encoding.BodyName}";
var request = new RestRequest(url, method, DataFormat.Json);
if ((method == Method.POST || method == Method.PUT) && jsonBody != null)
request.AddJsonBody(jsonBody);
await ExecuteRequest<object>(request);
}

private async Task<T> UniFiRequest<T>(Method method, string url, object jsonBody = null) where T : new()
{
var request = new RestRequest(url, method);
request.JsonSerializer.ContentType = $"application/json; charset={Encoding.BodyName}";
var request = new RestRequest(url, method, DataFormat.Json);
if ((method == Method.POST || method == Method.PUT) && jsonBody != null)
request.AddJsonBody(jsonBody);
var envelope = await ExecuteRequest<T>(request);
return (envelope.Data == null) ? default(T) : envelope.Data[0];
}

private async Task<IList<T>> UniFiRequestMany<T>(Method method, string url, object jsonBody = null) where T : new()
private async Task<IList<T>> UniFiRequestMany<T>(Method method, string url, object jsonBody = null)
where T : new()
{
var request = new RestRequest(url, method);
request.JsonSerializer.ContentType = $"application/json; charset={Encoding.BodyName}";
var request = new RestRequest(url, method, DataFormat.Json);
if ((method == Method.POST || method == Method.PUT) && jsonBody != null)
request.AddJsonBody(jsonBody);
var envelope = await ExecuteRequest<T>(request);
Expand All @@ -128,8 +129,7 @@ public async Task<JsonLoginResult> Authenticate()
{
if (_useModernApi)
{
var request = new RestRequest("api/auth/login", Method.POST);
request.JsonSerializer.ContentType = $"application/json; charset={Encoding.BodyName}";
var request = new RestRequest("api/auth/login", Method.POST, DataFormat.Json);
request.AddJsonBody(new
{
username = _username,
Expand Down Expand Up @@ -157,7 +157,8 @@ public async Task<JsonLoginResult> Authenticate()
}
}

private async Task<JsonMessageEnvelope<T>> ExecuteRequest<T>(IRestRequest request, bool attemptReauthentication = true) where T : new()
private async Task<JsonMessageEnvelope<T>> ExecuteRequest<T>(IRestRequest request,
bool attemptReauthentication = true) where T : new()
{
if (_useModernApi)
request.Resource = "proxy/network/" + request.Resource;
Expand All @@ -167,8 +168,14 @@ public async Task<JsonLoginResult> Authenticate()

if (this.CookieContainer.GetCookies(this.BaseUrl).Count > 0)
{
try { this.AddDefaultHeader("X-Csrf-Token", this.CookieContainer.GetCookies(this.BaseUrl)["csrf_token"].Value); }
catch { }
try
{
this.AddDefaultHeader("X-Csrf-Token",
this.CookieContainer.GetCookies(this.BaseUrl)["csrf_token"].Value);
}
catch
{
}
}

request.RequestFormat = DataFormat.Json;
Expand Down Expand Up @@ -201,7 +208,8 @@ public async Task<JsonLoginResult> Authenticate()
/// <param name="data"></param>
/// <param name="attemptReauthentication"></param>
/// <returns></returns>
private async Task UnifiMultipartFormRequest(string url, string name, string fileName, string contentType, byte[] data, bool attemptReauthentication = true)
private async Task UnifiMultipartFormRequest(string url, string name, string fileName, string contentType,
byte[] data, bool attemptReauthentication = true)
{
// Note the UniFi controller will return 404 when uploading a file - however the file *is* successfully uploaded.

Expand All @@ -212,8 +220,14 @@ private async Task UnifiMultipartFormRequest(string url, string name, string fil

if (this.CookieContainer.GetCookies(this.BaseUrl).Count > 0)
{
try { this.AddDefaultHeader("X-Csrf-Token", this.CookieContainer.GetCookies(this.BaseUrl)["csrf_token"].Value); }
catch { }
try
{
this.AddDefaultHeader("X-Csrf-Token",
this.CookieContainer.GetCookies(this.BaseUrl)["csrf_token"].Value);
}
catch
{
}
}

var request = new RestRequest(url, Method.POST)
Expand All @@ -238,7 +252,5 @@ private async Task UnifiMultipartFormRequest(string url, string name, string fil
}
}
}


}
}
}
2 changes: 1 addition & 1 deletion UniFiSharp/UniFiApi.NetworkDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async Task NetworkDeviceForget(string macAddress)
{
await RestClient.UniFiPost($"api/s/{Site}/cmd/sitemgr", new
{
cmd = "delete-device",
cmd = "forget-sta", //"delete-device",
mac = macAddress
});
}
Expand Down
3 changes: 2 additions & 1 deletion UniFiSharp/UniFiSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="106.2.2" />
<PackageReference Include="RestSharp" Version="106.12.0" />
<PackageReference Include="RestSharp.Serializers.NewtonsoftJson" Version="106.12.0" />
</ItemGroup>

</Project>

0 comments on commit 7e20839

Please sign in to comment.