cfcoreapi/CFCoreAPI/Endpoints/Files/FilesAPI.cs
2022-05-20 16:36:27 -05:00

89 lines
3.7 KiB
C#

using System.Text.Json;
using CFCoreAPI.Endpoints.Files.Requests;
using CFCoreAPI.Endpoints.Files.Responses;
using Microsoft.AspNetCore.WebUtilities;
namespace CFCoreAPI.Endpoints.Files
{
public class FilesAPI : APIBase
{
public FilesAPI(HttpClient httpClient) : base(httpClient)
{
}
public async Task<ModFileResponse> GetModFileAsync(int modId, int fileId)
{
string url = "/v1/mods/{0}/files/{1}";
string.Format(url, modId, fileId);
HttpResponseMessage response = await httpClient.GetAsync(url);
ModFileResponse result;
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
{
result = await JsonSerializer.DeserializeAsync<ModFileResponse>(contentStream);
}
return result;
}
public async Task<ModFilesResponse> GetModFilesAsync(int modId, string? gameVersion = null, ModLoaderType? modLoaderType = null, int? gameVersionTypeId = null, int? index = null, int? pageSize = null)
{
string url = "/v1/mods/{0}/files";
string.Format(url, modId);
if (gameVersion != null) QueryHelpers.AddQueryString(url, "gameVersion", gameVersion);
if (modLoaderType != null) QueryHelpers.AddQueryString(url, "modLoaderType", ((int)modLoaderType.Value).ToString());
if (gameVersionTypeId != null) QueryHelpers.AddQueryString(url, "gameVersionTypeId", gameVersionTypeId.ToString());
if (index != null) QueryHelpers.AddQueryString(url, "index", index.ToString());
if (pageSize != null) QueryHelpers.AddQueryString(url, "pageSize", pageSize.ToString());
HttpResponseMessage response = await httpClient.GetAsync(url);
ModFilesResponse result;
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
{
result = await JsonSerializer.DeserializeAsync<ModFilesResponse>(contentStream);
}
return result;
}
public async Task<FileResponse> GetFilesAsync(ModFilesRequest request)
{
string url = "/v1/mods/files";
HttpContent content = new StringContent(JsonSerializer.Serialize(request));
HttpResponseMessage response = await httpClient.PostAsync(url, content);
FileResponse result;
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
{
result = await JsonSerializer.DeserializeAsync<FileResponse>(contentStream);
}
return result;
}
public async Task<StringResponse> GetModFileChangelog(int modId, int field)
{
string url = "/v1/mods/{0}/files/{1}/changelog";
string.Format(url, modId, field);
HttpResponseMessage response = await httpClient.GetAsync(url);
StringResponse result;
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
{
result = await JsonSerializer.DeserializeAsync<StringResponse>(contentStream);
}
return result;
}
public async Task<StringResponse> GetModFileDownloadURL(int modId, int field)
{
string url = "/v1/mods/{0}/files/{1}/download-url";
string.Format(url, modId, field);
HttpResponseMessage response = await httpClient.GetAsync(url);
StringResponse result;
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
{
result = await JsonSerializer.DeserializeAsync<StringResponse>(contentStream);
}
return result;
}
}
}