2021-09-27 12:42:46 +00:00
|
|
|
using System;
|
2021-07-19 22:15:27 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2022-03-31 22:55:33 +00:00
|
|
|
using System.Net.Http;
|
|
|
|
using System.Net.Http.Json;
|
2021-07-19 22:15:27 +00:00
|
|
|
using System.Threading.Tasks;
|
2021-09-27 12:42:46 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-10-23 16:51:17 +00:00
|
|
|
using Octokit;
|
2021-09-27 12:42:46 +00:00
|
|
|
using Wabbajack.DTOs;
|
|
|
|
using Wabbajack.DTOs.GitHub;
|
|
|
|
using Wabbajack.DTOs.JsonConverters;
|
2022-03-31 22:55:33 +00:00
|
|
|
using Wabbajack.Networking.GitHub.DTOs;
|
2021-07-19 22:15:27 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
namespace Wabbajack.Networking.GitHub;
|
|
|
|
|
|
|
|
public class Client
|
2021-07-19 22:15:27 +00:00
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
private readonly GitHubClient _client;
|
|
|
|
private readonly DTOSerializer _dtos;
|
|
|
|
private readonly ILogger<Client> _logger;
|
|
|
|
private readonly GithubAuthTokenProvider _token;
|
2022-03-31 22:55:33 +00:00
|
|
|
private readonly HttpClient _httpClient;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
2022-03-31 22:55:33 +00:00
|
|
|
public Client(ILogger<Client> logger, DTOSerializer dtos, GitHubClient client, HttpClient httpClient)
|
2021-07-19 22:15:27 +00:00
|
|
|
{
|
2022-03-31 22:55:33 +00:00
|
|
|
_httpClient = httpClient;
|
2021-10-23 16:51:17 +00:00
|
|
|
_logger = logger;
|
|
|
|
_client = client;
|
|
|
|
_dtos = dtos;
|
|
|
|
}
|
2021-07-19 22:15:27 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public async Task<(string Hash, IReadOnlyList<ModlistMetadata> Lists)> GetData(List lst)
|
|
|
|
{
|
|
|
|
var content =
|
|
|
|
(await _client.Repository.Content.GetAllContents("wabbajack-tools", "mod-lists", PathNames.FromList[lst]))
|
|
|
|
.First();
|
|
|
|
return (content.Sha, _dtos.Deserialize<ModlistMetadata[]>(content.Content)!);
|
|
|
|
}
|
2021-07-21 21:40:55 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
private async Task WriteData(List file, string machinenUrl, string dataHash,
|
|
|
|
IReadOnlyList<ModlistMetadata> dataLists)
|
|
|
|
{
|
|
|
|
var listData = _dtos.Serialize(dataLists, true);
|
|
|
|
// the website requires all names be in lowercase;
|
|
|
|
listData = GameRegistry.Games.Keys.Aggregate(listData,
|
|
|
|
(current, g) => current.Replace($"\"game\": \"{g}\",", $"\"game\": \"{g.ToString().ToLower()}\","));
|
2021-07-19 22:15:27 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
var updateRequest = new UpdateFileRequest($"New release of {machinenUrl}", listData, dataHash);
|
|
|
|
await _client.Repository.Content.UpdateFile("wabbajack-tools", "mod-lists", PathNames.FromList[file],
|
|
|
|
updateRequest);
|
|
|
|
}
|
2021-07-19 22:15:27 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
public async Task UpdateList(string maintainer, UpdateRequest newData)
|
|
|
|
{
|
|
|
|
foreach (var file in Enum.GetValues<List>())
|
|
|
|
{
|
|
|
|
var data = await GetData(file);
|
|
|
|
var found = data.Lists.FirstOrDefault(l =>
|
2022-03-30 03:39:48 +00:00
|
|
|
l.NamespacedName == newData.MachineUrl && l.Maintainers.Contains(maintainer));
|
2021-10-23 16:51:17 +00:00
|
|
|
if (found == null) continue;
|
2021-07-19 22:15:27 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
found.DownloadMetadata = newData.DownloadMetadata;
|
|
|
|
found.Version = newData.Version;
|
|
|
|
found.Links.Download = newData.DownloadUrl.ToString();
|
2021-07-19 22:15:27 +00:00
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
await WriteData(file, newData.MachineUrl, data.Hash, data.Lists);
|
|
|
|
return;
|
2021-07-19 22:15:27 +00:00
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
|
|
|
throw new Exception("List not found or user not authorized");
|
2021-07-19 22:15:27 +00:00
|
|
|
}
|
2021-12-17 14:20:40 +00:00
|
|
|
|
|
|
|
public async Task<(string Sha, string Content)> GetData(string owner, string repo, string path)
|
|
|
|
{
|
|
|
|
var result = (await _client.Repository.Content.GetAllContents(owner, repo, path))[0];
|
|
|
|
return (result.Sha, result.Content);
|
|
|
|
}
|
|
|
|
|
2022-03-31 22:55:33 +00:00
|
|
|
public async Task<UserInfo?> GetUserInfoFromPAT(string pat)
|
|
|
|
{
|
|
|
|
var msg = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/user");
|
|
|
|
msg.Headers.Add("User-Agent", "wabbajack");
|
|
|
|
msg.Headers.Add("Authorization", "Token " + pat);
|
|
|
|
var result = await _httpClient.SendAsync(msg);
|
|
|
|
if (!result.IsSuccessStatusCode) return null;
|
|
|
|
return await result.Content.ReadFromJsonAsync<UserInfo>();
|
|
|
|
}
|
|
|
|
|
2021-12-17 14:20:40 +00:00
|
|
|
public async Task PutData(string owner, string repo, string path, string message, string content, string oldSha)
|
|
|
|
{
|
|
|
|
await _client.Repository.Content.UpdateFile(owner, repo, path, new UpdateFileRequest(message, content, oldSha));
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
}
|