summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs
blob: ef1904d432d72ef4c991e483519ecaabe0b0927b (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Pathoschild.Http.Client;
using StardewModdingAPI.Toolkit.Serialization;
using StardewModdingAPI.Toolkit.Utilities;

namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi
{
    /// <summary>Provides methods for interacting with the SMAPI web API.</summary>
    public class WebApiClient : IDisposable
    {
        /*********
        ** Fields
        *********/
        /// <summary>The API version number.</summary>
        private readonly ISemanticVersion Version;

        /// <summary>The underlying HTTP client.</summary>
        private readonly IClient Client;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="baseUrl">The base URL for the web API.</param>
        /// <param name="version">The web API version.</param>
        public WebApiClient(string baseUrl, ISemanticVersion version)
        {
            this.Version = version;
            this.Client = new FluentClient(baseUrl)
                .SetUserAgent($"SMAPI/{version}");

            this.Client.Formatters.JsonFormatter.SerializerSettings = JsonHelper.CreateDefaultSettings();
        }

        /// <summary>Get metadata about a set of mods from the web API.</summary>
        /// <param name="mods">The mod keys for which to fetch the latest version.</param>
        /// <param name="apiVersion">The SMAPI version installed by the player. If this is null, the API won't provide a recommended update.</param>
        /// <param name="gameVersion">The Stardew Valley version installed by the player.</param>
        /// <param name="platform">The OS on which the player plays.</param>
        /// <param name="includeExtendedMetadata">Whether to include extended metadata for each mod.</param>
        public async Task<IDictionary<string, ModEntryModel>> GetModInfoAsync(ModSearchEntryModel[] mods, ISemanticVersion apiVersion, ISemanticVersion gameVersion, Platform platform, bool includeExtendedMetadata = false)
        {
            ModEntryModel[] result = await this.Client
                .PostAsync(
                    $"v{this.Version}/mods",
                    new ModSearchModel(mods, apiVersion, gameVersion, platform, includeExtendedMetadata)
                )
                .As<ModEntryModel[]>();

            return result.ToDictionary(p => p.ID);
        }

        /// <inheritdoc />
        public void Dispose()
        {
            this.Client.Dispose();
        }
    }
}