Skip to content

Commit

Permalink
Code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
IEvangelist committed Oct 8, 2024
1 parent e478226 commit e1a918a
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 143 deletions.
9 changes: 3 additions & 6 deletions samples/HaveIBeenPwned.BlazorApp/Client/Pages/Counter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
private int currentCount = 0;

private void IncrementCount() => currentCount++;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ public class ErrorModel : PageModel

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
public void OnGet() => RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
59 changes: 27 additions & 32 deletions samples/HaveIBeenPwned.WebApi/Controllers/BreachesController.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

using HaveIBeenPwned.Client;
using HaveIBeenPwned.Client.Abstractions;
using Microsoft.AspNetCore.Mvc;

namespace HaveIBeenPwned.WebApi.Controllers;

[ApiController]
[Route("api/breaches")]
public class BreachesController(IPwnedBreachesClient pwnedBreachesClient) : ControllerBase
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

using HaveIBeenPwned.Client;
using HaveIBeenPwned.Client.Abstractions;
using Microsoft.AspNetCore.Mvc;

namespace HaveIBeenPwned.WebApi.Controllers;

[ApiController]
[Route("api/breaches")]
public class BreachesController(IPwnedBreachesClient pwnedBreachesClient) : ControllerBase
{
[HttpGet, Route("{breachName}")]
public Task<BreachDetails?> GetBreach([FromRoute] string breachName) =>
pwnedBreachesClient.GetBreachAsync(breachName);

[HttpGet, Route("headers/{domain}")]
public Task<BreachHeader[]> GetBreaches([FromRoute] string? domain) =>
pwnedBreachesClient.GetBreachesAsync(domain);

[HttpGet, Route("{account}/breaches")]
public Task<BreachDetails[]> GetBreachesForAccount([FromRoute] string account) =>
pwnedBreachesClient.GetBreachesForAccountAsync(account);

[HttpGet, Route("{account}/headers")]
public Task<BreachHeader[]> GetBreachHeadersForAccount([FromRoute] string account) =>
pwnedBreachesClient.GetBreachHeadersForAccountAsync(account);

[HttpGet, Route("dataclasses")]
public Task<string[]> GetDataClasses() =>
pwnedBreachesClient.GetDataClassesAsync();
}
[HttpGet, Route("{breachName}")]
public Task<BreachDetails?> GetBreach([FromRoute] string breachName) => pwnedBreachesClient.GetBreachAsync(breachName);

[HttpGet, Route("headers/{domain}")]
public Task<BreachHeader[]> GetBreaches([FromRoute] string? domain) => pwnedBreachesClient.GetBreachesAsync(domain);

[HttpGet, Route("{account}/breaches")]
public Task<BreachDetails[]> GetBreachesForAccount([FromRoute] string account) => pwnedBreachesClient.GetBreachesForAccountAsync(account);

[HttpGet, Route("{account}/headers")]
public Task<BreachHeader[]> GetBreachHeadersForAccount([FromRoute] string account) => pwnedBreachesClient.GetBreachHeadersForAccountAsync(account);

