This commit is contained in:
2025-05-26 01:44:26 +02:00
parent ebddd3c3ed
commit 1a08f932af
23 changed files with 526 additions and 256 deletions

View File

@ -1,3 +0,0 @@
namespace SteamApiWrapper.ReturnTypes;
public record GetOwnedGames(uint game_count, Game[] games) : IReturnType;

View File

@ -1,3 +0,0 @@
namespace SteamApiWrapper.ReturnTypes;
public record GetPlayerSummaries(Player[] players) : IReturnType;

View File

@ -1,3 +1,4 @@
using log4net;
using Newtonsoft.Json.Linq;
using SteamApiWrapper.ReturnTypes;
@ -5,9 +6,10 @@ namespace SteamApiWrapper;
public static class SteamApiWrapper
{
public const string ApiUrl = "http://api.steampowered.com/";
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)
{
@ -25,81 +27,93 @@ public static class SteamApiWrapper
return ret;
}
public static GetOwnedGames GetOwnedGames(ulong steamid)
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);
return jObj["response"]?.ToObject<GetOwnedGames>()??new(0, []);
Log.Debug(jObj);
return jObj["response"]?["games"]?.ToObject<Game[]>()??[];
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return new(0, []);
Log.Error(e);
return [];
}
}
public static GetPlayerSummaries GetPlayerSummaries(ulong[] steamids)
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);
return jObj["response"]?["players"]?.ToObject<GetPlayerSummaries>()??new([]);
Log.Debug(jObj);
return jObj["response"]?["players"]?.ToObject<Player[]>()??[];
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return new([]);
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)
{
Console.WriteLine(e.Message);
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)
{
Console.WriteLine(e.Message);
Log.Error(e);
return new([]);
}
}

View File

@ -8,6 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="3.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>