using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Pathoschild.Http.Client;
using StardewModdingAPI.Web.Models;
namespace StardewModdingAPI.Web.Framework
{
/// An HTTP client for fetching mod metadata from Nexus Mods.
internal class NexusModsClient : IModRepository
{
/*********
** Properties
*********/
/// The underlying HTTP client.
private readonly IClient Client;
/*********
** Public methods
*********/
/// Construct an instance.
public NexusModsClient()
{
this.Client = new FluentClient("http://www.nexusmods.com/stardewvalley")
.SetUserAgent("Nexus Client v0.63.15");
}
/// Get metadata about a mod in the repository.
/// The mod ID in this repository.
public async Task GetModInfoAsync(int id)
{
try
{
NexusResponseModel response = await this.Client
.GetAsync($"mods/{id}")
.As();
return new ModGenericModel("Nexus", id, response.Name, response.Version, response.Url);
}
catch (Exception)
{
return new ModGenericModel("Nexus", id);
}
}
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
{
this.Client.Dispose();
}
/*********
** Private models
*********/
/// A mod metadata response from Nexus Mods.
private class NexusResponseModel
{
/*********
** Accessors
*********/
/// The unique mod ID.
public int ID { get; set; }
/// The mod name.
public string Name { get; set; }
/// The mod's semantic version number.
public string Version { get; set; }
/// The mod's web URL.
[JsonProperty("mod_page_uri")]
public string Url { get; set; }
}
}
}