Skip to content

Commit

Permalink
#1833 reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Nov 18, 2024
1 parent b0c3b48 commit c8983cd
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 195 deletions.
18 changes: 10 additions & 8 deletions starsky/starsky.foundation.http/Interfaces/IHttpClientHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace starsky.foundation.http.Interfaces
namespace starsky.foundation.http.Interfaces;

public interface IHttpClientHelper
{
public interface IHttpClientHelper
{
Task<bool> Download(string sourceHttpUrl, string fullLocalPath, int retryAfterInSeconds = 15);
Task<KeyValuePair<bool, string>> ReadString(string sourceHttpUrl);
Task<KeyValuePair<bool, string>> PostString(string sourceHttpUrl,
HttpContent? httpContent, bool verbose = true);
}
Task<bool> Download(string sourceHttpUrl, string fullLocalPath, int retryAfterInSeconds = 15);
Task<KeyValuePair<bool, string>> ReadString(string sourceHttpUrl);
Task<KeyValuePair<bool, string>> ReadString(Uri sourceHttpUrl);

Task<KeyValuePair<bool, string>> PostString(string sourceHttpUrl,
HttpContent? httpContent, bool verbose = true);
}
284 changes: 144 additions & 140 deletions starsky/starsky.foundation.http/Services/HttpClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,180 +13,184 @@
using starsky.foundation.storage.Interfaces;
using starsky.foundation.storage.Storage;

namespace starsky.foundation.http.Services
namespace starsky.foundation.http.Services;

