SteamGameTimeTrack/SteamApiWrapper/SteamApiWrapper.cs
2025-05-25 18:36:59 +02:00

106 lines
3.6 KiB
C#

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 ();
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 GetOwnedGames GetOwnedGames(ulong steamid)
{
HttpRequestMessage request = BuildRequest(SupportedAPIInterface.IPlayerService, "GetOwnedGames", opts: new()
{
{"steamid", steamid.ToString()},
{"include_appinfo", "true"},
{"include_played_free_games", "true"}
});
try
{
HttpResponseMessage response = client.Send(request);
response.EnsureSuccessStatusCode();
JObject jObj = JObject.Parse(response.Content.ReadAsStringAsync().Result);
return jObj["response"]?.ToObject<GetOwnedGames>()??new(0, []);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return new(0, []);
}
}
public static GetPlayerSummaries GetPlayerSummaries(ulong[] steamids)
{
HttpRequestMessage request = BuildRequest(SupportedAPIInterface.ISteamUser, "GetPlayerSummaries", "v0002", opts: new()
{
{"steamids", string.Join(',', steamids)}
});
try
{
HttpResponseMessage response = client.Send(request);
response.EnsureSuccessStatusCode();
JObject jObj = JObject.Parse(response.Content.ReadAsStringAsync().Result);
return jObj["response"]?["players"]?.ToObject<GetPlayerSummaries>()??new([]);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return new([]);
}
}
public static GetRecentlyPlayedGames GetRecentlyPlayedGames(ulong steamid)
{
HttpRequestMessage request = BuildRequest(SupportedAPIInterface.IPlayerService, "GetRecentlyPlayedGames", opts: new()
{
{"steamid", steamid.ToString()}
});
try
{
HttpResponseMessage response = client.Send(request);
response.EnsureSuccessStatusCode();
JObject jObj = JObject.Parse(response.Content.ReadAsStringAsync().Result);
return jObj["response"]?.ToObject<GetRecentlyPlayedGames>()??new(0, []);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return new(0, []);
}
}
public static GetSupportedAPIList GetSupportedAPIList()
{
HttpRequestMessage request = BuildRequest(SupportedAPIInterface.ISteamWebAPIUtil, "GetSupportedAPIList");
try
{
HttpResponseMessage response = client.Send(request);
response.EnsureSuccessStatusCode();
JObject jObj = JObject.Parse(response.Content.ReadAsStringAsync().Result);
return jObj["apilist"]?.ToObject<GetSupportedAPIList>()??new([]);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return new([]);
}
}
}