-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐ a senseless bit of refactoring love (#15)
โ "user secrets" nuget dependency ๐ HttpClient instantiation moved to its own class in a lazy way ๐งน code cleanup
- Loading branch information
Showing
4 changed files
with
90 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters