using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using StardewModdingAPI.Models;
namespace StardewModdingAPI.Framework
{
/// Provides methods for interacting with the SMAPI web API.
internal class WebApiClient
{
/*********
** Properties
*********/
/// The base URL for the web API.
private readonly Uri BaseUrl;
/// The API version number.
private readonly ISemanticVersion Version;
/*********
** Public methods
*********/
/// Construct an instance.
/// The base URL for the web API.
/// The web API version.
public WebApiClient(string baseUrl, ISemanticVersion version)
{
#if !SMAPI_FOR_WINDOWS
baseUrl = baseUrl.Replace("https://", "http://"); // workaround for OpenSSL issues with the game's bundled Mono on Linux/Mac
#endif
this.BaseUrl = new Uri(baseUrl);
this.Version = version;
}
/// Get the latest SMAPI version.
/// The mod keys for which to fetch the latest version.
public async Task> GetModInfoAsync(params string[] modKeys)
{
return await this.PostAsync>(
$"v{this.Version}/mods",
new ModSearchModel(modKeys)
);
}
/*********
** Private methods
*********/
/// Fetch the response from the backend API.
/// The body content type.
/// The expected response type.
/// The request URL, optionally excluding the base URL.
/// The body content to post.
private async Task PostAsync(string url, TBody content)
{
/***
** Note: avoid HttpClient for Mac compatibility.
***/
// serialise content
byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content));
// build request
HttpWebRequest request = WebRequest.CreateHttp(new Uri(this.BaseUrl, url).ToString());
request.Method = "POST";
request.UserAgent = $"SMAPI/{this.Version}";
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (Stream bodyStream = request.GetRequestStream())
bodyStream.Write(data, 0, data.Length);
// fetch data
using (WebResponse response = await request.GetResponseAsync())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
{
string responseText = reader.ReadToEnd();
return JsonConvert.DeserializeObject(responseText);
}
}
}
}