[HttpGet, Route("dataclasses")]
public Task<string[]> GetDataClasses() => pwnedBreachesClient.GetDataClassesAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ namespace HaveIBeenPwned.WebApi.Controllers;
public class PasswordsController(IPwnedPasswordsClient pwnedPasswordsClient) : ControllerBase
{
[HttpGet, Route("{plainTextPassword}")]
public Task<PwnedPassword> GetPwnedPassword([FromRoute] string plainTextPassword) =>
pwnedPasswordsClient.GetPwnedPasswordAsync(plainTextPassword);
public Task<PwnedPassword> GetPwnedPassword([FromRoute] string plainTextPassword) => pwnedPasswordsClient.GetPwnedPasswordAsync(plainTextPassword);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ namespace HaveIBeenPwned.WebApi.Controllers;
public class PastesController(IPwnedPastesClient pwnedPastesClient) : ControllerBase
{
[HttpGet, Route("{account}")]
public Task<Pastes[]> GetPaste([FromRoute] string account) =>
pwnedPastesClient.GetPastesAsync(account);
public Task<Pastes[]> GetPaste([FromRoute] string account) => pwnedPastesClient.GetPastesAsync(account);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@ public sealed record class PwnedPassword
/// </summary>
public string? HashedPassword { get; set; }

internal bool IsInvalid() =>
PlainTextPassword is null or { Length: 0 };
internal bool IsInvalid() => PlainTextPassword is null or { Length: 0 };
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

using SubscriptionStatusModel = HaveIBeenPwned.Client.Abstractions.SubscriptionStatus;
using BreachDetailsModel = HaveIBeenPwned.Client.Abstractions.BreachDetails;
using BreachHeaderModel = HaveIBeenPwned.Client.Abstractions.BreachHeader;
using PastesModel = HaveIBeenPwned.Client.Abstractions.Pastes;
using PwnedPasswordModel = HaveIBeenPwned.Client.Abstractions.PwnedPassword;
using SubscriptionStatusModel = HaveIBeenPwned.Client.Abstractions.SubscriptionStatus;

namespace HaveIBeenPwned.Client.Abstractions.Serialization;

Expand All @@ -22,6 +22,4 @@ namespace HaveIBeenPwned.Client.Abstractions.Serialization;
[JsonSerializable(typeof(PastesModel[]))]
[JsonSerializable(typeof(PwnedPasswordModel))]
[JsonSerializable(typeof(string[]))]
public sealed partial class SourceGeneratorContext : JsonSerializerContext
{
}
public sealed partial class SourceGeneratorContext : JsonSerializerContext;
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@ internal static void ConfigureHttpResilience(
{
// Don't log our API key, or auth/cookie headers.
builder.RedactLoggedHeaders(
shouldRedactHeaderValue: static headerName =>
{
return s_redactedHttpHeaders.Any(
predicate: name =>
{
return string.Equals(
name, headerName, StringComparison.OrdinalIgnoreCase);
});
});
shouldRedactHeaderValue: static headerName => s_redactedHttpHeaders.Any(
predicate: name => string.Equals(
name, headerName, StringComparison.OrdinalIgnoreCase)));

if (configureResilienceOptions is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ static IHttpClientBuilder AddPwnedHttpClient(
IServiceCollection services,
string httpClientName,
string baseAddress,
bool isPlainText = false) =>
services.AddHttpClient(
bool isPlainText = false) => services.AddHttpClient(
httpClientName,
(serviceProvider, client) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ static IHttpClientBuilder AddPwnedHttpClient(
IServiceCollection services,
string httpClientName,
string baseAddress,
bool isPlainText = false) =>
services.AddHttpClient(
bool isPlainText = false) => services.AddHttpClient(
httpClientName,
(serviceProvider, client) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public static class SubscriptionStatusExtensions
/// doesn't map to a level.
/// </exception>
public static HibpSubscriptionLevel GetSubscriptionLevel(
this SubscriptionStatus subscriptionStatus)
{
return subscriptionStatus.SubscriptionName switch
this SubscriptionStatus subscriptionStatus) => subscriptionStatus.SubscriptionName switch
{
"Pwned 1" => HibpSubscriptionLevel.One,
"Pwned 2" => HibpSubscriptionLevel.Two,
Expand All @@ -30,5 +28,4 @@ public static HibpSubscriptionLevel GetSubscriptionLevel(
_ => throw new ArgumentException(
$"Unknown subscription level: {subscriptionStatus.SubscriptionName}.")
};
}
}
13 changes: 6 additions & 7 deletions src/HaveIBeenPwned.Client/Factories/InternalHttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ internal static InternalHttpClientFactory Create(string apiKey)

private InternalHttpClientFactory() { }

HttpClient IHttpClientFactory.CreateClient(string name) =>
name switch
{
HttpClientNames.HibpClient => _hibpClient.Value,
HttpClientNames.PasswordsClient => _passwordsClient.Value,
_ => throw new NotImplementedException()
};
HttpClient IHttpClientFactory.CreateClient(string name) => name switch
{
HttpClientNames.HibpClient => _hibpClient.Value,
HttpClientNames.PasswordsClient => _passwordsClient.Value,
_ => throw new NotImplementedException()
};
}
3 changes: 1 addition & 2 deletions src/HaveIBeenPwned.Client/Options/HibpOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ public string UserAgent
/// </summary>
public HibpSubscriptionLevel? SubscriptionLevel { get; set; }

internal void Deconstruct(out string apiKey, out string userAgent, out HibpSubscriptionLevel? subscriptionLevel) =>
(apiKey, userAgent, subscriptionLevel) = (ApiKey, UserAgent, SubscriptionLevel);
internal void Deconstruct(out string apiKey, out string userAgent, out HibpSubscriptionLevel? subscriptionLevel) => (apiKey, userAgent, subscriptionLevel) = (ApiKey, UserAgent, SubscriptionLevel);
}
39 changes: 13 additions & 26 deletions src/HaveIBeenPwned.Client/PwnedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,75 +36,62 @@ public sealed class PwnedClient(string apiKey, ILoggerFactory? loggerFactory = d

/// <inheritdoc/>
Task<SubscriptionStatus?> IPwnedClient.GetSubscriptionStatusAsync(
CancellationToken cancellationToken) =>
_pwnedClient.GetSubscriptionStatusAsync(cancellationToken);
CancellationToken cancellationToken) => _pwnedClient.GetSubscriptionStatusAsync(cancellationToken);

/// <inheritdoc/>
Task<BreachDetails?> IPwnedBreachesClient.GetBreachAsync(
string breachName,
CancellationToken cancellationToken) =>
_pwnedClient.GetBreachAsync(breachName, cancellationToken);
CancellationToken cancellationToken) => _pwnedClient.GetBreachAsync(breachName, cancellationToken);

/// <inheritdoc/>
Task<BreachHeader[]> IPwnedBreachesClient.GetBreachesAsync(
string? domain,
CancellationToken cancellationToken) =>
_pwnedClient.GetBreachesAsync(domain, cancellationToken);
CancellationToken cancellationToken) => _pwnedClient.GetBreachesAsync(domain, cancellationToken);

/// <inheritdoc/>
Task<BreachDetails[]> IPwnedBreachesClient.GetBreachesForAccountAsync(
string account,
CancellationToken cancellationToken) =>
_pwnedClient.GetBreachesForAccountAsync(account, cancellationToken);
CancellationToken cancellationToken) => _pwnedClient.GetBreachesForAccountAsync(account, cancellationToken);

