* Added SendRequestAsync
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/pr/woodpecker Pipeline was successful
* Changed method names
This commit is contained in:
@@ -6,15 +6,7 @@ public class GiteaConfiguration
|
||||
public string Owner { get; set; } = string.Empty;
|
||||
public string Repository { get; set; } = string.Empty;
|
||||
public string Branch { get; set; } = string.Empty;
|
||||
public string RequestUrl { get; set; }
|
||||
public string ApiToken { get; set; } = string.Empty;
|
||||
|
||||
public GiteaConfiguration()
|
||||
{
|
||||
RequestUrl = GetRequestUrl();
|
||||
}
|
||||
|
||||
private string GetRequestUrl()
|
||||
{
|
||||
return $"{Host}/api/v1/repos/{Owner}/{Repository}/contents";
|
||||
}
|
||||
public string RequestUrl => $"{Host}/api/v1/repos/{Owner}/{Repository}";
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
using Gitea.Net.Api;
|
||||
using Gitea.Net.Client;
|
||||
using Gitea.Net.Model;
|
||||
using HttpMethod = System.Net.Http.HttpMethod;
|
||||
|
||||
namespace Services.Gitea;
|
||||
|
||||
public class GiteaService : IGiteaService
|
||||
{
|
||||
public RepositoryApi CreateGiteaClient(string basePath, string giteaApiToken)
|
||||
private static readonly HttpClient HttpClient = new();
|
||||
|
||||
public RepositoryApi CreateGiteaClientAsync(string basePath, string giteaApiToken)
|
||||
{
|
||||
var config = new Configuration
|
||||
{
|
||||
@@ -20,7 +23,7 @@ public class GiteaService : IGiteaService
|
||||
return new RepositoryApi(config);
|
||||
}
|
||||
|
||||
public async Task<List<Commit>> GetLastCommits(RepositoryApi repositoryApi, GiteaConfiguration configuration,
|
||||
public async Task<List<Commit>> GetLastCommitsAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration,
|
||||
int limit)
|
||||
{
|
||||
var lastCommits = await repositoryApi.RepoGetAllCommitsAsync(configuration.Owner, configuration.Repository,
|
||||
@@ -28,17 +31,41 @@ public class GiteaService : IGiteaService
|
||||
return lastCommits;
|
||||
}
|
||||
|
||||
public async Task<Commit?> GetLastCommit(RepositoryApi repositoryApi, GiteaConfiguration configuration)
|
||||
public async Task<Commit?> GetLastCommitAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration)
|
||||
{
|
||||
var lastCommit = await GetLastCommits(repositoryApi, configuration, 1);
|
||||
var lastCommit = await GetLastCommitsAsync(repositoryApi, configuration, 1);
|
||||
return lastCommit.FirstOrDefault();
|
||||
}
|
||||
|
||||
public async Task<ContentsResponse> GetFileContent(RepositoryApi repositoryApi, GiteaConfiguration configuration,
|
||||
public async Task<ContentsResponse> GetFileContentAsync(RepositoryApi repositoryApi,
|
||||
GiteaConfiguration configuration,
|
||||
string filename)
|
||||
{
|
||||
var repoGetContentsAsync =
|
||||
await repositoryApi.RepoGetContentsAsync(configuration.Owner, configuration.Repository, filename);
|
||||
return repoGetContentsAsync;
|
||||
}
|
||||
|
||||
public async Task<(bool Status, string Response)> SendRequestAsync(GiteaConfiguration configuration,
|
||||
string endpoint, HttpContent content)
|
||||
{
|
||||
var requestUrl = $"{configuration.RequestUrl}/{endpoint}";
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl)
|
||||
{
|
||||
Content = content,
|
||||
Headers = { {"Content-Type", "application/json"}, { "Authorization", $"token {configuration.ApiToken}" } }
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var giteaResponse = await HttpClient.SendAsync(request);
|
||||
var response = await giteaResponse.Content.ReadAsStringAsync();
|
||||
|
||||
return (giteaResponse.IsSuccessStatusCode, response);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"Error while sending request to Gitea: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,14 @@ namespace Services.Gitea;
|
||||
|
||||
public interface IGiteaService
|
||||
{
|
||||
RepositoryApi CreateGiteaClient(string basePath, string giteaApiToken);
|
||||
Task<List<Commit>> GetLastCommits(RepositoryApi repositoryApi, GiteaConfiguration configuration, int limit);
|
||||
RepositoryApi CreateGiteaClientAsync(string basePath, string giteaApiToken);
|
||||
Task<List<Commit>> GetLastCommitsAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration, int limit);
|
||||
|
||||
Task<ContentsResponse> GetFileContent(RepositoryApi repositoryApi, GiteaConfiguration configuration,
|
||||
Task<ContentsResponse> GetFileContentAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration,
|
||||
string filename);
|
||||
|
||||
Task<Commit?> GetLastCommit(RepositoryApi repositoryApi, GiteaConfiguration configuration);
|
||||
Task<Commit?> GetLastCommitAsync(RepositoryApi repositoryApi, GiteaConfiguration configuration);
|
||||
|
||||
Task<(bool Status, string Response)> SendRequestAsync(GiteaConfiguration configuration, string endpoint,
|
||||
HttpContent content);
|
||||
}
|
||||
Reference in New Issue
Block a user