Skip to content

Commit 0cb904e

Browse files
ReallyLiriigalk
andauthored
Added ConnectionClient (#141)
Co-authored-by: Igal Kreichman <igal1987@gmail.com>
1 parent 8d9d097 commit 0cb904e

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using GitLabApiClient.Internal.Http;
4+
5+
namespace GitLabApiClient
6+
{
7+
/// <summary>
8+
/// Provides a client connection to make rest requests to HTTP endpoints.
9+
/// <exception cref="GitLabException">Thrown if request to GitLab API does not indicate success</exception>
10+
/// <exception cref="HttpRequestException">Thrown if request to GitLab API fails</exception>
11+
/// </summary>
12+
public sealed class ConnectionClient
13+
{
14+
private readonly GitLabHttpFacade _httpFacade;
15+
16+
internal ConnectionClient(
17+
GitLabHttpFacade httpFacade
18+
)
19+
{
20+
_httpFacade = httpFacade;
21+
}
22+
23+
/// <summary>
24+
/// Performs an asynchronous HTTP GET request.
25+
/// </summary>
26+
/// <param name="uri">URI endpoint to send request to</param>
27+
public Task<T> GetAsync<T>(string uri)
28+
=> _httpFacade.Get<T>(uri);
29+
30+
/// <summary>
31+
/// Performs an asynchronous HTTP paged GET request that iterates all pages.
32+
/// </summary>
33+
/// <param name="uri">URI endpoint to send request to</param>
34+
public Task<IList<T>> GetPagedListAsync<T>(string uri)
35+
=> _httpFacade.GetPagedList<T>(uri);
36+
}
37+
}

src/GitLabApiClient/GitLabClient.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net.Http;
23
using System.Threading.Tasks;
34
using GitLabApiClient.Internal.Http;
45
using GitLabApiClient.Internal.Http.Serialization;
@@ -23,7 +24,8 @@ public sealed class GitLabClient
2324
/// </summary>
2425
/// <param name="hostUrl">Host address of GitLab instance. For example https://gitlab.example.com or https://gitlab.example.com/api/v4/ </param>
2526
/// <param name="authenticationToken">Personal access token. Obtained from GitLab profile settings.</param>
26-
public GitLabClient(string hostUrl, string authenticationToken = "")
27+
/// <param name="httpMessageHandler">Optional handler for HTTP messages. Used for SSL pinning or canceling validation for example.</param>
28+
public GitLabClient(string hostUrl, string authenticationToken = "", HttpMessageHandler httpMessageHandler = null)
2729
{
2830
Guard.NotEmpty(hostUrl, nameof(hostUrl));
2931
Guard.NotNull(authenticationToken, nameof(authenticationToken));
@@ -34,7 +36,8 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
3436
_httpFacade = new GitLabHttpFacade(
3537
HostUrl,
3638
jsonSerializer,
37-
authenticationToken);
39+
authenticationToken,
40+
httpMessageHandler);
3841

3942
var projectQueryBuilder = new ProjectsQueryBuilder();
4043
var projectIssueNotesQueryBuilder = new ProjectIssueNotesQueryBuilder();
@@ -74,6 +77,7 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
7477
Files = new FilesClient(_httpFacade);
7578
Runners = new RunnersClient(_httpFacade);
7679
ToDoList = new ToDoListClient(_httpFacade, toDoListBuilder);
80+
Connection = new ConnectionClient(_httpFacade);
7781
}
7882

7983
/// <summary>
@@ -161,6 +165,11 @@ public GitLabClient(string hostUrl, string authenticationToken = "")
161165
/// </summary>
162166
public ToDoListClient ToDoList { get; }
163167

168+
/// <summary>
169+
/// Provides a client connection to make rest requests to HTTP endpoints.
170+
/// </summary>
171+
public ConnectionClient Connection { get; }
172+
164173
/// <summary>
165174
/// Host address of GitLab instance. For example https://gitlab.example.com or https://gitlab.example.com/api/v4/.
166175
/// </summary>

src/GitLabApiClient/Internal/Http/GitLabHttpFacade.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ internal sealed class GitLabHttpFacade
2121
private GitLabApiRequestor _requestor;
2222
private GitLabApiPagedRequestor _pagedRequestor;
2323

24-
private GitLabHttpFacade(string hostUrl, RequestsJsonSerializer jsonSerializer)
24+
private GitLabHttpFacade(string hostUrl, RequestsJsonSerializer jsonSerializer, HttpMessageHandler httpMessageHandler)
2525
{
26-
_httpClient = new HttpClient { BaseAddress = new Uri(hostUrl) };
26+
_httpClient = new HttpClient(httpMessageHandler ?? new HttpClientHandler()) { BaseAddress = new Uri(hostUrl) };
2727

2828
Setup(jsonSerializer);
2929
}
3030

31-
public GitLabHttpFacade(string hostUrl, RequestsJsonSerializer jsonSerializer, string authenticationToken = "") :
32-
this(hostUrl, jsonSerializer)
31+
public GitLabHttpFacade(string hostUrl, RequestsJsonSerializer jsonSerializer, string authenticationToken = "", HttpMessageHandler httpMessageHandler = null) :
32+
this(hostUrl, jsonSerializer, httpMessageHandler)
3333
{
3434
switch (authenticationToken.Length)
3535
{

0 commit comments

Comments
 (0)