[Service(typeof(IHttpClientHelper), InjectionLifetime = InjectionLifetime.Singleton)]
public sealed class HttpClientHelper : IHttpClientHelper
{
[Service(typeof(IHttpClientHelper), InjectionLifetime = InjectionLifetime.Singleton)]
public sealed class HttpClientHelper : IHttpClientHelper
/// <summary>
/// These domains are only allowed domains to download from (and https only)
/// </summary>
private readonly List<string> _allowedDomains =
[
"dl.dropboxusercontent.com",
"qdraw.nl", // < used by test
"media.qdraw.nl", // < used by demo
"locker.ifttt.com",
"download.geonames.org",
"exiftool.org",
"api.github.com"
];

/// <summary>
/// Http Provider
/// </summary>
private readonly IHttpProvider _httpProvider;

private readonly IWebLogger _logger;
private readonly IStorage? _storage;

/// <summary>
/// Set Http Provider
/// </summary>
/// <param name="httpProvider">IHttpProvider</param>
/// <param name="serviceScopeFactory">ScopeFactory contains a IStorageSelector</param>
/// <param name="logger">WebLogger</param>
public HttpClientHelper(IHttpProvider httpProvider,
IServiceScopeFactory? serviceScopeFactory, IWebLogger logger)
{
private readonly IStorage? _storage;

/// <summary>
/// Set Http Provider
/// </summary>
/// <param name="httpProvider">IHttpProvider</param>
/// <param name="serviceScopeFactory">ScopeFactory contains a IStorageSelector</param>
/// <param name="logger">WebLogger</param>
public HttpClientHelper(IHttpProvider httpProvider,
IServiceScopeFactory? serviceScopeFactory, IWebLogger logger)
_httpProvider = httpProvider;
_logger = logger;
if ( serviceScopeFactory == null )
{
_httpProvider = httpProvider;
_logger = logger;
if ( serviceScopeFactory == null )
{
return;
}

using ( var scope = serviceScopeFactory.CreateScope() )
{
// ISelectorStorage is a scoped service
var selectorStorage = scope.ServiceProvider.GetRequiredService<ISelectorStorage>();
_storage = selectorStorage.Get(SelectorStorage.StorageServices.HostFilesystem);
}
return;
}

/// <summary>
/// Http Provider
/// </summary>
private readonly IHttpProvider _httpProvider;

private readonly IWebLogger _logger;

/// <summary>
/// These domains are only allowed domains to download from (and https only)
/// </summary>
private readonly List<string> _allowedDomains =
[
"dl.dropboxusercontent.com",
"qdraw.nl", // < used by test
"media.qdraw.nl", // < used by demo
"locker.ifttt.com",
"download.geonames.org",
"exiftool.org",
"api.github.com"
];

public async Task<KeyValuePair<bool, string>> ReadString(string sourceHttpUrl)
using ( var scope = serviceScopeFactory.CreateScope() )
{
var sourceUri = new Uri(sourceHttpUrl);
// ISelectorStorage is a scoped service
var selectorStorage = scope.ServiceProvider.GetRequiredService<ISelectorStorage>();
_storage = selectorStorage.Get(SelectorStorage.StorageServices.HostFilesystem);
}
}

_logger.LogInformation("[ReadString] HttpClientHelper > "
+ sourceUri.Host + " ~ " + sourceHttpUrl);
public async Task<KeyValuePair<bool, string>> ReadString(string sourceHttpUrl)
{
var sourceUri = new Uri(sourceHttpUrl);
return await ReadString(sourceUri);
}

// allow whitelist and https only
if ( !_allowedDomains.Contains(sourceUri.Host) || sourceUri.Scheme != "https" )
{
return
public async Task<KeyValuePair<bool, string>> ReadString(Uri sourceHttpUrl)
{
_logger.LogInformation("[ReadString] HttpClientHelper > "
+ sourceHttpUrl);
// allow whitelist and https only
if ( !_allowedDomains.Contains(sourceHttpUrl.Host) || sourceHttpUrl.Scheme != "https" )
{
return
new KeyValuePair<bool, string>(false, string.Empty);
}
}

try
{
using ( HttpResponseMessage response = await _httpProvider.GetAsync(sourceHttpUrl) )
using ( Stream streamToReadFrom = await response.Content.ReadAsStreamAsync() )
{
var reader = new StreamReader(streamToReadFrom, Encoding.UTF8);
var result = await reader.ReadToEndAsync();
return new KeyValuePair<bool, string>(response.StatusCode == HttpStatusCode.OK, result);
}
}
catch ( HttpRequestException exception )
try
{
using ( var response = await _httpProvider.GetAsync(sourceHttpUrl.ToString()) )
using ( var streamToReadFrom = await response.Content.ReadAsStreamAsync() )
{
return new KeyValuePair<bool, string>(false, exception.Message);
var reader = new StreamReader(streamToReadFrom, Encoding.UTF8);
var result = await reader.ReadToEndAsync();
return new KeyValuePair<bool, string>(response.StatusCode == HttpStatusCode.OK,
result);
}
}

public async Task<KeyValuePair<bool, string>> PostString(string sourceHttpUrl,
HttpContent? httpContent, bool verbose = true)
catch ( HttpRequestException exception )
{
var sourceUri = new Uri(sourceHttpUrl);
return new KeyValuePair<bool, string>(false, exception.Message);
}
}

if ( verbose )
{
_logger.LogInformation("[PostString] HttpClientHelper > "
+ sourceUri.Host + " ~ " + sourceHttpUrl);
}
public async Task<KeyValuePair<bool, string>> PostString(string sourceHttpUrl,
HttpContent? httpContent, bool verbose = true)
{
var sourceUri = new Uri(sourceHttpUrl);

// // allow whitelist and https only
if ( !_allowedDomains.Contains(sourceUri.Host) || sourceUri.Scheme != "https" )
{
return
if ( verbose )
{
_logger.LogInformation("[PostString] HttpClientHelper > "
+ sourceUri.Host + " ~ " + sourceHttpUrl);
}

// // allow whitelist and https only
if ( !_allowedDomains.Contains(sourceUri.Host) || sourceUri.Scheme != "https" )
{
return
new KeyValuePair<bool, string>(false, string.Empty);
}
}

try
{
using ( HttpResponseMessage response = await _httpProvider.PostAsync(sourceHttpUrl, httpContent) )
using ( Stream streamToReadFrom = await response.Content.ReadAsStreamAsync() )
{
var reader = new StreamReader(streamToReadFrom, Encoding.UTF8);
var result = await reader.ReadToEndAsync();
return new KeyValuePair<bool, string>(response.StatusCode == HttpStatusCode.OK, result);
}
}
catch ( HttpRequestException exception )
try
{
using ( var response = await _httpProvider.PostAsync(sourceHttpUrl, httpContent) )
using ( var streamToReadFrom = await response.Content.ReadAsStreamAsync() )
{
return new KeyValuePair<bool, string>(false, exception.Message);
var reader = new StreamReader(streamToReadFrom, Encoding.UTF8);
var result = await reader.ReadToEndAsync();
return new KeyValuePair<bool, string>(response.StatusCode == HttpStatusCode.OK,
result);
}
}
catch ( HttpRequestException exception )
{
return new KeyValuePair<bool, string>(false, exception.Message);
}
}

/// <summary>
/// Downloads the specified source HTTPS URL.
/// </summary>
/// <param name="sourceHttpUrl">The source HTTPS URL.</param>
/// <param name="fullLocalPath">The full local path.</param>
/// <param name="retryAfterInSeconds">Retry after number of seconds</param>
/// <returns></returns>
public async Task<bool> Download(string sourceHttpUrl, string fullLocalPath, int retryAfterInSeconds = 15)
/// <summary>
/// Downloads the specified source HTTPS URL.
/// </summary>
/// <param name="sourceHttpUrl">The source HTTPS URL.</param>
/// <param name="fullLocalPath">The full local path.</param>
/// <param name="retryAfterInSeconds">Retry after number of seconds</param>
/// <returns></returns>
public async Task<bool> Download(string sourceHttpUrl, string fullLocalPath,
int retryAfterInSeconds = 15)
{
if ( _storage == null )
{
if ( _storage == null )
{
throw new EndOfStreamException("is null " + nameof(_storage));
}
throw new EndOfStreamException("is null " + nameof(_storage));
}

var sourceUri = new Uri(sourceHttpUrl);

Uri sourceUri = new Uri(sourceHttpUrl);
_logger.LogInformation("[Download] HttpClientHelper > "
+ sourceUri.Host + " ~ " + sourceHttpUrl);

// allow whitelist and https only
if ( !_allowedDomains.Contains(sourceUri.Host) ||
sourceUri.Scheme != "https" )
{
_logger.LogInformation("[Download] HttpClientHelper > "
+ sourceUri.Host + " ~ " + sourceHttpUrl);
+ "skip: domain not whitelisted " + " ~ " + sourceHttpUrl);
return false;
}

// allow whitelist and https only
if ( !_allowedDomains.Contains(sourceUri.Host) ||
sourceUri.Scheme != "https" )
async Task<bool> DownloadAsync()
{
using var response = await _httpProvider.GetAsync(sourceHttpUrl);
await using var streamToReadFrom = await response.Content.ReadAsStreamAsync();
if ( response.StatusCode != HttpStatusCode.OK )
{
_logger.LogInformation("[Download] HttpClientHelper > "
+ "skip: domain not whitelisted " + " ~ " + sourceHttpUrl);
_logger.LogInformation("[Download] HttpClientHelper > " +
response.StatusCode + " ~ " + sourceHttpUrl);
return false;
}

async Task<bool> DownloadAsync()
{
using var response = await _httpProvider.GetAsync(sourceHttpUrl);
await using var streamToReadFrom = await response.Content.ReadAsStreamAsync();
if ( response.StatusCode != HttpStatusCode.OK )
{
_logger.LogInformation("[Download] HttpClientHelper > " +
response.StatusCode + " ~ " + sourceHttpUrl);
return false;
}

await _storage!.WriteStreamAsync(streamToReadFrom, fullLocalPath);
return true;
}
await _storage!.WriteStreamAsync(streamToReadFrom, fullLocalPath);
return true;
}

try
{
return await RetryHelper.DoAsync(DownloadAsync,
TimeSpan.FromSeconds(retryAfterInSeconds), 2);
}
catch ( AggregateException exception )
try
{
return await RetryHelper.DoAsync(DownloadAsync,
TimeSpan.FromSeconds(retryAfterInSeconds), 2);
}
catch ( AggregateException exception )
{
foreach ( var innerException in exception.InnerExceptions )
{
foreach ( var innerException in exception.InnerExceptions )
{
_logger.LogError(innerException, $"[Download] InnerException: {exception.Message}");
}
_logger.LogError(exception, $"[Download] Exception: {exception.Message}");
return false;
_logger.LogError(innerException, $"[Download] InnerException: {exception.Message}");
}

_logger.LogError(exception, $"[Download] Exception: {exception.Message}");
return false;
}
}

}
Loading

0 comments on commit c8983cd

Please sign in to comment.