Skip to content

Commit

Permalink
Fix warnings (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored Sep 26, 2023
1 parent 89fd3e5 commit d1c5b0c
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 187 deletions.
7 changes: 6 additions & 1 deletion src/Passwordless/IPasswordlessClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Passwordless.Models;
using Passwordless.Models;

namespace Passwordless;

Expand Down Expand Up @@ -43,6 +43,11 @@ public interface IPasswordlessClient
/// <exception cref="PasswordlessApiException">An exception containing details abaout the reason for failure.</exception>
Task DeleteCredentialAsync(byte[] id, CancellationToken cancellationToken = default);

/// <summary>
/// Gets the users count.
/// </summary>
Task<UsersCount> GetUsersCountAsync(CancellationToken cancellationToken = default);

/// <summary>
/// List all the <see cref="AliasPointer" /> for a given user.
/// </summary>
Expand Down
4 changes: 1 addition & 3 deletions src/Passwordless/Models/RegisterOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.Json.Serialization;

namespace Passwordless;
namespace Passwordless;

/// <summary>
///
Expand Down
4 changes: 1 addition & 3 deletions src/Passwordless/Models/RegisterTokenResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.Json.Serialization;

namespace Passwordless.Models;
namespace Passwordless.Models;

public class RegisterTokenResponse
{
Expand Down
5 changes: 0 additions & 5 deletions src/Passwordless/Passwordless.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
<DebugType>embedded</DebugType>
</PropertyGroup>

<!-- .NET 6 first introduced these types, for all others we include our own pollyfill so that it builds -->
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp' OR $([MSBuild]::VersionLessThan($(TargetFrameworkVersion), '6.0'))">
<DefineConstants>$(DefineConstants);INCLUDE_DYNAMICALLY_ACCESSED_MEMBERS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
Expand Down
2 changes: 0 additions & 2 deletions src/Passwordless/PasswordlessApiException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Diagnostics;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand Down
83 changes: 49 additions & 34 deletions src/Passwordless/PasswordlessClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

namespace Passwordless;

/// <summary>
/// TODO: FILL IN
/// </summary>
/// <inheritdoc cref="IPasswordlessClient" />
[DebuggerDisplay("{DebuggerToString(),nq}")]
public class PasswordlessClient : IPasswordlessClient, IDisposable
{
private readonly bool _needsDisposing;
private readonly HttpClient _client;
private readonly bool _disposeClient;

/// <summary>
/// Initializes an instance of <see cref="PasswordlessClient" />.
/// </summary>
public static PasswordlessClient Create(PasswordlessOptions options, IHttpClientFactory factory)
{
var client = factory.CreateClient();
Expand All @@ -24,123 +25,136 @@ public static PasswordlessClient Create(PasswordlessOptions options, IHttpClient
return new PasswordlessClient(client);
}

/// <summary>
/// Initializes an instance of <see cref="PasswordlessClient" />.
/// </summary>
public PasswordlessClient(PasswordlessOptions passwordlessOptions)
{
_needsDisposing = true;
_client = new HttpClient
{
BaseAddress = new Uri(passwordlessOptions.ApiUrl),
};
_client.DefaultRequestHeaders.Add("ApiSecret", passwordlessOptions.ApiSecret);
_disposeClient = true;
}

/// <summary>
/// Initializes an instance of <see cref="PasswordlessClient" />.
/// </summary>
public PasswordlessClient(HttpClient client)
{
_needsDisposing = false;
_client = client;
_disposeClient = false;
}

/// <inheritdoc/>
/// <inheritdoc />
public async Task SetAliasAsync(SetAliasRequest request, CancellationToken cancellationToken)
{
var res = await _client.PostAsJsonAsync("alias",
using var response = await _client.PostAsJsonAsync("alias",
request,
PasswordlessSerializerContext.Default.SetAliasRequest,
cancellationToken);
res.EnsureSuccessStatusCode();

response.EnsureSuccessStatusCode();
}

/// <inheritdoc/>
/// <inheritdoc />
public async Task<RegisterTokenResponse> CreateRegisterTokenAsync(RegisterOptions registerOptions, CancellationToken cancellationToken = default)
{
var res = await _client.PostAsJsonAsync("register/token",
using var response = await _client.PostAsJsonAsync("register/token",
registerOptions,
PasswordlessSerializerContext.Default.RegisterOptions,
cancellationToken);
res.EnsureSuccessStatusCode();
return (await res.Content.ReadFromJsonAsync<RegisterTokenResponse>(

response.EnsureSuccessStatusCode();

return (await response.Content.ReadFromJsonAsync(
PasswordlessSerializerContext.Default.RegisterTokenResponse,
cancellationToken))!;
}

/// <inheritdoc/>
/// <inheritdoc />
public async Task<VerifiedUser?> VerifyTokenAsync(string verifyToken, CancellationToken cancellationToken = default)
{
var request = new HttpRequestMessage(HttpMethod.Post, "signin/verify")
{
// TODO: No JsonTypeInfo overload yet?
Content = JsonContent.Create(new VerifyTokenRequest(verifyToken)),
};
using var request = new HttpRequestMessage(HttpMethod.Post, "signin/verify");

// TODO: No JsonTypeInfo overload yet?
request.Content = JsonContent.Create(new VerifyTokenRequest(verifyToken));

// We just want to return null if there is a problem.
request.SkipErrorHandling();
var response = await _client.SendAsync(request, cancellationToken);
using var response = await _client.SendAsync(request, cancellationToken);

if (response.IsSuccessStatusCode)
{
var res = await response.Content.ReadFromJsonAsync(
PasswordlessSerializerContext.Default.VerifiedUser,
cancellationToken);

return res;
}

return null;
}

/// <inheritdoc/>
/// <inheritdoc />
public async Task DeleteUserAsync(string userId, CancellationToken cancellationToken = default)
{
await _client.PostAsJsonAsync("users/delete",
using var response = await _client.PostAsJsonAsync("users/delete",
new DeleteUserRequest(userId),
PasswordlessSerializerContext.Default.DeleteUserRequest,
cancellationToken);
}

/// <inheritdoc/>
/// <inheritdoc />
public async Task<IReadOnlyList<PasswordlessUserSummary>> ListUsersAsync(CancellationToken cancellationToken = default)
{
var response = await _client.GetFromJsonAsync(
"users/list",
PasswordlessSerializerContext.Default.ListResponsePasswordlessUserSummary,
cancellationToken);

return response!.Values;
}

/// <inheritdoc/>
/// <inheritdoc />
public async Task<IReadOnlyList<AliasPointer>> ListAliasesAsync(string userId, CancellationToken cancellationToken = default)
{
var response = await _client.GetFromJsonAsync(
$"alias/list?userid={userId}",
PasswordlessSerializerContext.Default.ListResponseAliasPointer,
cancellationToken);

return response!.Values;
}

/// <inheritdoc/>
/// <inheritdoc />
public async Task<IReadOnlyList<Credential>> ListCredentialsAsync(string userId, CancellationToken cancellationToken = default)
{
var response = await _client.GetFromJsonAsync(
$"credentials/list?userid={userId}",
PasswordlessSerializerContext.Default.ListResponseCredential,
cancellationToken);

return response!.Values;
}

/// <inheritdoc/>
/// <inheritdoc />
public async Task DeleteCredentialAsync(string id, CancellationToken cancellationToken = default)
{
await _client.PostAsJsonAsync("credentials/delete",
using var response = await _client.PostAsJsonAsync("credentials/delete",
new DeleteCredentialRequest(id),
PasswordlessSerializerContext.Default.DeleteCredentialRequest,
cancellationToken);
}

/// <inheritdoc/>
/// <inheritdoc />
public async Task DeleteCredentialAsync(byte[] id, CancellationToken cancellationToken = default)
{
await DeleteCredentialAsync(Base64Url.Encode(id), cancellationToken);
}

/// <inheritdoc />
public async Task<UsersCount> GetUsersCountAsync(CancellationToken cancellationToken = default)
{
return (await _client.GetFromJsonAsync(
Expand Down Expand Up @@ -174,18 +188,19 @@ private string DebuggerToString()
return sb.ToString();
}

/// <inheritdoc />
public void Dispose()
{
if (_needsDisposing)
{
Dispose(true);
GC.SuppressFinalize(this);
}
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases unmanaged resources.
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (disposing)
if (disposing && _disposeClient)
{
_client.Dispose();
}
Expand Down
1 change: 0 additions & 1 deletion src/Passwordless/PasswordlessDelegatingHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net.Http;
using System.Net.Http.Json;
using Passwordless.Helpers;

Expand Down
2 changes: 0 additions & 2 deletions src/Passwordless/PasswordlessHttpRequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Net.Http;

namespace Passwordless;

internal static class PasswordlessHttpRequestExtensions
Expand Down
Loading

0 comments on commit d1c5b0c

Please sign in to comment.