/// <inheritdoc/>
Task<BreachHeader[]> IPwnedBreachesClient.GetBreachHeadersForAccountAsync(
string account,
CancellationToken cancellationToken) =>
_pwnedClient.GetBreachHeadersForAccountAsync(account, cancellationToken);
CancellationToken cancellationToken) => _pwnedClient.GetBreachHeadersForAccountAsync(account, cancellationToken);

/// <inheritdoc/>
Task<string[]> IPwnedBreachesClient.GetDataClassesAsync(CancellationToken cancellationToken) =>
_pwnedClient.GetDataClassesAsync(cancellationToken);
Task<string[]> IPwnedBreachesClient.GetDataClassesAsync(CancellationToken cancellationToken) => _pwnedClient.GetDataClassesAsync(cancellationToken);

/// <inheritdoc/>
Task<Pastes[]> IPwnedPastesClient.GetPastesAsync(
string account,
CancellationToken cancellationToken) =>
_pwnedClient.GetPastesAsync(account, cancellationToken);
CancellationToken cancellationToken) => _pwnedClient.GetPastesAsync(account, cancellationToken);

/// <inheritdoc/>
Task<PwnedPassword> IPwnedPasswordsClient.GetPwnedPasswordAsync(
string plainTextPassword,
CancellationToken cancellationToken) =>
_pwnedClient.GetPwnedPasswordAsync(plainTextPassword, cancellationToken);
CancellationToken cancellationToken) => _pwnedClient.GetPwnedPasswordAsync(plainTextPassword, cancellationToken);

/// <inheritdoc/>
public IAsyncEnumerable<BreachHeader?> GetBreachesAsAsyncEnumerable(
string? domain = null,
CancellationToken cancellationToken = default) =>
_pwnedClient.GetBreachesAsAsyncEnumerable(domain, cancellationToken);
CancellationToken cancellationToken = default) => _pwnedClient.GetBreachesAsAsyncEnumerable(domain, cancellationToken);

/// <inheritdoc/>
public IAsyncEnumerable<BreachDetails?> GetBreachesForAccountAsAsyncEnumerable(
string account,
CancellationToken cancellationToken = default) =>
_pwnedClient.GetBreachesForAccountAsAsyncEnumerable(account, cancellationToken);
CancellationToken cancellationToken = default) => _pwnedClient.GetBreachesForAccountAsAsyncEnumerable(account, cancellationToken);

/// <inheritdoc/>
public IAsyncEnumerable<BreachHeader?> GetBreachHeadersForAccountAsAsyncEnumerable(
string account,
CancellationToken cancellationToken = default) =>
_pwnedClient.GetBreachHeadersForAccountAsAsyncEnumerable(account, cancellationToken);
CancellationToken cancellationToken = default) => _pwnedClient.GetBreachHeadersForAccountAsAsyncEnumerable(account, cancellationToken);

