summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.Toolkit/Framework/Clients/WebApi/WebApiClient.cs
blob: 0ecd966441ce4f08bbeee1428183eb5d7654d0c9 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Newtonsoft.Json;
using StardewModdingAPI.Toolkit.Serialisation;

namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi
{
    /// <summary>Provides methods for interacting with the SMAPI web API.</summary>
    public class WebApiClient
    {
        /*********
        ** Properties
        *********/
        /// <summary>The base URL for the web API.</summary>
        private readonly Uri BaseUrl;

        /// <summary>The API version number.</summary>
        private readonly ISemanticVersion Version;

        /// <summary>The JSON serializer settings to use.</summary>
        private readonly JsonSerializerSettings JsonSettings = new JsonHelper().JsonSettings;


        /*********
        ** 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.BaseUrl = new Uri(baseUrl);
            this.Version = version;
        }

        /// <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="includeExtendedMetadata">Whether to include extended metadata for each mod.</param>
        public IDictionary<string, ModEntryModel> GetModInfo(ModSearchEntryModel[] mods, bool includeExtendedMetadata = false)
        {
            return this.Post<ModSearchModel, ModEntryModel[]>(
                $"v{this.Version}/mods",
                new ModSearchModel(mods, includeExtendedMetadata)
            ).ToDictionary(p => p.ID);
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Fetch the response from the backend API.</summary>
        /// <typeparam name="TBody">The body content type.</typeparam>
        /// <typeparam name="TResult">The expected response type.</typeparam>
        /// <param name="url">The request URL, optionally excluding the base URL.</param>
        /// <param name="content">The body content to post.</param>
        private TResult Post<TBody, TResult>(string url, TBody content)
        {
            // note: avoid HttpClient for Mac compatibility
            using (WebClient client = new WebClient())
            {
                Uri fullUrl = new Uri(this.BaseUrl, url);
                string data = JsonConvert.SerializeObject(content);

                client.Headers["Content-Type"] = "application/json";
                client.Headers["User-Agent"] = $"SMAPI/{this.Version}";
                string response = client.UploadString(fullUrl, data);
                return JsonConvert.DeserializeObject<TResult>(response, this.JsonSettings);
            }
        }
    }
}