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 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(contentStream); } return result; } public async Task 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(contentStream); } return result; } public async Task 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(contentStream); } return result; } public async Task 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(contentStream); } return result; } public async Task 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(contentStream); } return result; } } }