Skip to content

Commit 3f48c1a

Browse files
committed
retrieve json data from metaforge commands
1 parent 5ae2649 commit 3f48c1a

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

src/DCLPulseTestClient/Auth/MetaForgeAuthenticator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public class MetaForgeAuthenticator : IAuthenticator
99
public async Task<string> LoginAsync(string account, CancellationToken ct)
1010
{
1111
await MetaForge.RunCommandAsync($"account create {account} --skip-update-check --skip-auto-login", ct);
12-
12+
1313
var output = await MetaForge.RunCommandAsync(
14-
$"account chain {account} --method connect --path / --metadata {{}} --skip-update-check", ct);
14+
$"account chain {account} --method connect --path / --metadata {{}} --skip-update-check --json", ct);
1515

1616
var options = new JsonSerializerOptions
1717
{
@@ -34,4 +34,4 @@ public async Task<string> LoginAsync(string account, CancellationToken ct)
3434

3535
return result.ToJsonString();
3636
}
37-
}
37+
}
Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,54 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
13
using System.Text.RegularExpressions;
24

35
namespace PulseTestClient.Profiles;
46

57
public class MetaForgeProfileGateway : IProfileGateway
68
{
9+
private static readonly JsonSerializerOptions JsonOptions = new()
10+
{
11+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
12+
};
13+
714
public async Task<Profile> GetAsync(string account, CancellationToken ct)
815
{
9-
var raw = await MetaForge.RunCommandAsync($"account info {account}", ct);
10-
var output = Regex.Replace(raw, @"\x1B\[[0-9;]*m", "");
16+
var json = await MetaForge.RunCommandAsync($"account info {account} --json", ct);
1117

12-
var address = Regex.Match(output, @"Address:\s+(0x[0-9a-fA-F]+)").Groups[1].Value;
13-
var version = int.Parse(Regex.Match(output, @"Version\s+(\d+)").Groups[1].Value);
18+
var response = JsonSerializer.Deserialize<ProfileResponse>(json, JsonOptions)!;
19+
var avatar = response.Metadata.Avatars[0];
1420

15-
// Match emote rows from the right-side table (lines ending with │ <item> │ │)
16-
var emotes = Regex.Matches(output, @"│\s+(\w+)\s+│\s+│\s*$", RegexOptions.Multiline)
17-
.Select(m => m.Groups[1].Value)
18-
.Where(v => v != "Item")
21+
var emotes = avatar.Avatar.Emotes
22+
.Select(e => e.Urn)
1923
.ToArray();
2024

21-
return new Profile(new Web3Address(address), version, emotes);
25+
return new Profile(new Web3Address(avatar.EthAddress), avatar.Version, emotes);
2226
}
23-
}
27+
}
28+
29+
file class ProfileResponse
30+
{
31+
[JsonPropertyName("metadata")] public ProfileMetadata Metadata { get; set; } = null!;
32+
}
33+
34+
file class ProfileMetadata
35+
{
36+
[JsonPropertyName("avatars")] public List<AvatarEntry> Avatars { get; set; } = [];
37+
}
38+
39+
file class AvatarEntry
40+
{
41+
[JsonPropertyName("ethAddress")] public string EthAddress { get; set; } = "";
42+
[JsonPropertyName("version")] public int Version { get; set; }
43+
[JsonPropertyName("avatar")] public AvatarData Avatar { get; set; } = null!;
44+
}
45+
46+
file class AvatarData
47+
{
48+
[JsonPropertyName("emotes")] public List<EmoteEntry> Emotes { get; set; } = [];
49+
}
50+
51+
file class EmoteEntry
52+
{
53+
[JsonPropertyName("urn")] public string Urn { get; set; } = "";
54+
}

0 commit comments

Comments
 (0)