Skip to content

Commit a9fe274

Browse files
author
babelshift
committed
Removed the monolithic web session class.
Instead, callers are required to construct whatever steam web interface they want to use through the steam web factory. Refactored some interface/method classes to reduce repetition. Removed the "\\" replacement in JSON response since some players can have quotes in their names and this escape is needed to parse the JSON properly. Still don't know how to handle some of the API methods returning invalid JSON with wrong "\\" escapes.
1 parent 0178612 commit a9fe274

10 files changed

Lines changed: 119 additions & 134 deletions

SteamWebAPI2/CSGOServers.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
namespace SteamWebAPI2
1010
{
11-
internal class CSGOServers : SteamWebRequest
11+
public class CSGOServers : SteamWebInterface
1212
{
13-
public CSGOServers(SteamWebRequestParameter developerKey)
14-
: base(developerKey, "ICSGOServers_730") { }
13+
public CSGOServers(string steamWebApiKey)
14+
: base(steamWebApiKey, "ICSGOServers_730") { }
1515

1616
public async Task<ServerStatusResult> GetGameServerStatusAsync()
1717
{
18-
var gameServerStatus = await GetJsonAsync<ServerStatusResultContainer>(interfaceName, "GetGameServersStatus", 1);
18+
var gameServerStatus = await CallMethodAsync<ServerStatusResultContainer>("GetGameServersStatus", 1);
1919
return gameServerStatus.Result;
2020
}
2121
}

SteamWebAPI2/DOTA2Fantasy.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88

99
namespace SteamWebAPI2
1010
{
11-
internal class DOTA2Fantasy : SteamWebRequest
11+
public class DOTA2Fantasy : SteamWebInterface
1212
{
13-
public DOTA2Fantasy(SteamWebRequestParameter developerKey)
14-
: base(developerKey, "IDOTA2Fantasy_570") { }
13+
public DOTA2Fantasy(string steamWebApiKey)
14+
: base(steamWebApiKey, "IDOTA2Fantasy_570") { }
1515

1616
public async Task<PlayerOfficialInfoResult> GetPlayerOfficialInfo(long steamId)
1717
{
1818
List<SteamWebRequestParameter> parameters = new List<SteamWebRequestParameter>();
1919
parameters.Add(new SteamWebRequestParameter("accountid", steamId.ToString()));
2020

21-
var gameServerStatus = await GetJsonAsync<PlayerOfficialInfoResultContainer>(interfaceName, "GetPlayerOfficialInfo", 1, parameters);
21+
var gameServerStatus = await CallMethodAsync<PlayerOfficialInfoResultContainer>("GetPlayerOfficialInfo", 1, parameters);
2222
return gameServerStatus.Result;
2323
}
2424

2525
public async Task<ProPlayerListResult> GetProPlayerList()
2626
{
27-
var proPlayerList = await GetJsonAsync<ProPlayerListResultContainer>(interfaceName, "GetProPlayerList", 1);
27+
var proPlayerList = await CallMethodAsync<ProPlayerListResultContainer>("GetProPlayerList", 1);
2828
return proPlayerList.Results;
2929
}
3030
}

SteamWebAPI2/DOTA2Match.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,48 @@
55

66
namespace SteamWebAPI2
77
{
8-
internal class DOTA2Match : SteamWebRequest
8+
public class DOTA2Match : SteamWebInterface
99
{
10-
public DOTA2Match(SteamWebRequestParameter developerKey)
11-
: base(developerKey, "IDOTA2Match_570")
12-
{ }
10+
public DOTA2Match(string steamWebApiKey)
11+
: base(steamWebApiKey, "IDOTA2Match_570")
12+
{
13+
}
1314

1415
public async Task<LeagueResult> GetLeagueListing()
1516
{
16-
var leagueListing = await GetJsonAsync<LeagueResultContainer>(interfaceName, "GetLeagueListing", 1);
17+
var leagueListing = await CallMethodAsync<LeagueResultContainer>("GetLeagueListing", 1);
1718
return leagueListing.Result;
1819
}
1920

20-
public async Task<IReadOnlyCollection<LiveLeagueGame>> GetLiveLeagueGames()
21+
public async Task<IReadOnlyCollection<LiveLeagueGame>> GetLiveLeagueGames(int? leagueId = null, long? matchId = null)
2122
{
22-
var liveLeagueGames = await GetJsonAsync<LiveLeagueGameResultContainer>(interfaceName, "GetLiveLeagueGames", 1);
23+
List<SteamWebRequestParameter> parameters = new List<SteamWebRequestParameter>();
24+
25+
if(leagueId.HasValue)
26+
{
27+
parameters.Add(new SteamWebRequestParameter("league_id", leagueId.Value.ToString()));
28+
}
29+
30+
if (matchId.HasValue)
31+
{
32+
parameters.Add(new SteamWebRequestParameter("match_id", matchId.Value.ToString()));
33+
}
34+
35+
var liveLeagueGames = await CallMethodAsync<LiveLeagueGameResultContainer>("GetLiveLeagueGames", 1, parameters);
2336
return new ReadOnlyCollection<LiveLeagueGame>(liveLeagueGames.Result.Games);
2437
}
2538

2639
public async Task<MatchDetailResult> GetMatchDetails(int matchId)
2740
{
2841
List<SteamWebRequestParameter> parameters = new List<SteamWebRequestParameter>();
2942
parameters.Add(new SteamWebRequestParameter("match_id", matchId.ToString()));
30-
var matchDetail = await GetJsonAsync<MatchDetailResultContainer>(interfaceName, "GetMatchDetails", 1, parameters);
43+
var matchDetail = await CallMethodAsync<MatchDetailResultContainer>("GetMatchDetails", 1, parameters);
3144
return matchDetail.Result;
3245
}
3346

3447
public async Task<MatchHistoryResult> GetMatchHistory()
3548
{
36-
var matchHistory = await GetJsonAsync<MatchHistoryResultContainer>(interfaceName, "GetMatchHistory", 1);
49+
var matchHistory = await CallMethodAsync<MatchHistoryResultContainer>("GetMatchHistory", 1);
3750
return matchHistory.Result;
3851
}
3952
}

SteamWebAPI2/SteamUser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
namespace SteamWebAPI2
99
{
10-
internal class SteamUser : SteamWebRequest
10+
public class SteamUser : SteamWebInterface
1111
{
12-
public SteamUser(SteamWebRequestParameter developerKey)
13-
: base(developerKey, "ISteamUser") { }
12+
public SteamUser(string steamWebApiKey)
13+
: base(steamWebApiKey, "ISteamUser") { }
1414

1515
public async Task<PlayerSummary> GetPlayerSummaryAsync(string steamId)
1616
{
1717
List<SteamWebRequestParameter> parameters = new List<SteamWebRequestParameter>();
1818
parameters.Add(new SteamWebRequestParameter("steamids", steamId));
19-
var playerSummary = await GetJsonAsync<PlayerSummaryResponseContainer>(interfaceName, "GetPlayerSummaries", 2, parameters);
19+
var playerSummary = await CallMethodAsync<PlayerSummaryResponseContainer>("GetPlayerSummaries", 2, parameters);
2020

2121
if (playerSummary.Response.Players.Count > 0)
2222
{

SteamWebAPI2/SteamWebAPI2.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
<Compile Include="SteamUser.cs" />
5757
<Compile Include="SteamWebAPIUtil.cs" />
5858
<Compile Include="Properties\AssemblyInfo.cs" />
59-
<Compile Include="SteamWebSession.cs" />
59+
<Compile Include="SteamWebFactory.cs" />
60+
<Compile Include="SteamWebInterface.cs" />
6061
<Compile Include="SteamWebRequest.cs" />
6162
<Compile Include="SteamWebRequestParameter.cs" />
6263
</ItemGroup>

SteamWebAPI2/SteamWebAPIUtil.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88

99
namespace SteamWebAPI2
1010
{
11-
internal class SteamWebAPIUtil : SteamWebRequest
11+
public class SteamWebAPIUtil : SteamWebInterface
1212
{
13-
public SteamWebAPIUtil(SteamWebRequestParameter developerKey)
14-
: base(developerKey, "ISteamWebAPIUtil") { }
13+
public SteamWebAPIUtil(string steamWebApiKey)
14+
: base(steamWebApiKey, "ISteamWebAPIUtil") { }
1515

1616
public async Task<SteamServerInfo> GetServerInfoAsync()
1717
{
18-
var steamServerInfo = await GetJsonAsync<SteamServerInfo>(interfaceName, "GetServerInfo", 1);
18+
var steamServerInfo = await CallMethodAsync<SteamServerInfo>("GetServerInfo", 1);
1919
return steamServerInfo;
2020
}
2121

2222
public async Task<IReadOnlyCollection<SteamInterface>> GetSupportedAPIListAsync()
2323
{
24-
var steamApiListContainer = await GetJsonAsync<SteamApiListContainer>(interfaceName, "GetSupportedAPIList", 1);
24+
var steamApiListContainer = await CallMethodAsync<SteamApiListContainer>("GetSupportedAPIList", 1);
2525
return new ReadOnlyCollection<SteamInterface>(steamApiListContainer.ApiList.Interfaces);
2626
}
2727
}

SteamWebAPI2/SteamWebFactory.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SteamWebAPI2
8+
{
9+
public class SteamWebFactory
10+
{
11+
private string steamWebApiKey;
12+
13+
public SteamWebFactory(string steamWebApiKey)
14+
{
15+
if (String.IsNullOrEmpty(steamWebApiKey))
16+
{
17+
throw new ArgumentNullException("steamWebApiKey");
18+
}
19+
20+
this.steamWebApiKey = steamWebApiKey;
21+
}
22+
23+
public T Create<T>() where T : class
24+
{
25+
if(String.IsNullOrEmpty(steamWebApiKey))
26+
{
27+
throw new InvalidOperationException("Steam Web API key is invalid or missing. Did you initialize it?");
28+
}
29+
30+
return Activator.CreateInstance(typeof(T), steamWebApiKey) as T;
31+
}
32+
}
33+
}

SteamWebAPI2/SteamWebInterface.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace SteamWebAPI2
9+
{
10+
public class SteamWebInterface
11+
{
12+
private readonly SteamWebRequest steamWebRequest;
13+
private readonly string interfaceName;
14+
15+
public SteamWebInterface(string steamWebApiKey, string interfaceName)
16+
{
17+
Debug.Assert(!String.IsNullOrEmpty(interfaceName));
18+
19+
this.interfaceName = interfaceName;
20+
this.steamWebRequest = new SteamWebRequest(steamWebApiKey);
21+
}
22+
23+
internal async Task<T> CallMethodAsync<T>(string methodName, int version, IList<SteamWebRequestParameter> parameters = null)
24+
{
25+
Debug.Assert(!String.IsNullOrEmpty(methodName));
26+
Debug.Assert(version > 0);
27+
28+
return await steamWebRequest.GetJsonAsync<T>(interfaceName, methodName, version, parameters);
29+
}
30+
}
31+
}

SteamWebAPI2/SteamWebRequest.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,21 @@
1111

1212
namespace SteamWebAPI2
1313
{
14-
internal abstract class SteamWebRequest
14+
internal class SteamWebRequest
1515
{
16-
protected readonly string interfaceName;
17-
private SteamWebRequestParameter developerKey;
16+
private string steamWebApiKey;
1817

19-
public SteamWebRequest(SteamWebRequestParameter developerKey, string interfaceName)
18+
public SteamWebRequest(string steamWebApiKey)
2019
{
21-
// we assert here because SteamWebRequest is never constructed by the application, instead it is constructed by SteamWebSession (controlled by this library)
22-
Debug.Assert(developerKey != null);
23-
24-
// we assert here because the name is never determined by the caller and should never be null or empty
25-
Debug.Assert(!String.IsNullOrEmpty(developerKey.Name));
26-
27-
if (String.IsNullOrEmpty(developerKey.Value))
20+
if(String.IsNullOrEmpty(steamWebApiKey))
2821
{
29-
throw new ArgumentNullException("Steam Web API developer key value cannot be null or empty.");
22+
throw new ArgumentNullException("steamWebApiKey");
3023
}
3124

32-
this.developerKey = developerKey;
33-
34-
Debug.Assert(!String.IsNullOrEmpty(interfaceName));
35-
36-
this.interfaceName = interfaceName;
25+
this.steamWebApiKey = steamWebApiKey;
3726
}
3827

39-
protected async Task<T> GetJsonAsync<T>(string interfaceName, string methodName, int methodVersion)
28+
public async Task<T> GetJsonAsync<T>(string interfaceName, string methodName, int methodVersion)
4029
{
4130
Debug.Assert(!String.IsNullOrEmpty(interfaceName));
4231
Debug.Assert(!String.IsNullOrEmpty(methodName));
@@ -45,7 +34,7 @@ protected async Task<T> GetJsonAsync<T>(string interfaceName, string methodName,
4534
return await GetJsonAsync<T>(interfaceName, methodName, methodVersion, null);
4635
}
4736

48-
protected async Task<T> GetJsonAsync<T>(string interfaceName, string methodName, int methodVersion, IList<SteamWebRequestParameter> parameters)
37+
public async Task<T> GetJsonAsync<T>(string interfaceName, string methodName, int methodVersion, IList<SteamWebRequestParameter> parameters)
4938
{
5039
Debug.Assert(!String.IsNullOrEmpty(interfaceName));
5140
Debug.Assert(!String.IsNullOrEmpty(methodName));
@@ -56,18 +45,20 @@ protected async Task<T> GetJsonAsync<T>(string interfaceName, string methodName,
5645
parameters = new List<SteamWebRequestParameter>();
5746
}
5847

59-
parameters.Insert(0, developerKey);
48+
parameters.Insert(0, new SteamWebRequestParameter("key", steamWebApiKey));
6049

6150
string command = BuildRequestCommand(interfaceName, methodName, methodVersion, parameters);
6251

6352
HttpClient httpClient = new HttpClient();
6453
string response = await httpClient.GetStringAsync(command);
54+
//byte[] responseBytes = Encoding.Default.GetBytes(response);
55+
//string utf8response = Encoding.UTF8.GetString(responseBytes);
6556

6657
// some of the responses contain invalid json so we have to correct for it here
6758
// this is probably not the best way to do this since we have to iterate and copy over the entire string, but it works for now
6859
response = response.Replace("\n", "");
6960
response = response.Replace("\t", "");
70-
response = response.Replace("\\", "");
61+
//response = response.Replace("\\", "");
7162
response = response.Replace("\"{", "{");
7263
response = response.Replace("\"}", "}");
7364

@@ -84,7 +75,7 @@ protected async Task<T> GetJsonAsync<T>(string interfaceName, string methodName,
8475
/// <param name="methodVersion">Example: 1</param>
8576
/// <param name="parameters">Example: { key: 8A05823474AB641D684EBD95AB5F2E47 } </param>
8677
/// <returns></returns>
87-
private string BuildRequestCommand(string interfaceName, string methodName, int methodVersion, IList<SteamWebRequestParameter> parameters)
78+
public string BuildRequestCommand(string interfaceName, string methodName, int methodVersion, IList<SteamWebRequestParameter> parameters)
8879
{
8980
Debug.Assert(!String.IsNullOrEmpty(interfaceName));
9081
Debug.Assert(!String.IsNullOrEmpty(methodName));

SteamWebAPI2/SteamWebSession.cs

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)