Skip to content

Commit

Permalink
- Fix how HttpClientFactory is used to acquire HttpClient instance in…
Browse files Browse the repository at this point in the history
… GenerateStaticPagesNow.

- Add httpTimeoutSeconds optional parameter to GenerateStaticPages and GenerateStaticPagesNow.
  • Loading branch information
ZarehD committed Jul 17, 2023
1 parent b6d0b4f commit 88a2239
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/AspNetStatic/AspNetStatic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<Copyright>Copyright 2023 Zareh DerGevorkian. All rights reserved</Copyright>
<Product>AspNetStatic</Product>
<Description>Transforms ASP.NET Core into a static site generator.</Description>
<Version>0.17.0</Version>
<Version>0.17.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
39 changes: 33 additions & 6 deletions src/AspNetStatic/StaticPageGeneratorHostExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,18 @@ public static class StaticPageGeneratorHostExtension
/// Specifies whether periodic re-generation is enabled (non-null value),
/// and the interval between re-generation events.
/// </param>
/// <param name="httpTimeoutSeconds">
/// The HttpClient request timeout (in seconds) while fetching page content.
/// </param>
public static void GenerateStaticPages(
this IHost host,
string destinationRoot,
bool exitWhenDone = default,
bool alwaysDefaultFile = default,
bool dontUpdateLinks = default,
bool dontOptimizeContent = default,
TimeSpan? regenerationInterval = default)
TimeSpan? regenerationInterval = default,
ulong httpTimeoutSeconds = 90)
{
Throw.IfNull(host, nameof(host));
Throw.IfNullOrWhiteSpace(destinationRoot, nameof(destinationRoot), Properties.Resources.Err_ValueCannotBeNullEmptyWhitespace);
Expand Down Expand Up @@ -132,7 +136,7 @@ public static class StaticPageGeneratorHostExtension
if (baseUri is null) Throw.InvalidOp(Properties.Resources.Err_HostNotHttpService);
_httpClient.BaseAddress = new Uri(baseUri);
_httpClient.Timeout = TimeSpan.FromSeconds(90);
_httpClient.Timeout = TimeSpan.FromSeconds(httpTimeoutSeconds);
_httpClient.DefaultRequestHeaders.Add(HeaderNames.UserAgent, Consts.AspNetStatic);
var generatorConfig =
Expand Down Expand Up @@ -266,6 +270,15 @@ public static class StaticPageGeneratorHostExtension
/// <param name="httpClientName">
/// Optional. The name of a configured HTTP client to use for fetching pages.
/// </param>
/// <param name="httpTimeoutSeconds">
/// <para>
/// The HttpClient request timeout (in seconds) while fetching page content.
/// </para>
/// <para>
/// NOTE: Applies only when the HttpClient instance is created locally, or
/// when the timeout for the acquired instance is <see cref="Timespan.Zero"/>.
/// </para>
/// </param>
/// <param name="ct">The object to monitor for cancellation requests.</param>
/// <returns>
/// An object representing the async operation that will return
Expand All @@ -278,6 +291,7 @@ public static class StaticPageGeneratorHostExtension
bool dontUpdateLinks = default,
bool dontOptimizeContent = default,
string? httpClientName = default,
ulong httpTimeoutSeconds = 90,
CancellationToken ct = default)
{
Throw.IfNull(host, nameof(host));
Expand Down Expand Up @@ -313,13 +327,26 @@ public static class StaticPageGeneratorHostExtension
hostUrls.FirstOrDefault(x => x.StartsWith(Uri.UriSchemeHttp));
if (baseUri is null) Throw.InvalidOp(Properties.Resources.Err_HostNotHttpService);

var httpClientFactory = host.Services.GetService<IHttpClientFactory>();

var httpClient =
host.Services.GetService<IHttpClientFactory>()?.CreateClient() ??
new HttpClient()
(httpClientFactory is null)
? new HttpClient()
{
BaseAddress = new Uri(baseUri),
Timeout = TimeSpan.FromSeconds(90),
};
Timeout = TimeSpan.FromSeconds(httpTimeoutSeconds),
}
: string.IsNullOrWhiteSpace(httpClientName)
? httpClientFactory.CreateClient()
: httpClientFactory.CreateClient(httpClientName)
;

httpClient.BaseAddress ??= new Uri(baseUri);

if (httpClient.Timeout == TimeSpan.Zero)
{
httpClient.Timeout = TimeSpan.FromSeconds(httpTimeoutSeconds);
}

if (!httpClient.DefaultRequestHeaders.Any(
x =>
Expand Down

0 comments on commit 88a2239

Please sign in to comment.