-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduleService.swift
More file actions
106 lines (95 loc) · 3.46 KB
/
Copy pathScheduleService.swift
File metadata and controls
106 lines (95 loc) · 3.46 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
104
105
106
import Foundation
import PlayolaCore
import os.log
enum ScheduleService {
private static let logger = OSLog(
subsystem: "PlayolaPlayer",
category: "ScheduleService")
static func getSchedule(
stationId: String,
baseUrl: URL,
errorReporter: PlayolaErrorReporter = .shared
) async throws -> Schedule {
let url = baseUrl.appending(path: "/v1/stations/\(stationId)/schedule")
.appending(queryItems: [
URLQueryItem(name: "includeRelatedTexts", value: "true"),
URLQueryItem(name: "lockedIn", value: "true"),
])
do {
let (data, response) = try await URLSession.shared.data(from: url)
guard let httpResponse = response as? HTTPURLResponse else {
let error = StationPlayerError.networkError("Invalid response type")
Task {
await errorReporter.reportError(
error,
context: "Non-HTTP response received from schedule endpoint: \(url.absoluteString)",
level: .error)
}
throw error
}
guard (200...299).contains(httpResponse.statusCode) else {
let responseText = String(data: data, encoding: .utf8) ?? "Unable to decode response"
let error = StationPlayerError.networkError("HTTP error: \(httpResponse.statusCode)")
Task {
if httpResponse.statusCode == 404 {
await errorReporter.reportError(
error,
context: "Station not found: \(stationId) | Response: \(responseText.prefix(100))",
level: .error)
} else {
await errorReporter.reportError(
error,
context:
"HTTP \(httpResponse.statusCode) error getting schedule for station: \(stationId) | "
+ "Response: \(responseText.prefix(100))",
level: .error)
}
}
throw error
}
return try decodeSchedule(from: data, stationId: stationId, errorReporter: errorReporter)
} catch let error as StationPlayerError {
throw error
} catch {
Task {
await errorReporter.reportError(
error, context: "Failed to fetch schedule for station: \(stationId)", level: .error)
}
throw error
}
}
private static func decodeSchedule(
from data: Data, stationId: String, errorReporter: PlayolaErrorReporter
) throws -> Schedule {
let decoder = JSONDecoderWithIsoFull()
do {
let spins = try decoder.decode([Spin].self, from: data)
guard !spins.isEmpty else {
let error = StationPlayerError.scheduleError(
"No spins returned in schedule for station ID: \(stationId)")
Task {
await errorReporter.reportError(error, level: .error)
}
throw error
}
return Schedule(stationId: spins[0].stationId, spins: spins)
} catch let decodingError as DecodingError {
let context: String
switch decodingError {
case .dataCorrupted(let reportedContext):
context = "Corrupted data: \(reportedContext.debugDescription)"
case .keyNotFound(let key, _):
context = "Missing key: \(key)"
case .typeMismatch(let type, _):
context = "Type mismatch for: \(type)"
default:
context = "Unknown decoding error"
}
Task {
await errorReporter.reportError(
decodingError, context: "Failed to decode schedule: \(context)", level: .error)
}
throw StationPlayerError.scheduleError("Invalid schedule data: \(context)")
}
}
}