|
| 1 | +using System; |
| 2 | +using AbeckDev.DbTimetable.Mcp.Models; |
| 3 | +using Microsoft.Extensions.Options; |
| 4 | + |
| 5 | +namespace AbeckDev.DbTimetable.Mcp.Services; |
| 6 | + |
| 7 | +public class TimeTableService |
| 8 | +{ |
| 9 | + |
| 10 | + private readonly HttpClient _httpClient; |
| 11 | + private readonly Configuration _config; |
| 12 | + |
| 13 | + public TimeTableService(HttpClient httpClient, IOptions<Configuration> config) |
| 14 | + { |
| 15 | + _httpClient = httpClient; |
| 16 | + _config = config.Value; |
| 17 | + } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Get recent timetable changes for a specific event number |
| 21 | + /// </summary> |
| 22 | + public async Task<string> GetRecentTimetableChangesAsync( |
| 23 | + string eventNo, |
| 24 | + CancellationToken cancellationToken = default) |
| 25 | + { |
| 26 | + using var request = new HttpRequestMessage(HttpMethod.Get, $"rchg/{eventNo}"); |
| 27 | + request.Headers.Add("DB-Client-Id", _config.ClientId); |
| 28 | + request.Headers.Add("DB-Api-Key", _config.ApiKey); |
| 29 | + request.Headers.Add("accept", "application/xml"); |
| 30 | + |
| 31 | + var response = await _httpClient.SendAsync(request, cancellationToken); |
| 32 | + |
| 33 | + if (!response.IsSuccessStatusCode) |
| 34 | + { |
| 35 | + var errorContent = await response.Content.ReadAsStringAsync(cancellationToken); |
| 36 | + throw new HttpRequestException( |
| 37 | + $"Deutsche Bahn API request failed with status {response.StatusCode}: {errorContent}"); |
| 38 | + } |
| 39 | + |
| 40 | + var content = await response.Content.ReadAsStringAsync(cancellationToken); |
| 41 | + return content; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Get station board (departures/arrivals) for a specific station |
| 46 | + /// </summary> |
| 47 | + public async Task<string> GetStationBoardAsync( |
| 48 | + string evaNo, |
| 49 | + DateTime? date = null, |
| 50 | + CancellationToken cancellationToken = default) |
| 51 | + { |
| 52 | + // Format: yyMMddHHmm (e.g., 2511051830 for 2025-11-05 18:30) |
| 53 | + var dateParam = date?.ToString("yyMMdd") ?? DateTime.UtcNow.ToString("yyMMdd"); |
| 54 | + var hourParam = date?.ToString("HH") ?? DateTime.UtcNow.ToString("HH"); |
| 55 | + |
| 56 | + using var request = new HttpRequestMessage(HttpMethod.Get, $"plan/{evaNo}/{dateParam}/{hourParam}"); |
| 57 | + request.Headers.Add("DB-Client-Id", _config.ClientId); |
| 58 | + request.Headers.Add("DB-Api-Key", _config.ApiKey); |
| 59 | + request.Headers.Add("accept", "application/xml"); |
| 60 | + |
| 61 | + var response = await _httpClient.SendAsync(request, cancellationToken); |
| 62 | + response.EnsureSuccessStatusCode(); |
| 63 | + |
| 64 | + return await response.Content.ReadAsStringAsync(cancellationToken); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Get full changes for a station at a specific time |
| 69 | + /// </summary> |
| 70 | + public async Task<string> GetFullChangesAsync( |
| 71 | + string evaNo, |
| 72 | + CancellationToken cancellationToken = default) |
| 73 | + { |
| 74 | + using var request = new HttpRequestMessage(HttpMethod.Get, $"fchg/{evaNo}"); |
| 75 | + request.Headers.Add("DB-Client-Id", _config.ClientId); |
| 76 | + request.Headers.Add("DB-Api-Key", _config.ApiKey); |
| 77 | + request.Headers.Add("accept", "application/xml"); |
| 78 | + |
| 79 | + var response = await _httpClient.SendAsync(request, cancellationToken); |
| 80 | + response.EnsureSuccessStatusCode(); |
| 81 | + |
| 82 | + return await response.Content.ReadAsStringAsync(cancellationToken); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Get information about stations given either a station name (prefix), eva number, ds100/rl100 code, wildcard (*); doesn't seem to work with umlauten in station name (prefix) |
| 87 | + /// </summary> |
| 88 | + public async Task<string> GetStationInformation( |
| 89 | + string pattern, |
| 90 | + CancellationToken cancellationToken = default) |
| 91 | + { |
| 92 | + using var request = new HttpRequestMessage(HttpMethod.Get, $"station/{pattern}"); |
| 93 | + request.Headers.Add("DB-Client-Id", _config.ClientId); |
| 94 | + request.Headers.Add("DB-Api-Key", _config.ApiKey); |
| 95 | + request.Headers.Add("accept", "application/xml"); |
| 96 | + |
| 97 | + var response = await _httpClient.SendAsync(request, cancellationToken); |
| 98 | + response.EnsureSuccessStatusCode(); |
| 99 | + |
| 100 | + return await response.Content.ReadAsStringAsync(cancellationToken); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments