Skip to content

Commit

Permalink
Start pulling patrol information
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnield committed Nov 26, 2023
1 parent faa5ccf commit d8058cf
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
12 changes: 10 additions & 2 deletions OSM.Models/Member.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ public class Member
public string Initial => LastName.Substring(0, 1) ?? string.Empty;
[JsonPropertyName("lastname")]
public string LastName { get; set; } = string.Empty;
[JsonPropertyName("patrolid")]
public int PatrolId { get; set; }

[JsonPropertyName("badges")]
public BadgeSummary[] Badges { get; set; }
[JsonPropertyName("patrolid")]
public int PatrolId { get; set; }
[JsonPropertyName("patrol_role_level")]
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public int PatrolRoleLevel { get; set; } = 0;
[JsonPropertyName("patrol_role_level_label")]
public string PatrolRole { get; set; } = string.Empty;
[JsonPropertyName("patrol_role_level_abbr")]
public string PatrolRoleAbbr { get; set; } = string.Empty;
}
14 changes: 14 additions & 0 deletions OSM.Models/Patrol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;

namespace OSM.Models;

public class Patrol
{
[JsonPropertyName("patrolid")]
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public int Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
[JsonPropertyName("members")]
public List<Member> Members { get; set; } = new();
}
11 changes: 11 additions & 0 deletions OSM.Tests/GetDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public async Task GetMembers()
Assert.NotNull(me);
}


[Fact]
public async Task GetPatrolsWithMembers()
{
var currentTerm = (await _client.GetTermsAsync()).ToArray().First(t => t.Current);

var patrols = (await _client.GetPatrols(currentTerm.Id)).ToArray();
Assert.NotNull(patrols);
Assert.NotEmpty(patrols);
}

[Fact]
public async Task BadgeCompletion()
{
Expand Down
1 change: 1 addition & 0 deletions OSM/Interfaces/IOsmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface IOsmClient
Task<IList<Member>> GetMembersAsync(string termId);
Task<IList<BadgeCompletion>> GetBadgeCompletion(string termId, string badgeId, string badgeVersion);
Task<IList<Member>> GetPersonBadgeSummaryAsync(string termId);
Task<IList<Patrol>> GetPatrols(string termId, bool includeNoPatrol = true);
}
20 changes: 20 additions & 0 deletions OSM/OsmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,24 @@ public async Task<IList<BadgeCompletion>> GetBadgeCompletion(string termId, stri
if(evaluateResult.Matches == null || !evaluateResult.Matches.Any()) return Array.Empty<Member>();
return evaluateResult.Matches.Select(match => JsonSerializer.Deserialize<Member>(match.Value.AsJsonString())).ToList();
}

public async Task<IList<Patrol>> GetPatrols(string termId, bool includeNoPatrol = true)
{

var request = new RestRequest("/ext/members/patrols/");
request.Parameters.AddParameter(new QueryParameter("action", "getPatrolsWithPeople"));
request.Parameters.AddParameter(new QueryParameter("sectionid", _options.SectionId.ToString()));
request.Parameters.AddParameter(new QueryParameter("termid", termId));
request.Parameters.AddParameter(new QueryParameter("include_no_patrol", includeNoPatrol ? "y" : "n"));

var response = await _client.ExecuteGetAsync(request);
if (string.IsNullOrEmpty(response.Content)) return new List<Patrol>();
var node = JsonNode.Parse(response.Content!);

var evaluateResult = JsonPath.Parse($"$[*]").Evaluate(node);

if (evaluateResult.Matches == null || !evaluateResult.Matches.Any()) return Array.Empty<Patrol>();
return evaluateResult.Matches.Select(match => JsonSerializer.Deserialize<Patrol>(match.Value.AsJsonString()))
.ToList();
}
}

0 comments on commit d8058cf

Please sign in to comment.