2025-05-26 01:44:26 +02:00

120 lines
4.0 KiB
C#

using log4net;
using Newtonsoft.Json.Linq;
using SteamApiWrapper.ReturnTypes;
namespace SteamApiWrapper;
public static class SteamApiWrapper
{
public const string ApiUrl = "http://api.steampowered.com";
private static string _apiKey = string.Empty;
private static HttpClient client = new ();
private static ILog Log { get; } = LogManager.GetLogger(typeof(SteamApiWrapper));
public static void SetApiKey(string apiKey)
{
_apiKey = apiKey;
}
private static HttpRequestMessage BuildRequest(SupportedAPIInterface apiInterface, string methodName, string version = "v0001", Dictionary<string, string>? opts = null)
{
string url = $"{ApiUrl}/{apiInterface}/{methodName}/{version}?key={_apiKey}&format=json";
if (opts != null)
foreach ((string key, string value) in opts)
url += $"&{key}={value}";
HttpRequestMessage ret = new(HttpMethod.Get, url);
return ret;
}
public static Game[] GetOwnedGames(ulong steamid)
{
Log.Debug("GetOwnedGames");
HttpRequestMessage request = BuildRequest(SupportedAPIInterface.IPlayerService, "GetOwnedGames", opts: new()
{
{"steamid", steamid.ToString()},
{"include_appinfo", "true"},
{"include_played_free_games", "true"}
});
Log.Debug(request.ToString());
try
{
HttpResponseMessage response = client.Send(request);
response.EnsureSuccessStatusCode();
JObject jObj = JObject.Parse(response.Content.ReadAsStringAsync().Result);
Log.Debug(jObj);
return jObj["response"]?["games"]?.ToObject<Game[]>()??[];
}
catch (Exception e)
{
Log.Error(e);
return [];
}
}
public static Player[] GetPlayerSummaries(ulong[] steamids)
{
Log.Debug("GetPlayerSummaries");
HttpRequestMessage request = BuildRequest(SupportedAPIInterface.ISteamUser, "GetPlayerSummaries", "v0002", opts: new()
{
{"steamids", string.Join(',', steamids)}
});
Log.Debug(request.ToString());
try
{
HttpResponseMessage response = client.Send(request);
response.EnsureSuccessStatusCode();
JObject jObj = JObject.Parse(response.Content.ReadAsStringAsync().Result);
Log.Debug(jObj);
return jObj["response"]?["players"]?.ToObject<Player[]>()??[];
}
catch (Exception e)
{
Log.Error(e);
return [];
}
}
public static GetRecentlyPlayedGames GetRecentlyPlayedGames(ulong steamid)
{
Log.Debug("GetRecentlyPlayedGames");
HttpRequestMessage request = BuildRequest(SupportedAPIInterface.IPlayerService, "GetRecentlyPlayedGames", opts: new()
{
{"steamid", steamid.ToString()}
});
Log.Debug(request.ToString());
try
{
HttpResponseMessage response = client.Send(request);
response.EnsureSuccessStatusCode();
JObject jObj = JObject.Parse(response.Content.ReadAsStringAsync().Result);
Log.Debug(jObj);
return jObj["response"]?.ToObject<GetRecentlyPlayedGames>()??new(0, []);
}
catch (Exception e)
{
Log.Error(e);
return new(0, []);
}
}
public static GetSupportedAPIList GetSupportedAPIList()
{
Log.Debug("GetSupportedAPIList");
HttpRequestMessage request = BuildRequest(SupportedAPIInterface.ISteamWebAPIUtil, "GetSupportedAPIList");
Log.Debug(request.ToString());
try
{
HttpResponseMessage response = client.Send(request);
response.EnsureSuccessStatusCode();
JObject jObj = JObject.Parse(response.Content.ReadAsStringAsync().Result);
Log.Debug(jObj);
return jObj["apilist"]?.ToObject<GetSupportedAPIList>()??new([]);
}
catch (Exception e)
{
Log.Error(e);
return new([]);
}
}
}