Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Runtime/Api/AsobiVotes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ public class AsobiVotes
readonly AsobiClient _client;
internal AsobiVotes(AsobiClient client) => _client = client;

public async Task<string> ListForMatchAsync(string matchId)
public async Task<VoteListResponse> ListForMatchAsync(string matchId)
{
return await _client.Http.GetRaw($"/api/v1/matches/{matchId}/votes");
var raw = await _client.Http.GetRaw($"/api/v1/matches/{matchId}/votes");
return JsonHelper.ParseVoteList(raw);
}

public async Task<string> GetAsync(string voteId)
public async Task<Vote> GetAsync(string voteId)
{
return await _client.Http.GetRaw($"/api/v1/votes/{voteId}");
var raw = await _client.Http.GetRaw($"/api/v1/votes/{voteId}");
return JsonHelper.ParseVote(raw);
}
}
}
23 changes: 23 additions & 0 deletions Runtime/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ internal static MatchListResponse ParseMatchList(string json)
return new MatchListResponse { matches = matches };
}

internal static Vote ParseVote(string json)
{
var vote = JsonUtility.FromJson<Vote>(json);
vote.options = ExtractJsonField(json, "options");
vote.votes_cast = ExtractJsonField(json, "votes_cast");
vote.result = ExtractJsonField(json, "result");
vote.distribution = ExtractJsonField(json, "distribution");
return vote;
}

internal static VoteListResponse ParseVoteList(string json)
{
var votesJson = ExtractJsonField(json, "votes");
if (string.IsNullOrEmpty(votesJson))
return new VoteListResponse { votes = Array.Empty<Vote>() };

var items = SplitJsonArray(votesJson);
var votes = new Vote[items.Length];
for (int i = 0; i < items.Length; i++)
votes[i] = ParseVote(items[i]);
return new VoteListResponse { votes = votes };
}

internal static Notification ParseNotification(string json)
{
var notif = JsonUtility.FromJson<Notification>(json);
Expand Down
29 changes: 29 additions & 0 deletions Runtime/Models/VoteModels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace Asobi
{
[Serializable]
public class Vote
{
public string id;
public string match_id;
public string template;
public string method;
public string options;
public string votes_cast;
public string result;
public string distribution;
public float turnout;
public int eligible_count;
public int window_ms;
public string opened_at;
public string closed_at;
public string inserted_at;
}

[Serializable]
public class VoteListResponse
{
public Vote[] votes;
}
}
11 changes: 11 additions & 0 deletions Runtime/Models/VoteModels.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading