Rework
This commit is contained in:
3
SteamApiWrapper/ReturnTypes/Game.cs
Normal file
3
SteamApiWrapper/ReturnTypes/Game.cs
Normal file
@ -0,0 +1,3 @@
|
||||
namespace SteamApiWrapper.ReturnTypes;
|
||||
|
||||
public record Game(ulong appid, ulong playtime_forever, string name);
|
3
SteamApiWrapper/ReturnTypes/GetOwnedGames.cs
Normal file
3
SteamApiWrapper/ReturnTypes/GetOwnedGames.cs
Normal file
@ -0,0 +1,3 @@
|
||||
namespace SteamApiWrapper.ReturnTypes;
|
||||
|
||||
public record GetOwnedGames(uint game_count, Game[] games) : IReturnType;
|
3
SteamApiWrapper/ReturnTypes/GetPlayerSummaries.cs
Normal file
3
SteamApiWrapper/ReturnTypes/GetPlayerSummaries.cs
Normal file
@ -0,0 +1,3 @@
|
||||
namespace SteamApiWrapper.ReturnTypes;
|
||||
|
||||
public record GetPlayerSummaries(Player[] players) : IReturnType;
|
3
SteamApiWrapper/ReturnTypes/GetRecentlyPlayedGames.cs
Normal file
3
SteamApiWrapper/ReturnTypes/GetRecentlyPlayedGames.cs
Normal file
@ -0,0 +1,3 @@
|
||||
namespace SteamApiWrapper.ReturnTypes;
|
||||
|
||||
public record GetRecentlyPlayedGames(uint total_count, Game[] games) : IReturnType;
|
3
SteamApiWrapper/ReturnTypes/GetSupportedAPIList.cs
Normal file
3
SteamApiWrapper/ReturnTypes/GetSupportedAPIList.cs
Normal file
@ -0,0 +1,3 @@
|
||||
namespace SteamApiWrapper.ReturnTypes;
|
||||
|
||||
public record GetSupportedAPIList(SupportedAPIInterface[] Interfaces) : IReturnType;
|
6
SteamApiWrapper/ReturnTypes/IReturnType.cs
Normal file
6
SteamApiWrapper/ReturnTypes/IReturnType.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace SteamApiWrapper.ReturnTypes;
|
||||
|
||||
public interface IReturnType
|
||||
{
|
||||
|
||||
}
|
27
SteamApiWrapper/ReturnTypes/Player.cs
Normal file
27
SteamApiWrapper/ReturnTypes/Player.cs
Normal 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
|
||||
}
|
13
SteamApiWrapper/ReturnTypes/SupportedAPIInterface.cs
Normal file
13
SteamApiWrapper/ReturnTypes/SupportedAPIInterface.cs
Normal 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");
|
||||
}
|
7
SteamApiWrapper/ReturnTypes/SupportedAPIMethod.cs
Normal file
7
SteamApiWrapper/ReturnTypes/SupportedAPIMethod.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace SteamApiWrapper.ReturnTypes;
|
||||
|
||||
public struct SupportedAPIMethod(string name, int version)
|
||||
{
|
||||
public string Name = name;
|
||||
public int Version = version;
|
||||
}
|
106
SteamApiWrapper/SteamApiWrapper.cs
Normal file
106
SteamApiWrapper/SteamApiWrapper.cs
Normal 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([]);
|
||||
}
|
||||
}
|
||||
}
|
14
SteamApiWrapper/SteamApiWrapper.csproj
Normal file
14
SteamApiWrapper/SteamApiWrapper.csproj
Normal 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>
|
Reference in New Issue
Block a user