代码拉取完成,页面将自动刷新
using Extensions.Configuration.GitRepository.GiteeProvider;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using static System.Net.Mime.MediaTypeNames;
namespace Extensions.Configuration.GitRepository.GiteeProvider
{
public record gist_file(int size, string raw_url, string type, bool truncated, string content);
public record gist_file_put( string content);
public record gist(string url, string forks_url, string commits_url, string id, string description, Dictionary<string, gist_file> files, bool truncated, string html_url, int comments, string comments_url, string git_pull_url, string git_push_url, string created_at, string updated_at, string forks, string history);
internal class GiteeGistsClient : IGitRepositoryClient
{
private readonly GitRepositoryConfigurationOptions _options;
private HttpClient _client;
private string _id;
private string owner;
private string repoName;
public GiteeGistsClient(GitRepositoryConfigurationOptions options)
{
_options = options;
}
private void check_config()
{
if (_client == null)
{
_client = new HttpClient();
_id = _options.RepositoryPath;
_client.BaseAddress = new Uri($"https://gitee.com/api/v5/gists/{_id}");
}
}
public bool FileExists(string _filePath)
{
bool result = false;
check_config();
var reps = _client.GetAsync($"?access_token={_options.AuthenticationToken}").GetAwaiter().GetResult();
if (reps.IsSuccessStatusCode)
{
var text = reps.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var obj= JsonSerializer.Deserialize<gist>(text);
result = obj.files.Any(s => s.Key == _filePath);
}
return result;
}
public string GetFile(string _fileName)
{
string result = string.Empty;
check_config();
var reps = _client.GetAsync($"?access_token={_options.AuthenticationToken}").GetAwaiter().GetResult();
if (reps.IsSuccessStatusCode)
{
var text = reps.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var obj = JsonSerializer.Deserialize<gist>(text);
var gf = obj.files.FirstOrDefault(s => s.Key == _fileName);
result = gf.Value?.content;
}
return result;
}
public void PutFile(string _fileName, string _content, string _msg)
{
check_config();
//curl -X POST --header 'Content-Type: ' 'https://gitee.com/api/v5/repos/maikebing/gitcfg/contents/Gitee.json' -d '{"access_token":"d675106450be61985dd39ec076cc05c0","content":"sdfsd","message":"sdfsd"}'
Dictionary<string, gist_file_put> content = new Dictionary<string, gist_file_put>();
content.Add(_fileName, new gist_file_put( _content));
var body = JsonContent.Create(new { access_token = _options.AuthenticationToken, id=_id, files = content , description = _msg });
var reps = _client.PatchAsync($"", body).GetAwaiter().GetResult();
var result = reps.IsSuccessStatusCode;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。