mirror of
https://github.com/tonytins/amtkstat.git
synced 2026-10-24 05:32:54 -04:00
Wrote most of the client functions
This commit is contained in:
parent
af3f658d99
commit
b920f7aa8c
1 changed files with 30 additions and 0 deletions
|
|
@ -14,6 +14,36 @@ struct AmtrakClient {
|
||||||
let trainsBaseURL = "https://api.amtraker.com/v3/trains"
|
let trainsBaseURL = "https://api.amtraker.com/v3/trains"
|
||||||
let staleBaseURL = "https://api.amtraker.com/v3/stale"
|
let staleBaseURL = "https://api.amtraker.com/v3/stale"
|
||||||
|
|
||||||
|
func fetchTrain(number trainid: String) async throws -> TrainResponse {
|
||||||
|
try await get("\(trainsBaseURL)/\(trainid)")
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchAllTrains() async throws -> TrainResponse {
|
||||||
|
try await get(trainsBaseURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchStation(code stationid: String) async throws -> StationResponse {
|
||||||
|
try await get("\(stationBaseURL)/\(stationid)")
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchAllStations() async throws -> StationResponse {
|
||||||
|
try await get(stationBaseURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func trainNumber(fromTrainId trainId: String) -> String {
|
||||||
|
trainId.split(separator: "-").first.map(String.init) ?? trainId
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchStationTrainNumbers(stationCode code: String) async throws -> [String] {
|
||||||
|
let reponse = try await fetchStation(code: code)
|
||||||
|
|
||||||
|
guard let meta = reponse[code] else {
|
||||||
|
throw AmtrakError.stationNotFound(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Array(Set(meta.trains.map(trainNumber(fromTrainId:))))
|
||||||
|
}
|
||||||
|
|
||||||
func get<T: Decodable>(_ urlString: String) async throws -> T {
|
func get<T: Decodable>(_ urlString: String) async throws -> T {
|
||||||
|
|
||||||
guard let url = URL(string: urlString) else {
|
guard let url = URL(string: urlString) else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue