Skip to content

Commit c796a5a

Browse files
committed
feat: check if a file exists
1 parent 223e544 commit c796a5a

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed

src/GitLabApiClient/FilesClient.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Net;
12
using System.Threading.Tasks;
3+
24
using GitLabApiClient.Internal.Http;
35
using GitLabApiClient.Internal.Paths;
46
using GitLabApiClient.Internal.Utilities;
@@ -16,5 +18,16 @@ public async Task<File> GetAsync(ProjectId projectId, string filePath, string re
1618
{
1719
return await _httpFacade.Get<File>($"projects/{projectId}/repository/files/{filePath.UrlEncode()}?ref={reference}");
1820
}
21+
22+
public async Task<bool> ExistsAsync(ProjectId projectId, string filePath, string reference = "master")
23+
{
24+
var response = await _httpFacade.Head($"projects/{projectId}/repository/files/{filePath.UrlEncode()}?ref={reference}");
25+
return response.StatusCode switch
26+
{
27+
HttpStatusCode.OK => true,
28+
HttpStatusCode.NotFound => false,
29+
_ => throw new GitLabException(response.StatusCode, "")
30+
};
31+
}
1932
}
2033
}

src/GitLabApiClient/GitLabApiClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<IncludeSymbols>true</IncludeSymbols>
1717
<UseFullSemVerForNuGet>true</UseFullSemVerForNuGet>
1818
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
19+
<LangVersion>latest</LangVersion>
1920
</PropertyGroup>
2021

2122
<PropertyGroup Condition=" '$(GITHUB_ACTIONS)' == 'true'">

src/GitLabApiClient/IFilesClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Threading.Tasks;
2+
23
using GitLabApiClient.Internal.Paths;
34
using GitLabApiClient.Models.Files.Responses;
45

@@ -7,5 +8,6 @@ namespace GitLabApiClient
78
public interface IFilesClient
89
{
910
Task<File> GetAsync(ProjectId projectId, string filePath, string reference = "master");
11+
Task<bool> ExistsAsync(ProjectId projectId, string filePath, string reference = "master");
1012
}
1113
}

src/GitLabApiClient/Internal/Http/GitLabHttpFacade.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ private void Setup(RequestsJsonSerializer jsonSerializer)
6464
_pagedRequestor = new GitLabApiPagedRequestor(_requestor);
6565
}
6666

67+
public Task<HttpResponseMessage> Head(string url) =>
68+
_requestor.Head(url);
69+
6770
public Task<IList<T>> GetPagedList<T>(string uri) =>
6871
_pagedRequestor.GetPagedList<T>(uri);
6972

src/GitLabApiClient/Internal/Http/GitlabApiRequestor.cs

Lines changed: 7 additions & 0 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.Uploads.Requests;
910
using GitLabApiClient.Models.Uploads.Responses;
@@ -21,6 +22,12 @@ public GitLabApiRequestor(HttpClient client, RequestsJsonSerializer jsonSerializ
2122
_jsonSerializer = jsonSerializer;
2223
}
2324

25+
public async Task<HttpResponseMessage> Head(string url)
26+
{
27+
var requestMessage = new HttpRequestMessage(HttpMethod.Head, url);
28+
return await _client.SendAsync(requestMessage);
29+
}
30+
2431
public async Task<T> Get<T>(string url)
2532
{
2633
var responseMessage = await _client.GetAsync(url);

0 commit comments

Comments
 (0)