This commit is contained in:
2025-05-25 18:36:59 +02:00
parent e86a49d5f6
commit 90d7281050
29 changed files with 321 additions and 959 deletions

View File

@ -0,0 +1,3 @@
namespace SteamApiWrapper.ReturnTypes;
public record Game(ulong appid, ulong playtime_forever, string name);

View File

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

View File

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

View File

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

View File

@ -0,0 +1,3 @@
namespace SteamApiWrapper.ReturnTypes;
public record GetSupportedAPIList(SupportedAPIInterface[] Interfaces) : IReturnType;

View File

@ -0,0 +1,6 @@
namespace SteamApiWrapper.ReturnTypes;
public interface IReturnType
{
}

View File

@ -0,0 +1,27 @@
namespace SteamApiWrapper.ReturnTypes;
public record Player(ulong steamid,
CommunityVisibilityState communityvisibilitystate,
bool profilestate,
string personaname,
string profileurl,
string avatar,
PersonaState personastate,
string? realname);
public enum PersonaState : byte
{
Offline = 0,
Online = 1,
Busy = 2,
Away = 3,
Snooze = 4,
LookingToTrade = 5,
LookingToPlay = 6
}
public enum CommunityVisibilityState : byte
{
Hidden = 1,
Public = 3
}

View File

@ -0,0 +1,13 @@
namespace SteamApiWrapper.ReturnTypes;
public struct SupportedAPIInterface(string name, SupportedAPIMethod[]? methods = null)
{
public readonly string Name = name;
public readonly SupportedAPIMethod[]? Methods = methods;
public override string ToString() => Name;
public static SupportedAPIInterface IPlayerService = new("IPlayerService");
public static SupportedAPIInterface ISteamWebAPIUtil = new("ISteamWebAPIUtil");
public static SupportedAPIInterface ISteamUser = new("ISteamUser");
}

View File

@ -0,0 +1,7 @@
namespace SteamApiWrapper.ReturnTypes;
public struct SupportedAPIMethod(string name, int version)
{
public string Name = name;
public int Version = version;
}

View File

@ -0,0 +1,106 @@
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([]);
}
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>