-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetKeepers.cs
More file actions
103 lines (88 loc) · 4.64 KB
/
GetKeepers.cs
File metadata and controls
103 lines (88 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
namespace sleeper;
public static class GetKeepers
{
private static readonly HttpClient httpClient = new();
[Function("GetKeepers")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "keepers/{leagueId}")] HttpRequest req
, string leagueId
, FunctionContext context)
{
var corrolationId = context.TraceContext?.TraceParent?.ToString() ?? new Guid().ToString();
var log = context.GetLogger("GetKeepers");
log.LogInformation("[{corrolationId}] GetKeepers request received for leagueId: {leagueId}", corrolationId, leagueId);
// Construct the API URL using the leagueId
string url = $"https://api.sleeper.app/v1/league/{leagueId}/rosters";
try
{
// Make an HTTP GET request to the Sleeper API
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
// Read the response content as a string
string json = await response.Content.ReadAsStringAsync();
// Get our rosters
var rosters = JsonConvert.DeserializeObject<List<Roster>>(json) ?? throw new Exception("Error deserializing rosters from Sleeper API response");
// Get our owners
url = $"https://api.sleeper.app/v1/league/{leagueId}/users";
response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
// Read the response content as a string
json = await response.Content.ReadAsStringAsync();
var owners = JsonConvert.DeserializeObject<List<Owner>>(json) ?? throw new Exception("Error deserializing owners from Sleeper API response");
// Get all Rosters where Keeper count > 0
rosters = rosters.Where(r => r.Keepers != null && r.Keepers.Count > 0).ToList();
if (rosters == null || rosters.Count == 0)
{
string message = "No keepers found for leagueId: " + leagueId;
log.LogInformation("[{corrolationId}] {message}", corrolationId, message);
return new OkObjectResult(message);
}
// Get the players
url = $"https://api.sleeper.app/v1/players/nfl";
response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var players = JsonConvert.DeserializeObject<Players>(await response.Content.ReadAsStringAsync()) ?? throw new Exception("Error deserializing players from Sleeper API response");
// Return KeeperResult from the keepers with the owner and player names
var keeperResults = new List<KeeperResult>();
foreach (var roster in rosters)
{
var owner = owners.FirstOrDefault(o => o.OwnerId == roster.Owner_Id) ?? throw new Exception("Owner Id not found: " + roster.Owner_Id);
foreach (var playerId in roster.Keepers)
{
if (!players.ContainsKey(playerId))
{
throw new Exception("Player Id not found: " + playerId);
}
var player = players[playerId];
keeperResults.Add(new KeeperResult
{
Owner = owner.DisplayName,
TeamName = owner.TeamName,
Player = player.FirstName + " " + player.LastName,
Position = player.Position,
Team = player.Team
});
}
}
var results = new StringBuilder();
foreach (var keeper in keeperResults.OrderBy(k => k.Owner).ThenBy(k => k.Player))
{
results.AppendLine($"{keeper.Owner} ({keeper.TeamName}): {keeper.Player} ({keeper.Position} - {keeper.Team})");
}
// Return the response
return new OkObjectResult(results.ToString());
}
catch (Exception ex)
{
string message = $"Error fetching keeper data from Sleeper: {ex.Message}\n\nCorrolation Id: {corrolationId}";
log.LogError("[{corrolationId}] {message}", corrolationId, message);
return new ObjectResult(message) { StatusCode = StatusCodes.Status500InternalServerError };
}
}
}