Skip to content

Commit 380a095

Browse files
committed
Allow custom HttpClient to be used
1 parent fd83c6a commit 380a095

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

SimpleFeedReader/FeedReader.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ namespace SimpleFeedReader;
1717
/// When true, the <see cref="FeedReader"/> will throw on errors, when false the <see cref="FeedReader"/> will
1818
/// suppress exceptions and return empty results.
1919
/// </param>
20-
public class FeedReader(IFeedItemNormalizer defaultFeedItemNormalizer, bool throwOnError) : IFeedReader
20+
/// <param name="httpClient">
21+
/// When you want to use a custom <see cref="HttpClient"/> you can set it through here.
22+
/// </param>
23+
public class FeedReader(IFeedItemNormalizer defaultFeedItemNormalizer, bool throwOnError, HttpClient? httpClient = null) : IFeedReader
2124
{
22-
private static readonly HttpClient _httpclient = new();
25+
private readonly HttpClient _httpclient = httpClient ?? new();
2326

2427
/// <summary>
2528
/// Gets the default FeedItemNormalizer the <see cref="FeedReader"/> will use when normalizing
@@ -36,8 +39,11 @@ public class FeedReader(IFeedItemNormalizer defaultFeedItemNormalizer, bool thro
3639
/// <summary>
3740
/// Initializes a new instance of the <see cref="FeedReader"/> class.
3841
/// </summary>
39-
public FeedReader()
40-
: this(new DefaultFeedItemNormalizer()) { }
42+
/// <param name="httpClient">
43+
/// When you want to use a custom <see cref="HttpClient"/> you can set it through here.
44+
/// </param>
45+
public FeedReader(HttpClient? httpClient = null)
46+
: this(new DefaultFeedItemNormalizer(), httpClient) { }
4147

4248
/// <summary>
4349
/// Initializes a new instance of the <see cref="FeedReader"/> class.
@@ -46,17 +52,23 @@ public FeedReader()
4652
/// When true, the <see cref="FeedReader"/> will throw on errors, when false the <see cref="FeedReader"/> will
4753
/// suppress exceptions and return empty results.
4854
/// </param>
49-
public FeedReader(bool throwOnError)
50-
: this(new DefaultFeedItemNormalizer(), throwOnError) { }
55+
/// <param name="httpClient">
56+
/// When you want to use a custom <see cref="HttpClient"/> you can set it through here.
57+
/// </param>
58+
public FeedReader(bool throwOnError, HttpClient? httpClient = null)
59+
: this(new DefaultFeedItemNormalizer(), throwOnError, httpClient) { }
5160

5261
/// <summary>
5362
/// Initializes a new instance of the <see cref="FeedReader"/> class.
5463
/// </summary>
5564
/// <param name="defaultFeedItemNormalizer">
5665
/// The <see cref="IFeedItemNormalizer"/> to use when normalizing <see cref="SyndicationItem"/>s.
5766
/// </param>
58-
public FeedReader(IFeedItemNormalizer defaultFeedItemNormalizer)
59-
: this(defaultFeedItemNormalizer, false) { }
67+
/// <param name="httpClient">
68+
/// When you want to use a custom <see cref="HttpClient"/> you can set it through here.
69+
/// </param>
70+
public FeedReader(IFeedItemNormalizer defaultFeedItemNormalizer, HttpClient? httpClient = null)
71+
: this(defaultFeedItemNormalizer, false, httpClient) { }
6072

6173
/// <inheritdoc/>
6274
public Task<IEnumerable<FeedItem>> RetrieveFeedsAsync(IEnumerable<string> uris, CancellationToken cancellationToken = default)
@@ -126,7 +138,7 @@ public Task<IEnumerable<FeedItem>> RetrieveFeedAsync(XmlReader xmlReader, IFeedI
126138
return Task.FromResult(Enumerable.Empty<FeedItem>());
127139
}
128140

129-
private static async Task<XmlReader> GetXmlReaderAsync(string uri, CancellationToken cancellationToken = default)
141+
private async Task<XmlReader> GetXmlReaderAsync(string uri, CancellationToken cancellationToken = default)
130142
{
131143
if (Uri.IsWellFormedUriString(uri, UriKind.Absolute))
132144
{

0 commit comments

Comments
 (0)