Skip to content

Commit f6fbaab

Browse files
committed
Provide extension method and service to access capabilities
1 parent 56ae796 commit f6fbaab

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

AngleSharp.Io/AngleSharp.Io.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
<Reference Include="System.Xml" />
4747
</ItemGroup>
4848
<ItemGroup>
49+
<Compile Include="ConfigurationExtensions.cs" />
4950
<Compile Include="Extensions\GeneralExtensions.cs" />
5051
<Compile Include="Network\HttpClientRequester.cs" />
5152
<Compile Include="Network\Response.cs" />
5253
<Compile Include="Properties\AssemblyInfo.cs" />
54+
<Compile Include="Services\LoaderService.cs" />
5355
</ItemGroup>
5456
<ItemGroup>
5557
<None Include="packages.config" />
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace AngleSharp
2+
{
3+
using AngleSharp.Io.Network;
4+
using AngleSharp.Io.Services;
5+
using AngleSharp.Network;
6+
using AngleSharp.Network.Default;
7+
using AngleSharp.Services;
8+
using System.Linq;
9+
10+
/// <summary>
11+
/// Additional extensions for improved requesters.
12+
/// </summary>
13+
public static class ConfigurationExtensions
14+
{
15+
/// <summary>
16+
/// Adds a loader service that comes with all (improved) requesters.
17+
/// </summary>
18+
/// <param name="configuration">The configuration to use.</param>
19+
/// <returns>The new configuration.</returns>
20+
public static IConfiguration WithRequesters(this IConfiguration configuration)
21+
{
22+
if (!configuration.Services.OfType<ILoaderService>().Any())
23+
{
24+
var requesters = new IRequester [] { new HttpClientRequester(), new DataRequester() };
25+
var service = new LoaderService(requesters);
26+
return configuration.With(service);
27+
}
28+
29+
return configuration;
30+
}
31+
}
32+
}

AngleSharp.Io/Network/HttpClientRequester.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,22 @@
1515
/// </summary>
1616
public class HttpClientRequester : IRequester
1717
{
18+
#region Fields
19+
1820
readonly HttpClient _client;
1921

22+
#endregion
23+
24+
#region ctor
25+
26+
/// <summary>
27+
/// Creates a new HTTP client request with a new HttpClient instance.
28+
/// </summary>
29+
public HttpClientRequester()
30+
: this(new HttpClient())
31+
{
32+
}
33+
2034
/// <summary>
2135
/// Creates a new HTTP client request.
2236
/// </summary>
@@ -26,6 +40,10 @@ public HttpClientRequester(HttpClient client)
2640
_client = client;
2741
}
2842

43+
#endregion
44+
45+
#region Methods
46+
2947
/// <summary>
3048
/// Checks if the given protocol is supported.
3149
/// </summary>
@@ -94,5 +112,7 @@ public async Task<IResponse> RequestAsync(IRequest request, CancellationToken ca
94112

95113
return response;
96114
}
115+
116+
#endregion
97117
}
98118
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace AngleSharp.Io.Services
2+
{
3+
using AngleSharp.Dom;
4+
using AngleSharp.Network;
5+
using AngleSharp.Network.Default;
6+
using AngleSharp.Services;
7+
using System.Collections.Generic;
8+
9+
/// <summary>
10+
/// The adjusted loader service to use.
11+
/// </summary>
12+
public class LoaderService : ILoaderService
13+
{
14+
readonly IEnumerable<IRequester> _requesters;
15+
16+
/// <summary>
17+
/// Creates a new loader service with the provided requesters.
18+
/// </summary>
19+
/// <param name="requesters">The requesters to use.</param>
20+
public LoaderService(IEnumerable<IRequester> requesters)
21+
{
22+
_requesters = requesters;
23+
}
24+
25+
/// <summary>
26+
/// Gets the available requesters.
27+
/// </summary>
28+
public IEnumerable<IRequester> Requesters
29+
{
30+
get { return _requesters; }
31+
}
32+
33+
/// <summary>
34+
/// Gets the appropriate requester for the provided address.
35+
/// </summary>
36+
/// <param name="address">
37+
/// The address the requester needs to be able to handle.
38+
/// </param>
39+
/// <returns>The requester or null.</returns>
40+
public IRequester GetRequester(Url address)
41+
{
42+
foreach (var requester in _requesters)
43+
{
44+
if (requester.SupportsProtocol(address.Scheme))
45+
return requester;
46+
}
47+
48+
return default(IRequester);
49+
}
50+
51+
/// <summary>
52+
/// Creates the document loader for the given context.
53+
/// </summary>
54+
/// <param name="context">The context to use.</param>
55+
/// <returns>The created document loader.</returns>
56+
public IDocumentLoader CreateDocumentLoader(IBrowsingContext context)
57+
{
58+
return new DocumentLoader(_requesters, context);
59+
}
60+
61+
/// <summary>
62+
/// Creates the resource loader for the given document.
63+
/// </summary>
64+
/// <param name="document">The document to host the loading.</param>
65+
/// <returns>The created resource loader.</returns>
66+
public IResourceLoader CreateResourceLoader(IDocument document)
67+
{
68+
return new ResourceLoader(_requesters, document);
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)