aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Sources.Tussa/SantiagoRealtimeEstimatesProvider.cs
blob: 7437a0591f93068943143f390b5372210856aec0 (plain)
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
using System.Net.Http.Json;

namespace Enmarcha.Sources.Tussa;

public class SantiagoRealtimeEstimatesProvider
{
    private HttpClient _http;

    public SantiagoRealtimeEstimatesProvider(HttpClient http)
    {
        _http = http;
    }

    public async Task<List<SantiagoEstimate>> GetEstimatesForStop(int stopId)
    {
        var url = GetRequestUrl(stopId.ToString());

        var response = await _http.GetAsync(url);
        var maisbusResponse = await response.Content.ReadFromJsonAsync<MaisbusResponse>();

        if (maisbusResponse is null)
        {
            var responseString = await response.Content.ReadAsStringAsync();
            throw new Exception("Error parsing maisbus response: " + responseString);
        }

        return maisbusResponse.Routes.Select(r => new SantiagoEstimate
        (
            r.Id.ToString(),
            r.MinutesToArrive
        )).OrderBy(a => a.Minutes).ToList();
    }

    private static string GetRequestUrl(string stopId)
    {
        return $"https://tussa.gal/maisbus/api/stop/{stopId}";
    }
}

public record SantiagoEstimate(string RouteId, int Minutes);