Skip to content

Commit f48d617

Browse files
authored
Merge pull request #5 from cnblogs/support-httpclientfactory
Implement GitLabClientFactory to support IHttpClientFactory
2 parents 58b62c8 + ed9452b commit f48d617

File tree

8 files changed

+393
-199
lines changed

8 files changed

+393
-199
lines changed

.editorconfig

Lines changed: 296 additions & 192 deletions
Large diffs are not rendered by default.

src/GitLabApiClient/GitLabApiClient.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@
2323
<EmbedUntrackedSources>true</EmbedUntrackedSources>
2424
</PropertyGroup>
2525

26+
<Choose>
27+
<When Condition="'$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'net7.0'">
28+
<ItemGroup>
29+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
30+
</ItemGroup>
31+
</When>
32+
<Otherwise>
33+
<ItemGroup>
34+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
35+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
36+
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
37+
</ItemGroup>
38+
</Otherwise>
39+
</Choose>
40+
2641
<ItemGroup>
2742
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" PrivateAssets="All" Version="1.0.3" />
2843
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />

src/GitLabApiClient/GitLabClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public sealed class GitLabClient : IGitLabClient
2424
/// <param name="authenticationToken">Personal access token. Obtained from GitLab profile settings.</param>
2525
/// <param name="httpMessageHandler">Optional handler for HTTP messages. Used for SSL pinning or canceling validation for example.</param>
2626
/// <param name="clientTimeout">Timespan with the HTTP client timeout if null default timeout is used. Sometimes required to upload files to a gitlab instance depending on filesize and networkspeed.</param>
27-
public GitLabClient(string hostUrl, string authenticationToken = "", HttpMessageHandler httpMessageHandler = null, TimeSpan? clientTimeout = null)
27+
public GitLabClient(string hostUrl, string authenticationToken = "", HttpClient httpClient = null, TimeSpan? clientTimeout = null)
2828
{
2929
Guard.NotEmpty(hostUrl, nameof(hostUrl));
3030
Guard.NotNull(authenticationToken, nameof(authenticationToken));
@@ -36,7 +36,7 @@ public GitLabClient(string hostUrl, string authenticationToken = "", HttpMessage
3636
HostUrl,
3737
jsonSerializer,
3838
authenticationToken,
39-
httpMessageHandler,
39+
httpClient ?? new HttpClient(),
4040
clientTimeout);
4141

4242
var projectQueryBuilder = new ProjectsQueryBuilder();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Net.Http;
2+
3+
using Microsoft.Extensions.Options;
4+
5+
namespace GitLabApiClient
6+
{
7+
public class GitLabClientFactory : IGitLabClientFactory
8+
{
9+
private readonly IHttpClientFactory _httpClientFactory;
10+
private readonly GitLabClientOptions _options;
11+
12+
public GitLabClientFactory(IHttpClientFactory httpClientFactory, IOptions<GitLabClientOptions> options)
13+
{
14+
_httpClientFactory = httpClientFactory;
15+
_options = options.Value;
16+
}
17+
18+
public IGitLabClient CreateClient()
19+
{
20+
return new GitLabClient(
21+
_options.HostUrl,
22+
_options.AuthenticationToken,
23+
_httpClientFactory.CreateClient(),
24+
_options.ClientTimeout);
25+
}
26+
}
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace GitLabApiClient
6+
{
7+
public class GitLabClientOptions
8+
{
9+
public string HostUrl { get; set; }
10+
public string AuthenticationToken { get; set; }
11+
public TimeSpan? ClientTimeout { get; internal set; }
12+
}
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
3+
using GitLabApiClient;
4+
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection.Extensions;
7+
8+
namespace Microsoft.Extensions.DependencyInjection
9+
{
10+
public static class GitLabClientServiceCollectionExtensions
11+
{
12+
public static IServiceCollection AddGitLabClient(
13+
this IServiceCollection services,
14+
string sectionKey = "gitLabClient")
15+
{
16+
services.AddOptions();
17+
var config = services.BuildServiceProvider().GetRequiredService<IConfiguration>(); ;
18+
services.Configure<GitLabClientOptions>(config.GetSection(sectionKey));
19+
20+
services.AddHttpClient();
21+
services.TryAddSingleton<IGitLabClientFactory, GitLabClientFactory>();
22+
23+
return services;
24+
}
25+
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GitLabApiClient
2+
{
3+
public interface IGitLabClientFactory
4+
{
5+
IGitLabClient CreateClient();
6+
}
7+
}

src/GitLabApiClient/Internal/Http/GitLabHttpFacade.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net.Http;
55
using System.Net.Http.Headers;
66
using System.Threading.Tasks;
7+
78
using GitLabApiClient.Internal.Http.Serialization;
89
using GitLabApiClient.Models.Oauth.Requests;
910
using GitLabApiClient.Models.Oauth.Responses;
@@ -20,17 +21,18 @@ internal sealed class GitLabHttpFacade
2021
private GitLabApiRequestor _requestor;
2122
private GitLabApiPagedRequestor _pagedRequestor;
2223

23-
private GitLabHttpFacade(string hostUrl, RequestsJsonSerializer jsonSerializer, HttpMessageHandler httpMessageHandler, TimeSpan? clientTimeout = null)
24+
private GitLabHttpFacade(string hostUrl, RequestsJsonSerializer jsonSerializer, HttpClient httpClient, TimeSpan? clientTimeout = null)
2425
{
25-
_httpClient = new HttpClient(httpMessageHandler ?? new HttpClientHandler()) { BaseAddress = new Uri(hostUrl) };
26+
_httpClient = httpClient ?? new HttpClient();
27+
_httpClient.BaseAddress = new Uri(hostUrl);
2628
if (clientTimeout.HasValue)
2729
_httpClient.Timeout = clientTimeout.Value;
2830

2931
Setup(jsonSerializer);
3032
}
3133

32-
public GitLabHttpFacade(string hostUrl, RequestsJsonSerializer jsonSerializer, string authenticationToken = "", HttpMessageHandler httpMessageHandler = null, TimeSpan? clientTimeout = null) :
33-
this(hostUrl, jsonSerializer, httpMessageHandler, clientTimeout)
34+
public GitLabHttpFacade(string hostUrl, RequestsJsonSerializer jsonSerializer, string authenticationToken = "", HttpClient httpClient = null, TimeSpan? clientTimeout = null) :
35+
this(hostUrl, jsonSerializer, httpClient, clientTimeout)
3436
{
3537
switch (authenticationToken.Length)
3638
{
@@ -49,7 +51,7 @@ public GitLabHttpFacade(string hostUrl, RequestsJsonSerializer jsonSerializer, s
4951

5052
public GitLabHttpFacade(RequestsJsonSerializer jsonSerializer, HttpClient httpClient)
5153
{
52-
_httpClient = httpClient;
54+
_httpClient = httpClient ?? new HttpClient();
5355
Setup(jsonSerializer);
5456
}
5557

0 commit comments

Comments
 (0)