Skip to content

Commit

Permalink
Merge pull request #2 from KrystianLesniak/develop
Browse files Browse the repository at this point in the history
RC: 0.2.0-alpha
  • Loading branch information
KrystianLesniak authored Jul 10, 2023
2 parents 2e6b2b1 + 7346c71 commit 06ce25a
Show file tree
Hide file tree
Showing 37 changed files with 514 additions and 227 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,17 @@ class Program
#### Users
- ✅ GetAchievementsEarnedBetweenAsync(string user, DateTime from, DateTime to) - Get a list of achievements earned by a user between two dates.
- ✅ GetAchievementsEarnedOnDayAsync(string user, DateTime date) - Get a list of achievements earned by a user on a given date.
- GetGameInfoAndUserProgressAsync() - Get metadata about a game as well as a user's progress on that game.
- GetGameInfoAndUserProgressAsync() - Temporary disabled: [#46](https://github.com/RetroAchievements/retroachievements-api-js/issues/46) - Get metadata about a game as well as a user's progress on that game.
- ✅ GetUserAwardsAsync() - Get a list of a user's site awards/badges.
- ⏳ GetUserClaimsAsync() - Get a list of set claims made over the lifetime of a user.
- ⏳ GetUserCompletedGamesAsync() - Get hardcore and softcore completion metadata about games a user has played.
- ⏳ GetUserGameRankAndScoreAsync() - Get metadata about how a user has performed on a given game.
- ⏳ GetUserPointsAsync() - Get a user's total hardcore and softcore points.
- ⏳ GetUserProgressAsync() - Get a user's progress on a list of specified games.
- ⏳ GetUserRecentAchievementsAsync() - Get a list of achievements recently earned by the user.
- ⏳ GetUserRecentlyPlayedGamesAsync() - Get a list of games a user has recently played.
- ⏳ GetUserSummaryAsync() - Get a user's profile metadata.
- ✅ GetUserClaimsAsync() - Get a list of set claims made over the lifetime of a user.
- ✅ GetUserCompletedGamesAsync() - Get hardcore and softcore completion metadata about games a user has played.
- ✅ GetUserGameRankAndScoreAsync() - Get metadata about how a user has performed on a given game.
- ✅ GetUserPointsAsync() - Get a user's total points.
- ✅ GetUserRankAndScore() - Get a user's total points and rank.
- ✅ GetUserProgressAsync() - Get a user's progress on a list of specified games.
- ✅ GetUserRecentAchievementsAsync() - Get a list of achievements recently earned by the user.
- ❌ GetUserRecentlyPlayedGamesAsync() - Temporary disabled: [#46](https://github.com/RetroAchievements/retroachievements-api-js/issues/46) - Get a list of games a user has recently played.
- ❌ GetUserSummaryAsync() - Temporary disabled: [#46](https://github.com/RetroAchievements/retroachievements-api-js/issues/46) - Get a user's profile metadata.

#### Games
- ⏳ GetAchievementCountAsync() - Get the list of achievement IDs for a game.
Expand Down
2 changes: 1 addition & 1 deletion src/RetroAchievements.Api.ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
// await Task.Delay(500);

//}
var response = await client.SendAsync(new GetUserAwardsRequest("Retrokaiser"));
var response = await client.GetUserRecentAchievementsAsync(userName, 400);
};
13 changes: 13 additions & 0 deletions src/RetroAchievements.Api/Exceptions/ArgumentExceptionHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections;

namespace RetroAchievements.Api.Exceptions
{
internal static class ArgumentExceptionGuard
{
public static void ThrowIfEmpty<T>(IEnumerable<T> enumerable, string parameterName)
{
if(!enumerable.Any())
throw new ArgumentException($"{enumerable.GetType().Name} cannot be empty.", parameterName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSer
{
JsonTokenType.True => true,
JsonTokenType.False => false,
JsonTokenType.String => bool.TryParse(reader.GetString(), out var b) ? b : throw new JsonException(),
JsonTokenType.String => bool.TryParse(reader.GetString(), out var b) ? b :
reader.GetString() == "0" ? false :
reader.GetString() == "1" ? true :
throw new JsonException(),
JsonTokenType.Number => reader.TryGetInt64(out long l) ? Convert.ToBoolean(l) : reader.TryGetDouble(out double d) ? Convert.ToBoolean(d) : false,
_ => throw new JsonException(),
};
Expand Down
21 changes: 14 additions & 7 deletions src/RetroAchievements.Api/Internal/Utils/HttpClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ public static string PrepareRequestUrl(string apiUrl)
if (attr != null)
{
var value = property.GetValue(request);
values.Add(attr.Key, GetMemberStringValue(property, value, attr.Option));
values.Add(attr.Key, GetMemberStringValue(property.PropertyType, value, attr.Option));
}
}

return values;
}

private static string GetMemberStringValue(PropertyInfo p, object? propertyValue, CustomOption option)
private static string GetMemberStringValue(Type propertyType, object? propertyValue, CustomOption option)
{
if (propertyValue == null)
return string.Empty;

//Is DateTime
if (p.PropertyType == typeof(DateTime))
if (propertyType == typeof(DateTime))
{
if (option == CustomOption.DateTimeToUnixSeconds)
{
Expand All @@ -60,17 +60,24 @@ private static string GetMemberStringValue(PropertyInfo p, object? propertyValue
return ((DateTime)propertyValue).ToString("yyyy-MM-dd HH:mm:ss");
}
//Is Enum
else if (p.PropertyType.IsEnum)
else if (propertyType.IsEnum)
{
return ((int)propertyValue).ToString();

}
//Is IEnumerable
else if (p.PropertyType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(p.PropertyType))
else if (propertyType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(propertyType))
{
var prop = (IEnumerable<object?>)propertyValue;
//TODO: Add support for CSV: collect(explode(',', $gameCSV))
var enumerable = (IEnumerable)propertyValue;

var preparedPropList = new List<string>();
foreach(var enumerableItem in enumerable)
{
if (enumerableItem != null)
preparedPropList.Add(GetMemberStringValue(enumerableItem.GetType(), enumerableItem, option));
}

return string.Join(',', preparedPropList);
}

return propertyValue.ToString() ?? string.Empty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
using RetroAchievements.Api.Request;
using RetroAchievements.Api.Response.Users;
//using RetroAchievements.Api.Request;
//using RetroAchievements.Api.Response.Users;

namespace RetroAchievements.Api.Requests.Users
{
public record GetGameInfoAndUserProgressRequest : IRetroAchievementsRequest<GetGameInfoAndUserProgressResponse>
{
public string RequestEndpoint => "API_GetGameInfoAndUserProgress";
//namespace RetroAchievements.Api.Requests.Users
//{
// TODO: Request disabled until issue will be resolved: https://github.com/RetroAchievements/retroachievements-api-js/issues/46
// public record GetGameInfoAndUserProgressRequest : IRetroAchievementsRequest<GetGameInfoAndUserProgressResponse>
// {
// public string RequestEndpoint => "API_GetGameInfoAndUserProgress";

public GetGameInfoAndUserProgressRequest(int gameId, string username)
{
ArgumentNullException.ThrowIfNull(gameId, nameof(gameId));
ArgumentNullException.ThrowIfNull(username, nameof(username));
// public GetGameInfoAndUserProgressRequest(int gameId, string username)
// {
// ArgumentNullException.ThrowIfNull(gameId, nameof(gameId));
// ArgumentNullException.ThrowIfNull(username, nameof(username));

GameId = gameId;
Username = username;
}
// GameId = gameId;
// Username = username;
// }

[ApiInputKey("g")]
public int GameId { get; init; }
// [ApiInputKey("g")]
// public int GameId { get; init; }

[ApiInputKey("u")]
public string Username { get; init; }
}
}
// [ApiInputKey("u")]
// public string Username { get; init; }
// }
//}
31 changes: 16 additions & 15 deletions src/RetroAchievements.Api/Requests/Users/GetUserClaimsRequest.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
//using RetroAchievements.Api.Request;
using RetroAchievements.Api.Request;
using RetroAchievements.Api.Response.Users;

//namespace RetroAchievements.Api.Requests.Users
//{
// public record GetUserClaimsRequest : IRetroAchievementsRequest
// {
// public GetUserClaimsRequest(string username)
// {
// ArgumentNullException.ThrowIfNull(username, nameof(username));
namespace RetroAchievements.Api.Requests.Users
{
public record GetUserClaimsRequest : IRetroAchievementsRequest<GetUserClaimsResponse>
{
public GetUserClaimsRequest(string username)
{
ArgumentNullException.ThrowIfNull(username, nameof(username));

// Username = username;
// }
Username = username;
}

// public string RequestEndpoint => "API_GetUserClaims";
public string RequestEndpoint => "API_GetUserClaims";

// [ApiInputKey("u")]
// public string Username { get; init; }
// }
//}
[ApiInputKey("u")]
public string Username { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
//using RetroAchievements.Api.Request;
using RetroAchievements.Api.Request;
using RetroAchievements.Api.Response.Users;

//namespace RetroAchievements.Api.Requests.Users
//{
// public record GetUserCompletedGamesRequest : IRetroAchievementsRequest
// {
// public GetUserCompletedGamesRequest(string username)
// {
// ArgumentNullException.ThrowIfNull(username, nameof(username));
namespace RetroAchievements.Api.Requests.Users
{
public record GetUserCompletedGamesRequest : IRetroAchievementsRequest<GetUserCompletedGamesResponse>
{
public GetUserCompletedGamesRequest(string username)
{
ArgumentNullException.ThrowIfNull(username, nameof(username));

// Username = username;
// }
Username = username;
}

// public string RequestEndpoint => "API_GetUserCompletedGames";
public string RequestEndpoint => "API_GetUserCompletedGames";

// [ApiInputKey("u")]
// public string Username { get; init; }
// }
//}
[ApiInputKey("u")]
public string Username { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
//using RetroAchievements.Api.Request;
using RetroAchievements.Api.Request;
using RetroAchievements.Api.Response.Users;

//namespace RetroAchievements.Api.Requests.Users
//{
// public record GetUserGameRankAndScoreRequest : IRetroAchievementsRequest
// {
// public GetUserGameRankAndScoreRequest(string username, int gameId)
// {
// ArgumentNullException.ThrowIfNull(username, nameof(username));
namespace RetroAchievements.Api.Requests.Users
{
public record GetUserGameRankAndScoreRequest : IRetroAchievementsRequest<GetUserGameRankAndScoreResponse>
{
public GetUserGameRankAndScoreRequest(string username, int gameId)
{
ArgumentNullException.ThrowIfNull(username, nameof(username));

// Username = username;
// GameId = gameId;
// }
Username = username;
GameId = gameId;
}

// public string RequestEndpoint => "API_GetUserGameRankAndScore";
public string RequestEndpoint => "API_GetUserGameRankAndScore";

// [ApiInputKey("g")]
// public int GameId { get; init; }
[ApiInputKey("g")]
public int GameId { get; init; }

// [ApiInputKey("u")]
// public string Username { get; init; }
// }
//}
[ApiInputKey("u")]
public string Username { get; init; }
}
}
31 changes: 16 additions & 15 deletions src/RetroAchievements.Api/Requests/Users/GetUserPointsRequest.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
//using RetroAchievements.Api.Request;
using RetroAchievements.Api.Request;
using RetroAchievements.Api.Response.Users;

//namespace RetroAchievements.Api.Requests.Users
//{
// public record GetUserPointsRequest : IRetroAchievementsRequest
// {
// public GetUserPointsRequest(string username)
// {
// ArgumentNullException.ThrowIfNull(username, nameof(username));
namespace RetroAchievements.Api.Requests.Users
{
public record GetUserPointsRequest : IRetroAchievementsRequest<GetUserPointsResponse>
{
public GetUserPointsRequest(string username)
{
ArgumentNullException.ThrowIfNull(username, nameof(username));

// Username = username;
// }
Username = username;
}

// public string RequestEndpoint => "API_GetUserPoints";
public string RequestEndpoint => "API_GetUserPoints";

// [ApiInputKey("u")]
// public string Username { get; init; }
// }
//}
[ApiInputKey("u")]
public string Username { get; init; }
}
}
43 changes: 23 additions & 20 deletions src/RetroAchievements.Api/Requests/Users/GetUserProgressRequest.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
//using RetroAchievements.Api.Request;
using RetroAchievements.Api.Exceptions;
using RetroAchievements.Api.Request;
using RetroAchievements.Api.Response.Users;

//namespace RetroAchievements.Api.Requests.Users
//{
// public record GetUserProgressRequest : IRetroAchievementsRequest
// {
// public GetUserProgressRequest(string username, IEnumerable<int> gameIds)
// {
// ArgumentNullException.ThrowIfNull(username, nameof(username));
// ArgumentNullException.ThrowIfNull(gameIds, nameof(gameIds));

// Username = username;
// GameIds = gameIds;
// }
namespace RetroAchievements.Api.Requests.Users
{
public record GetUserProgressRequest : IRetroAchievementsRequest<GetUserProgressResponse>
{
public GetUserProgressRequest(string username, IEnumerable<int> gameIds)
{
ArgumentNullException.ThrowIfNull(username, nameof(username));
ArgumentNullException.ThrowIfNull(gameIds, nameof(gameIds));
ArgumentExceptionGuard.ThrowIfEmpty(gameIds, nameof(gameIds));

// public string RequestEndpoint => "API_GetUserProgress";
Username = username;
GameIds = gameIds;
}

// [ApiInputKey("u")]
// public string Username { get; init; }
public string RequestEndpoint => "API_GetUserProgress";

// //TODO: Add support for CSV: collect(explode(',', $gameCSV))
// [ApiInputKey("i")]
// public IEnumerable<int> GameIds { get; init; }
// }
//}
[ApiInputKey("u")]
public string Username { get; init; }

[ApiInputKey("i")]
public IEnumerable<int> GameIds { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using RetroAchievements.Api.Request;
using RetroAchievements.Api.Response.Users;

namespace RetroAchievements.Api.Requests.Users
{
public record GetUserRankAndPointsRequest : IRetroAchievementsRequest<GetUserRankAndPointsResponse>
{
public GetUserRankAndPointsRequest(string username)
{
ArgumentNullException.ThrowIfNull(username, nameof(username));

Username = username;
}

public string RequestEndpoint => "API_GetUserRankAndScore";

[ApiInputKey("u")]
public string Username { get; init; }
}
}
Loading

0 comments on commit 06ce25a

Please sign in to comment.