Skip to content

Commit

Permalink
๐Ÿ›  a senseless bit of refactoring love (#15)
Browse files Browse the repository at this point in the history
โž– "user secrets" nuget dependency
๐Ÿ›  HttpClient instantiation moved to its own class in a lazy way
๐Ÿงน code cleanup
  • Loading branch information
nop77svk authored Feb 16, 2024
1 parent 5444572 commit 8eb1bab
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 28 deletions.
4 changes: 2 additions & 2 deletions jira-worklogger.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "jwl.infra", "jwl.infra\jwl.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "jwl.core", "jwl.core\jwl.core.csproj", "{DA3C8E30-0358-4103-97E0-29740E648A27}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "jwl.wadl", "jwl.wadl\jwl.wadl.csproj", "{3A874F02-B059-4DEE-A1EF-2F13BC981BBA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "jwl.wadl", "jwl.wadl\jwl.wadl.csproj", "{3A874F02-B059-4DEE-A1EF-2F13BC981BBA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "jwl.test", "jwl.test\jwl.test.csproj", "{35704A9C-16A2-4F64-9042-C59421FCDE9B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "jwl.test", "jwl.test\jwl.test.csproj", "{35704A9C-16A2-4F64-9042-C59421FCDE9B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
76 changes: 76 additions & 0 deletions jwl.core/ConfigDrivenHttpClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
๏ปฟnamespace jwl.core;
using System;

internal class ConfigDrivenHttpClientFactory
: IDisposable
{
public HttpClientHandler HttpClientHandler => _lazyHttpClientHandler.Value;
public HttpClient HttpClient => _lazyHttpClient.Value;

private readonly Lazy<HttpClientHandler> _lazyHttpClientHandler;
private readonly Lazy<HttpClient> _lazyHttpClient;
private bool _isDisposed;

public ConfigDrivenHttpClientFactory(AppConfig config)
{
_lazyHttpClientHandler = new Lazy<HttpClientHandler>(() => InstantiateHttpClientHandler(config));
_lazyHttpClient = new Lazy<HttpClient>(() => InstantiateHttpClient(config));
}

// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~ConfigDrivenHttpClientFactory()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_isDisposed)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
}

if (_lazyHttpClient.IsValueCreated)
_lazyHttpClient.Value.Dispose();

if (_lazyHttpClientHandler.IsValueCreated)
_lazyHttpClientHandler.Value.Dispose();

// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
_isDisposed = true;
}
}

private HttpClientHandler InstantiateHttpClientHandler(AppConfig config)
{
HttpClientHandler result = new HttpClientHandler()
{
UseProxy = config.JiraServer?.UseProxy ?? false,
UseDefaultCredentials = false,
MaxConnectionsPerServer = config.JiraServer?.MaxConnectionsPerServer ?? AppConfigFactory.DefaultMaxConnectionsPerServer
};

if (config.JiraServer?.SkipSslCertificateCheck ?? false)
HttpClientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;

return result;
}

private HttpClient InstantiateHttpClient(AppConfig config)
{
return new HttpClient(HttpClientHandler)
{
BaseAddress = new Uri(config.JiraServer?.BaseUrl ?? string.Empty)
};
}
}
37 changes: 12 additions & 25 deletions jwl.core/JwlCoreProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,28 @@ public class JwlCoreProcess : IDisposable
{
public const int TotalProcessSteps = 7;

public ICoreProcessFeedback? Feedback { get; init; }
public ICoreProcessInteraction _interaction { get; }
public static Version? ExeVersion => AssemblyVersioning.GetExeVersion();
public static Version? CoreVersion => AssemblyVersioning.GetCoreVersion(typeof(JwlCoreProcess));

public Version? ExeVersion => AssemblyVersioning.GetExeVersion();
public Version? CoreVersion => AssemblyVersioning.GetCoreVersion(typeof(JwlCoreProcess));
public ICoreProcessFeedback? Feedback { get; init; }
public ICoreProcessInteraction Interaction { get; }

private bool _isDisposed;

private AppConfig _config;
private HttpClientHandler _httpClientHandler;
private HttpClient _httpClient;
private IJiraClient _jiraClient;
private HttpClient _httpClient => _httpClientFactory.HttpClient;
private readonly ConfigDrivenHttpClientFactory _httpClientFactory;

public JwlCoreProcess(AppConfig config, ICoreProcessInteraction interaction)
{
_config = config;
_interaction = interaction;

_httpClientHandler = new HttpClientHandler()
{
UseProxy = _config.JiraServer?.UseProxy ?? false,
UseDefaultCredentials = false,
MaxConnectionsPerServer = _config.JiraServer?.MaxConnectionsPerServer ?? AppConfigFactory.DefaultMaxConnectionsPerServer
};

if (_config.JiraServer?.SkipSslCertificateCheck ?? false)
_httpClientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
Interaction = interaction;

_httpClient = new HttpClient(_httpClientHandler)
{
BaseAddress = new Uri(_config.JiraServer?.BaseUrl ?? string.Empty)
};
_httpClientFactory = new ConfigDrivenHttpClientFactory(config);

string userName = _config.User?.Name ?? throw new ArgumentNullException($"{nameof(_config)}.{nameof(_config.User)}.{nameof(_config.User.Name)})");
string userName = _config.User?.Name
?? throw new ArgumentNullException($"{nameof(_config)}.{nameof(_config.User)}.{nameof(_config.User.Name)})");
_jiraClient = ServerApiFactory.CreateApi(_httpClient, userName, _config.JiraServer);

// 2do! optional trace-logging the HTTP requests
Expand All @@ -62,8 +50,8 @@ public async Task PreProcess()

if (string.IsNullOrEmpty(jiraUserName) || string.IsNullOrEmpty(jiraUserPassword))
{
if (_interaction != null)
(jiraUserName, jiraUserPassword) = _interaction.AskForCredentials(jiraUserName);
if (Interaction != null)
(jiraUserName, jiraUserPassword) = Interaction.AskForCredentials(jiraUserName);
}

if (string.IsNullOrEmpty(jiraUserName) || string.IsNullOrEmpty(jiraUserPassword))
Expand Down Expand Up @@ -124,7 +112,6 @@ protected virtual void Dispose(bool disposing)
// note: free unmanaged resources (unmanaged objects) and override finalizer
// note: set large fields to null
_httpClient?.Dispose();
_httpClientHandler?.Dispose();

_isDisposed = true;
}
Expand Down
1 change: 0 additions & 1 deletion jwl.test/jwl.test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
Expand Down

0 comments on commit 8eb1bab

Please sign in to comment.