/// <inheritdoc/>
public IAsyncEnumerable<string?> GetDataClassesAsAsyncEnumerable(
CancellationToken cancellationToken = default) =>
_pwnedClient.GetDataClassesAsAsyncEnumerable(cancellationToken);
CancellationToken cancellationToken = default) => _pwnedClient.GetDataClassesAsAsyncEnumerable(cancellationToken);

/// <inheritdoc/>
public IAsyncEnumerable<Pastes?> GetPastesAsAsyncEnumerable(
string account,
CancellationToken cancellationToken) =>
_pwnedClient.GetPastesAsAsyncEnumerable(account, cancellationToken);
CancellationToken cancellationToken) => _pwnedClient.GetPastesAsAsyncEnumerable(account, cancellationToken);
}
5 changes: 2 additions & 3 deletions tests/HaveIBeenPwned.Client.AcceptanceTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.

global using Xunit;

global using HaveIBeenPwned.Client.Extensions;
global using HaveIBeenPwned.Client.Options;
global using HaveIBeenPwned.Client.Options;
global using Xunit;
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,23 @@ namespace HaveIBeenPwned.Client.PollyExtensionsTests.Extensions;
public class PwnedClientServiceCollectionExtensionsTests
{
[Fact]
public void AddPwnedServicesThrowsWhenServiceCollectionIsNull() =>
Assert.Throws<ArgumentNullException>(
public void AddPwnedServicesThrowsWhenServiceCollectionIsNull() => Assert.Throws<ArgumentNullException>(
"services",
() => ((IServiceCollection)null!).AddPwnedServices(_ => { }));

() => ((IServiceCollection)null!).AddPwnedServices(_ => { }));

[Fact]
public void AddPwnedServicesThrowsWhenConfigureOptionsIsNull() =>
Assert.Throws<ArgumentNullException>(
public void AddPwnedServicesThrowsWhenConfigureOptionsIsNull() => Assert.Throws<ArgumentNullException>(
"configureOptions",
() => new ServiceCollection().AddPwnedServices(
(null as Action<HibpOptions>)!));

(null as Action<HibpOptions>)!));

[Fact]
public void AddPwnedServicesThrowsWhenConfigureRetryPolicyIsNull() =>
Assert.Throws<ArgumentNullException>(
public void AddPwnedServicesThrowsWhenConfigureRetryPolicyIsNull() => Assert.Throws<ArgumentNullException>(
"configureResilienceOptions",
() => new ServiceCollection().AddPwnedServices(
options => { },
(null as Action<HttpStandardResilienceOptions>)!));

(null as Action<HttpStandardResilienceOptions>)!));

[Fact]
public void AddPwnedServicesAddsDefaultImplementationsWhenValid()
{
Expand Down
24 changes: 12 additions & 12 deletions tests/HaveIBeenPwned.ClientTests/DefaultPwnedClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,28 +193,28 @@ class IntegrationHttpClientFactory : IHttpClientFactory
HttpClient? _hibpHttpClient;
HttpClient? _pwnedPasswordsClient;

HttpClient IHttpClientFactory.CreateClient(string name) => name switch
{
HttpClientNames.HibpClient =>
_hibpHttpClient ??= new HttpClient { BaseAddress = new(HttpClientUrls.HibpApiUrl) },

HttpClientNames.PasswordsClient =>
_pwnedPasswordsClient ??= new HttpClient { BaseAddress = new(HttpClientUrls.PasswordsApiUrl) },

_ => throw null!
};
HttpClient IHttpClientFactory.CreateClient(string name) => name switch
{
HttpClientNames.HibpClient =>
_hibpHttpClient ??= new HttpClient { BaseAddress = new(HttpClientUrls.HibpApiUrl) },

HttpClientNames.PasswordsClient =>
_pwnedPasswordsClient ??= new HttpClient { BaseAddress = new(HttpClientUrls.PasswordsApiUrl) },

_ => throw null!
};
}

class NullHttpClientFactory : IHttpClientFactory
{
public static readonly IHttpClientFactory Instance = new NullHttpClientFactory();

HttpClient IHttpClientFactory.CreateClient(string name) => default!;
HttpClient IHttpClientFactory.CreateClient(string name) => default!;
}

class ThrowingHttpClientFactory : IHttpClientFactory
{
public static readonly IHttpClientFactory Instance = new NullHttpClientFactory();

HttpClient IHttpClientFactory.CreateClient(string name) => throw default!;
HttpClient IHttpClientFactory.CreateClient(string name) => throw default!;
}
Loading

0 comments on commit e1a918a

Please sign in to comment.