-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated SteamAPIService to be easier to abstract.
- Loading branch information
1 parent
60a58c6
commit 8993c17
Showing
17 changed files
with
345 additions
and
325 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Globalization; | ||
using Uncreated.Warfare.Util; | ||
|
||
namespace Uncreated.Warfare.Steam; | ||
public interface ISteamApiService | ||
{ | ||
/// <exception cref="SteamApiRequestException"/> | ||
Task<TResponse> ExecuteQueryAsync<TResponse>(SteamApiQuery query, CancellationToken token) where TResponse : notnull; | ||
} | ||
|
||
public class SteamApiRequestException : Exception | ||
{ | ||
public SteamApiRequestException(string message) : base(message) { } | ||
public SteamApiRequestException(string message, Exception innerException) : base(message, innerException) { } | ||
} | ||
|
||
public readonly struct SteamApiQuery | ||
{ | ||
public readonly string Interface; | ||
public readonly string Method; | ||
public readonly int Version; | ||
public readonly string? QueryString; | ||
public readonly float StartTimeout = 1f; | ||
|
||
public SteamApiQuery(string @interface, string method, int version, string? queryString) | ||
{ | ||
Interface = @interface; | ||
Method = method; | ||
Version = version; | ||
QueryString = queryString; | ||
} | ||
|
||
public string CreateUrl(string apiKey) | ||
{ | ||
int vLen = MathUtility.CountDigits(Version); | ||
int stringLen = SteamApiServiceExtensions.BaseSteamApiUrl.Length + Interface.Length + 1 + Method.Length + 2 + vLen + 5 + apiKey.Length; | ||
if (QueryString != null) | ||
stringLen += 1 + QueryString.Length; | ||
|
||
CreateUrlState state = default; | ||
state.APIKey = apiKey; | ||
state.Version = Version; | ||
state.Method = Method; | ||
state.Interface = Interface; | ||
state.VersionLength = vLen; | ||
state.Query = QueryString; | ||
|
||
return string.Create(stringLen, state, (span, state) => | ||
{ | ||
int index = 0; | ||
SteamApiServiceExtensions.BaseSteamApiUrl.AsSpan().CopyTo(span); | ||
index += SteamApiServiceExtensions.BaseSteamApiUrl.Length; | ||
|
||
state.Interface.AsSpan().CopyTo(span[index..]); | ||
index += state.Interface.Length; | ||
|
||
span[index++] = '/'; | ||
|
||
state.Method.AsSpan().CopyTo(span[index..]); | ||
index += state.Method.Length; | ||
|
||
span[index++] = '/'; | ||
span[index++] = 'v'; | ||
|
||
state.Version.TryFormat(span[index..], out _, "D", CultureInfo.InvariantCulture); | ||
index += state.VersionLength; | ||
|
||
span[index++] = '?'; | ||
span[index++] = 'k'; | ||
span[index++] = 'e'; | ||
span[index++] = 'y'; | ||
span[index++] = '='; | ||
|
||
state.APIKey.AsSpan().CopyTo(span[index..]); | ||
index += state.APIKey.Length; | ||
|
||
if (state.Query == null) | ||
return; | ||
|
||
span[index++] = '&'; | ||
state.Query.AsSpan().CopyTo(span[index..]); | ||
}); | ||
} | ||
private struct CreateUrlState | ||
{ | ||
public string Interface; | ||
public string Method; | ||
public string? Query; | ||
public string APIKey; | ||
public int Version; | ||
public int VersionLength; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"steam://{Interface}/{Method}/v{Version.ToString(CultureInfo.InvariantCulture)}"; | ||
} | ||
} |
Oops, something went wrong.