summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.Web/Framework/ModRepositories/NexusRepository.cs
blob: 7e3ce4b6a65d8cf98081b43d3e748eaef4b740fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Pathoschild.Http.Client;
using StardewModdingAPI.Web.Models;

namespace StardewModdingAPI.Web.Framework.ModRepositories
{
    /// <summary>An HTTP client for fetching mod metadata from Nexus Mods.</summary>
    internal class NexusRepository : IModRepository
    {
        /*********
        ** Properties
        *********/
        /// <summary>The underlying HTTP client.</summary>
        private readonly IClient Client;


        /*********
        ** Accessors
        *********/
        /// <summary>The unique key for this vendor.</summary>
        public string VendorKey { get; } = "Nexus";


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        public NexusRepository()
        {
            this.Client = new FluentClient("http://www.nexusmods.com/stardewvalley")
                .SetUserAgent("Nexus Client v0.63.15");
        }

        /// <summary>Get metadata about a mod in the repository.</summary>
        /// <param name="id">The mod ID in this repository.</param>
        public async Task<ModInfoModel> GetModInfoAsync(string id)
        {
            try
            {
                NexusResponseModel response = await this.Client
                    .GetAsync($"mods/{id}")
                    .As<NexusResponseModel>();

                return response != null
                    ? new ModInfoModel($"{this.VendorKey}:{id}", response.Name, response.Version, response.Url)
                    : new ModInfoModel($"{this.VendorKey}:{id}", "Found no mod with this ID.");
            }
            catch (Exception ex)
            {
                return new ModInfoModel($"{this.VendorKey}:{id}", ex.ToString());
            }
        }

        /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
        public void Dispose()
        {
            this.Client.Dispose();
        }


        /*********
        ** Private models
        *********/
        /// <summary>A mod metadata response from Nexus Mods.</summary>
        private class NexusResponseModel
        {
            /*********
            ** Accessors
            *********/
            /// <summary>The mod name.</summary>
            public string Name { get; set; }

            /// <summary>The mod's semantic version number.</summary>
            public string Version { get; set; }

            /// <summary>The mod's web URL.</summary>
            [JsonProperty("mod_page_uri")]
            public string Url { get; set; }
        }
    }
}