@@ -5,64 +5,24 @@ namespace GuardianClient.Extensions;
55public static class ServiceCollectionExtensions
66{
77 /// <summary>
8- /// Adds GuardianApiClient to the service collection with HttpClient factory support
8+ /// Registers <see cref="GuardianApiClient"/> in the service collection,
9+ /// configuring it to use <see cref="IHttpClientFactory"/> for efficient
10+ /// <see cref="HttpClient"/> management.
911 /// </summary>
10- /// <param name="services">The service collection</param>
11- /// <param name="apiKey">Your Guardian API key</param>
12- /// <returns>The service collection for chaining</returns>
12+ /// <param name="services">The DI service collection. </param>
13+ /// <param name="apiKey">The Guardian API key to use for all requests. </param>
14+ /// <returns>The same <paramref name="services"/> instance for chaining. </returns>
1315 public static IServiceCollection AddGuardianApiClient ( this IServiceCollection services , string apiKey )
1416 {
15- if ( string . IsNullOrWhiteSpace ( apiKey ) )
16- throw new ArgumentException ( "API key cannot be null or empty" , nameof ( apiKey ) ) ;
17+ ArgumentException . ThrowIfNullOrWhiteSpace ( apiKey ) ;
1718
18- // Register the HttpClient with IHttpClientFactory
19- services . AddHttpClient < GuardianApiClient > ( client =>
20- {
21- client . BaseAddress = new Uri ( "https://content.guardianapis.com" ) ;
22- client . DefaultRequestHeaders . Add ( "User-Agent" , "GuardianClient.NET/0.1.0-alpha" ) ;
23- } ) ;
19+ services . AddHttpClient < GuardianApiClient > ( ) ;
2420
25- // Register GuardianApiClient as scoped
2621 services . AddScoped < GuardianApiClient > ( serviceProvider =>
2722 {
2823 var httpClientFactory = serviceProvider . GetRequiredService < IHttpClientFactory > ( ) ;
2924 var httpClient = httpClientFactory . CreateClient ( nameof ( GuardianApiClient ) ) ;
30- return new GuardianApiClient ( httpClient , apiKey ) ;
31- } ) ;
32-
33- return services ;
34- }
3525
36- /// <summary>
37- /// Adds GuardianApiClient to the service collection with configuration action
38- /// </summary>
39- /// <param name="services">The service collection</param>
40- /// <param name="apiKey">Your Guardian API key</param>
41- /// <param name="configureClient">Action to configure the HttpClient</param>
42- /// <returns>The service collection for chaining</returns>
43- public static IServiceCollection AddGuardianApiClient (
44- this IServiceCollection services ,
45- string apiKey ,
46- Action < HttpClient > configureClient )
47- {
48- if ( string . IsNullOrWhiteSpace ( apiKey ) )
49- throw new ArgumentException ( "API key cannot be null or empty" , nameof ( apiKey ) ) ;
50-
51- // Register the HttpClient with IHttpClientFactory and custom configuration
52- services . AddHttpClient < GuardianApiClient > ( client =>
53- {
54- client . BaseAddress = new Uri ( "https://content.guardianapis.com" ) ;
55- client . DefaultRequestHeaders . Add ( "User-Agent" , "GuardianClient.NET/0.1.0-alpha" ) ;
56-
57- // Apply custom configuration
58- configureClient ? . Invoke ( client ) ;
59- } ) ;
60-
61- // Register GuardianApiClient as scoped
62- services . AddScoped < GuardianApiClient > ( serviceProvider =>
63- {
64- var httpClientFactory = serviceProvider . GetRequiredService < IHttpClientFactory > ( ) ;
65- var httpClient = httpClientFactory . CreateClient ( nameof ( GuardianApiClient ) ) ;
6626 return new GuardianApiClient ( httpClient , apiKey ) ;
6727 } ) ;
6828
0 commit comments