Skip to content

Commit ef8ad55

Browse files
committed
return array
1 parent 1520546 commit ef8ad55

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

MojangAPI/Mojang.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,25 @@ public Task<UserUUID> GetUUID(string username, long timestamp) =>
6161
}
6262
});
6363

64-
public Task<NameHistoryResponse> GetNameHistories(string uuid) =>
65-
client.SendActionAsync(new HttpAction<NameHistoryResponse>
64+
public Task<NameHistory[]?> GetNameHistories(string uuid) =>
65+
client.SendActionAsync(new HttpAction<NameHistory[]?>
6666
{
6767
Method = HttpMethod.Get,
6868
Host = "https://api.mojang.com",
6969
Path = $"user/profiles/{uuid?.Replace("-", "") ?? throw new ArgumentNullException(nameof(uuid))}/names",
70-
ResponseHandler = async (response) =>
71-
{
72-
var handler = HttpResponseHandlers.GetJsonArrayHandler<NameHistory>();
73-
var histories = await handler.Invoke(response);
74-
return new NameHistoryResponse(histories);
75-
},
76-
ErrorHandler = HttpResponseHandlers.GetJsonErrorHandler<NameHistoryResponse>()
70+
ResponseHandler = HttpResponseHandlers.GetJsonArrayHandler<NameHistory>(),
71+
ErrorHandler = (res, ex) => Task.FromResult<NameHistory[]?>(null)
7772
});
7873

79-
public Task<UserUUID[]> GetUUIDs(string[] usernames) =>
80-
client.SendActionAsync(new HttpAction<UserUUID[]>
74+
public Task<UserUUID[]?> GetUUIDs(string[] usernames) =>
75+
client.SendActionAsync(new HttpAction<UserUUID[]?>
8176
{
8277
Method = HttpMethod.Post,
8378
Host = "https://api.mojang.com",
8479
Path = "profiles/minecraft",
8580
Content = new JsonHttpContent(usernames ?? throw new ArgumentNullException()),
86-
ResponseHandler = HttpResponseHandlers.GetJsonArrayHandler<UserUUID>()
81+
ResponseHandler = HttpResponseHandlers.GetJsonArrayHandler<UserUUID>(),
82+
ErrorHandler = (res, ex) => Task.FromResult<UserUUID[]?>(null)
8783
});
8884

8985
public Task<UserProfile> GetProfileUsingUUID(string uuid) =>

MojangAPISample/Program.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ static async Task testMojangAPIs(Mojang mojang, Session session)
4040
Console.WriteLine($"UUID: {uuid.UUID}, IsLegacy: {uuid.IsLegacy}, IsDemo: {uuid.IsDemo}\n");
4141

4242
Console.WriteLine("GetNameHistories");
43-
NameHistoryResponse historyRes = await mojang.GetNameHistories(session.UUID);
44-
printResponse(historyRes);
45-
if (historyRes.IsSuccess)
43+
NameHistory[] historyRes = await mojang.GetNameHistories(session.UUID);
44+
if (historyRes != null)
4645
{
47-
foreach (var item in historyRes.Histories)
46+
foreach (var item in historyRes)
4847
{
4948
Console.WriteLine($"[{item.ChangedTime}] {item.Name}");
5049
}
5150
}
51+
else
52+
Console.WriteLine("failed");
5253
Console.WriteLine();
5354

5455
Console.WriteLine("GetProfileUsingUUID");

0 commit comments

Comments
 (0)