Skip to content

Commit

Permalink
Refactoring the code
Browse files Browse the repository at this point in the history
Unit tests are using the new ToPostReequestAsync from now on
  • Loading branch information
geloczi committed Jul 31, 2022
1 parent 32bbba8 commit df3074b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion SynologyDotNet.Core.Test/RequestBuilder_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void ToPostRequest()
{


var r = new RequestBuilder("AudioStation/album.cgi", "SYNO.AudioStation.Album", 1, "list").ToPostRequest();
var r = new RequestBuilder("AudioStation/album.cgi", "SYNO.AudioStation.Album", 1, "list").ToPostRequestAsync().Result;
Assert.AreEqual("AudioStation/album.cgi", r.RequestUri.OriginalString);
Assert.AreEqual(HttpMethod.Post, r.Method);
if (r.Content is FormUrlEncodedContent form)
Expand Down
2 changes: 1 addition & 1 deletion SynologyDotNet.Core/Helpers/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public RequestBuilder SetExplicitQueryStringParams(params (string, object)[] par
/// Converts to HTTP POST request.
/// </summary>
/// <returns></returns>
public async Task<HttpRequestMessage> ToPostRequest()
public async Task<HttpRequestMessage> ToPostRequestAsync()
{
// Only ExplicitQueryStringParams are going to the query string
string url = GetBaseUrl();
Expand Down
10 changes: 5 additions & 5 deletions SynologyDotNet.Core/SynoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public async Task<SynoSession> LoginAsync(string username, string password, Canc
req["enable_syno_token"] = "yes";
req.SetExplicitQueryStringParam("enable_syno_token", "yes"); // This is necessary to get a valid synotoken, this has to be present in the query string as well (even if it's a POST!)

var response = await _httpClient.SendAsync(await req.ToPostRequest(), cancellationToken).ConfigureAwait(false);
var response = await _httpClient.SendAsync(await req.ToPostRequestAsync(), cancellationToken).ConfigureAwait(false);
ThrowIfNotSuccessfulHttpResponse(response);
var json = Encoding.UTF8.GetString(await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
var loginResult = JsonConvert.DeserializeObject<LoginResult>(json);
Expand Down Expand Up @@ -491,7 +491,7 @@ public async Task<string> QueryStringAsync(RequestBuilder req)
/// <returns></returns>
public async Task<string> QueryStringAsync(RequestBuilder req, CancellationToken cancellationToken)
{
var response = await _httpClient.SendAsync(await req.ToPostRequest(), cancellationToken).ConfigureAwait(false);
var response = await _httpClient.SendAsync(await req.ToPostRequestAsync(), cancellationToken).ConfigureAwait(false);
ThrowIfNotSuccessfulHttpResponse(response);
var bytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); // Do not use ReadAsStringAsync here
var text = Encoding.UTF8.GetString(bytes);
Expand All @@ -516,7 +516,7 @@ public async Task<T> QueryObjectAsync<T>(RequestBuilder req)
/// <returns></returns>
public async Task<T> QueryObjectAsync<T>(RequestBuilder req, CancellationToken cancellationToken)
{
var response = await _httpClient.SendAsync(await req.ToPostRequest(), cancellationToken).ConfigureAwait(false);
var response = await _httpClient.SendAsync(await req.ToPostRequestAsync(), cancellationToken).ConfigureAwait(false);
ThrowIfNotSuccessfulHttpResponse(response);
var bytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false); // Do not use ReadAsStringAsync here
var text = Encoding.UTF8.GetString(bytes);
Expand All @@ -542,7 +542,7 @@ public async Task<ByteArrayData> QueryByteArrayAsync(RequestBuilder req)
/// <exception cref="SynologyDotNet.Core.Exceptions.SynoHttpException"></exception>
public async Task<ByteArrayData> QueryByteArrayAsync(RequestBuilder req, CancellationToken cancellationToken)
{
var response = await _httpClient.SendAsync(await req.ToPostRequest(), cancellationToken).ConfigureAwait(false);
var response = await _httpClient.SendAsync(await req.ToPostRequestAsync(), cancellationToken).ConfigureAwait(false);
ThrowIfNotSuccessfulHttpResponse(response);
var bytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
return new ByteArrayData()
Expand Down Expand Up @@ -576,7 +576,7 @@ public async Task QueryStreamAsync(RequestBuilder req, Action<StreamResult> read
/// <exception cref="NotSupportedException"></exception>
public async Task QueryStreamAsync(RequestBuilder req, Action<StreamResult> readStreamAction, CancellationToken cancellationToken)
{
var response = await _httpClient.SendAsync(await req.ToPostRequest(), HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
var response = await _httpClient.SendAsync(await req.ToPostRequestAsync(), HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
ThrowIfNotSuccessfulHttpResponse(response);
if (response.Content is null)
throw new NullReferenceException("No content.");
Expand Down

0 comments on commit df3074b

Please sign in to comment.