aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Sources.OpenTripPlannerGql/OpenTripPlannerClient.cs
blob: 453a03e727ac3230ac63cc9d2c0f04c19a9a3249 (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
41
42
43
44
45
46
47
using System.Net.Http.Json;
using Enmarcha.Sources.OpenTripPlannerGql.Queries;
using Microsoft.Extensions.Logging;

namespace Enmarcha.Sources.OpenTripPlannerGql;

public class OpenTripPlannerClient
{
    private readonly HttpClient _httpClient;
    private readonly string _baseUrl;
    private readonly ILogger<OpenTripPlannerClient> _logger;

    public OpenTripPlannerClient(
        HttpClient httpClient,
        string baseUrl,
        ILogger<OpenTripPlannerClient> logger
    )
    {
        _httpClient = httpClient;
        _baseUrl = baseUrl;
        _logger = logger;
    }

    public async Task GetStopsInBbox(double minLat, double minLon, double maxLat, double maxLon)
    {
        var requestContent =
            StopTileRequestContent.Query(new StopTileRequestContent.Bbox(minLon, minLat, maxLon, maxLat));

        var request = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}/gtfs/v1");
        request.Content = JsonContent.Create(new GraphClientRequest
        {
            Query = requestContent
        });

        var response = await _httpClient.SendAsync(request);
        var responseBody = await response.Content.ReadFromJsonAsync<GraphClientResponse<StopTileResponse>>();

        if (responseBody is not { IsSuccess: true })
        {
            _logger.LogError(
                "Error fetching stop data, received {StatusCode} {ResponseBody}",
                response.StatusCode,
                await response.Content.ReadAsStringAsync()
            );
        }
    }
}