using System.Collections.Generic;
using System.Threading.Tasks;
using Pathoschild.Http.Client;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
using StardewModdingAPI.Web.Framework.Clients.ModDrop.ResponseModels;
namespace StardewModdingAPI.Web.Framework.Clients.ModDrop
{
/// An HTTP client for fetching mod metadata from the ModDrop API.
internal class ModDropClient : IModDropClient
{
/*********
** Fields
*********/
/// The underlying HTTP client.
private readonly IClient Client;
/// The URL for a ModDrop mod page for the user, where {0} is the mod ID.
private readonly string ModUrlFormat;
/*********
** Accessors
*********/
/// The unique key for the mod site.
public ModSiteKey SiteKey => ModSiteKey.ModDrop;
/*********
** Public methods
*********/
/// Construct an instance.
/// The user agent for the API client.
/// The base URL for the ModDrop API.
/// The URL for a ModDrop mod page for the user, where {0} is the mod ID.
public ModDropClient(string userAgent, string apiUrl, string modUrlFormat)
{
this.Client = new FluentClient(apiUrl).SetUserAgent(userAgent);
this.ModUrlFormat = modUrlFormat;
}
/// Get update check info about a mod.
/// The mod ID.
public async Task GetModData(string id)
{
var page = new GenericModPage(this.SiteKey, id);
if (!long.TryParse(id, out long parsedId))
return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid ModDrop mod ID, must be an integer ID.");
// get raw data
ModListModel response = await this.Client
.PostAsync("")
.WithBody(new
{
ModIDs = new[] { parsedId },
Files = true,
Mods = true
})
.As();
ModModel mod = response.Mods[parsedId];
if (mod.Mod?.Title == null || mod.Mod.ErrorCode.HasValue)
return null;
// get files
var downloads = new List();
foreach (FileDataModel file in mod.Files)
{
if (file.IsOld || file.IsDeleted || file.IsHidden)
continue;
downloads.Add(
new GenericModDownload(file.Name, file.Description, file.Version)
);
}
// return info
string name = mod.Mod?.Title;
string url = string.Format(this.ModUrlFormat, id);
return page.SetInfo(name: name, version: null, url: url, downloads: downloads);
}
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
{
this.Client?.Dispose();
}
}
}