-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
207 additions
and
23 deletions.
There are no files selected for viewing
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,11 @@ | ||
namespace UnofficialArcaeaAPI.Lib.Core; | ||
|
||
public sealed class UaaImageApi | ||
{ | ||
public UaaImageUserApi User { get; } | ||
|
||
internal UaaImageApi(HttpClient client) | ||
{ | ||
User = new UaaImageUserApi(client); | ||
} | ||
} |
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,177 @@ | ||
using System.Text.Json; | ||
using UnofficialArcaeaAPI.Lib.Models; | ||
using UnofficialArcaeaAPI.Lib.Responses; | ||
using UnofficialArcaeaAPI.Lib.Utils; | ||
|
||
namespace UnofficialArcaeaAPI.Lib.Core; | ||
|
||
public sealed class UaaImageUserApi | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
internal UaaImageUserApi(HttpClient client) | ||
{ | ||
_client = client; | ||
} | ||
|
||
#region /user/info | ||
|
||
private async Task<byte[]> GetInfoAsyncCore(string? user, int? userCode, int recent, | ||
AuaReplyWith replyWith) | ||
{ | ||
var qb = new QueryBuilder() | ||
.Add("recent", recent.ToString()); | ||
|
||
if (user is not null) | ||
qb.Add("user_name", user); | ||
else | ||
qb.Add("user_code", userCode.ToString()!); | ||
|
||
if (replyWith.HasFlag(AuaReplyWith.SongInfo)) | ||
qb.Add("with_song_info", "true"); | ||
var resp = await _client.GetAsync("image/user/info" + qb.Build()); | ||
return await resp.EnsureDataSuccess(); | ||
} | ||
|
||
/// <summary> | ||
/// Get user info image. | ||
/// </summary> | ||
/// <param name="user">User name or 9-digit user code</param> | ||
/// <param name="recent">The number of recently played songs expected, range 0-7</param> | ||
/// <param name="replyWith">Additional information to reply with. Supports songinfo only.</param> | ||
/// <returns>User info image</returns> | ||
public Task<byte[]> GetInfoAsync(string user, int recent = 0, | ||
AuaReplyWith replyWith = AuaReplyWith.None) | ||
=> GetInfoAsyncCore(user, null, recent, replyWith); | ||
|
||
/// <summary> | ||
/// Get user info image. | ||
/// </summary> | ||
/// <param name="userCode">9-digit user code</param> | ||
/// <param name="recent">The number of recently played songs expected, range 0-7</param> | ||
/// <param name="replyWith">Additional information to reply with. Supports songinfo only.</param> | ||
/// <returns>User info image</returns> | ||
public Task<byte[]> GetInfoAsync(int userCode, int recent = 0, | ||
AuaReplyWith replyWith = AuaReplyWith.None) | ||
=> GetInfoAsyncCore(null, userCode, recent, replyWith); | ||
|
||
/// <summary> | ||
/// Get user info image. | ||
/// </summary> | ||
/// <param name="user">User name or 9-digit user code</param> | ||
/// <param name="replyWith">Additional information to reply with. Supports songinfo only.</param> | ||
/// <returns>User info image</returns> | ||
public Task<byte[]> GetInfoAsync(string user, AuaReplyWith replyWith) | ||
=> GetInfoAsyncCore(user, null, 0, replyWith); | ||
|
||
/// <summary> | ||
/// Get user info image. | ||
/// </summary> | ||
/// <param name="userCode">9-digit user code</param> | ||
/// <param name="replyWith">Additional information to reply with. Supports songinfo only.</param> | ||
/// <returns>User info image</returns> | ||
public Task<byte[]> GetInfoAsync(int userCode, AuaReplyWith replyWith) | ||
=> GetInfoAsyncCore(null, userCode, 0, replyWith); | ||
|
||
#endregion /user/info | ||
|
||
#region /user/best | ||
|
||
private async Task<byte[]> GetBestAsyncCore(string? user, int? userCode, string songname, | ||
AuaSongQueryType queryType, ArcaeaDifficulty difficulty, AuaReplyWith replyWith) | ||
{ | ||
var qb = new QueryBuilder() | ||
.Add(queryType == AuaSongQueryType.SongId ? "song_id" : "song_name", songname) | ||
.Add("difficulty", ((int)difficulty).ToString()); | ||
|
||
if (user is not null) | ||
qb.Add("user_name", user); | ||
else | ||
qb.Add("user_code", userCode.ToString()!); | ||
|
||
if (replyWith.HasFlag(AuaReplyWith.Recent)) | ||
qb.Add("with_recent", "true"); | ||
if (replyWith.HasFlag(AuaReplyWith.SongInfo)) | ||
qb.Add("with_song_info", "true"); | ||
|
||
var resp = await _client.GetAsync("user/best" + qb.Build()); | ||
return await resp.EnsureDataSuccess(); | ||
} | ||
|
||
/// <summary> | ||
/// Get user best score image. | ||
/// </summary> | ||
/// <param name="user">User name or 9-digit user code</param> | ||
/// <param name="songName">Any song name for fuzzy querying or sid in Arcaea songlist</param> | ||
/// <param name="queryType">Specify the query type between songname and songid</param> | ||
/// <param name="difficulty">Song difficulty</param> | ||
/// <param name="replyWith">Additional information to reply with. Supports songinfo and recent.</param> | ||
/// <returns>User best image</returns> | ||
public Task<byte[]> GetBestAsync(string user, string songName, | ||
AuaSongQueryType queryType = AuaSongQueryType.SongName, ArcaeaDifficulty difficulty = ArcaeaDifficulty.Future, | ||
AuaReplyWith replyWith = AuaReplyWith.None) | ||
=> GetBestAsyncCore(user, null, songName, queryType, difficulty, replyWith); | ||
|
||
/// <summary> | ||
/// Get user best score image. | ||
/// </summary> | ||
/// <param name="userCode">9-digit user code</param> | ||
/// <param name="songName">Any song name for fuzzy querying or sid in Arcaea songlist</param> | ||
/// <param name="queryType">Specify the query type between songname and songid</param> | ||
/// <param name="difficulty">Song difficulty</param> | ||
/// <param name="replyWith">Additional information to reply with. Supports songinfo and recent.</param> | ||
/// <returns>User best image</returns> | ||
public Task<byte[]> GetBestAsync(int userCode, string songName, | ||
AuaSongQueryType queryType = AuaSongQueryType.SongName, ArcaeaDifficulty difficulty = ArcaeaDifficulty.Future, | ||
AuaReplyWith replyWith = AuaReplyWith.None) | ||
=> GetBestAsyncCore(null, userCode, songName, queryType, difficulty, replyWith); | ||
|
||
/// <summary> | ||
/// Get user best score image. | ||
/// </summary> | ||
/// <param name="user">User name or 9-digit user code</param> | ||
/// <param name="songName">Any song name for fuzzy querying</param> | ||
/// <param name="difficulty">Song difficulty</param> | ||
/// <param name="replyWith">Additional information to reply with. Supports songinfo and recent.</param> | ||
/// <returns>User best image</returns> | ||
public Task<byte[]> GetBestAsync(string user, string songName, | ||
ArcaeaDifficulty difficulty, | ||
AuaReplyWith replyWith = AuaReplyWith.None) | ||
=> GetBestAsyncCore(user, null, songName, AuaSongQueryType.SongName, difficulty, replyWith); | ||
|
||
/// <summary> | ||
/// Get user best score image. | ||
/// </summary> | ||
/// <param name="userCode">9-digit user code</param> | ||
/// <param name="songName">Any song name for fuzzy querying</param> | ||
/// <param name="difficulty">Song difficulty</param> | ||
/// <param name="replyWith">Additional information to reply with. Supports songinfo and recent.</param> | ||
/// <returns>User best image</returns> | ||
public Task<byte[]> GetBestAsync(int userCode, string songName, | ||
ArcaeaDifficulty difficulty, | ||
AuaReplyWith replyWith = AuaReplyWith.None) | ||
=> GetBestAsyncCore(null, userCode, songName, AuaSongQueryType.SongName, difficulty, replyWith); | ||
|
||
/// <summary> | ||
/// Get user best score image. | ||
/// </summary> | ||
/// <param name="user">User name or 9-digit user code</param> | ||
/// <param name="songName">Any song name for fuzzy querying</param> | ||
/// <param name="replyWith">Additional information to reply with. Supports songinfo and recent.</param> | ||
/// <returns>User best image</returns> | ||
public Task<byte[]> GetBestAsync(string user, string songName, AuaReplyWith replyWith) | ||
=> GetBestAsyncCore(user, null, songName, AuaSongQueryType.SongName, ArcaeaDifficulty.Future, replyWith); | ||
|
||
/// <summary> | ||
/// Get user best score image. | ||
/// </summary> | ||
/// <param name="userCode">9-digit user code</param> | ||
/// <param name="songName">Any song name for fuzzy querying</param> | ||
/// <param name="replyWith">Additional information to reply with. Supports songinfo and recent.</param> | ||
/// <returns>User best image</returns> | ||
public Task<byte[]> GetBestAsync(int userCode, string songName, | ||
AuaReplyWith replyWith) | ||
=> GetBestAsyncCore(null, userCode, songName, AuaSongQueryType.SongName, ArcaeaDifficulty.Future, replyWith); | ||
|
||
#endregion /user/best | ||
} |
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 |
---|---|---|
@@ -1,14 +1,19 @@ | ||
using UnofficialArcaeaAPI.Lib.Responses; | ||
using System.Net; | ||
using System.Text.Json; | ||
using UnofficialArcaeaAPI.Lib.Responses; | ||
|
||
namespace UnofficialArcaeaAPI.Lib.Utils; | ||
|
||
internal static class ResponseExtensions | ||
{ | ||
/// <summary> | ||
/// Because these status code has additional data, we should | ||
/// </summary> | ||
internal static bool HasAdditionalData(this UaaResponse response) | ||
internal static async Task<byte[]> EnsureDataSuccess(this HttpResponseMessage resp) | ||
{ | ||
return response.Status is <= -31 and >= -33; | ||
if (resp.StatusCode != HttpStatusCode.OK) | ||
{ | ||
var errJson = JsonSerializer.Deserialize<UaaResponse>(await resp.Content.ReadAsStringAsync())!; | ||
throw new UaaRequestException(errJson.Status, errJson.Message!); | ||
} | ||
|
||
return await resp.Content.ReadAsByteArrayAsync(); | ||
} | ||
} |