Skip to content

Commit aa245a0

Browse files
committed
change and comparison queries
1 parent 2d52cf1 commit aa245a0

4 files changed

Lines changed: 45 additions & 7 deletions

File tree

Sources/OpenWeatherKit/Internal/Models/APIWeather.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ struct APIWeather: Codable, Equatable {
1313
let forecastDaily: APIForecastDaily?
1414
let forecastHourly: APIForecastHourly?
1515
let forecastNextHour: APIForecastNextHour?
16+
let historicalComparisons: APIHistoricalComparisons?
1617
let weatherAlerts: APIWeatherAlerts?
18+
let weatherChanges: APIWeatherChanges?
1719

1820
enum CodingKeys: String, CodingKey {
1921
case currentWeather = "currentWeather"
2022
case forecastDaily = "forecastDaily"
2123
case forecastHourly = "forecastHourly"
2224
case forecastNextHour = "forecastNextHour"
25+
case historicalComparisons = "historicalComparisons"
2326
case weatherAlerts = "weatherAlerts"
27+
case weatherChanges = "weatherChanges"
2428
}
2529
}

Sources/OpenWeatherKit/Public/Requests/WeatherQuery.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public struct WeatherQuery<T> {
2020
public static var current: WeatherQuery<CurrentWeather> {
2121
WeatherQuery<CurrentWeather>(
2222
queryType: .current(APIWeather.CodingKeys.currentWeather.rawValue),
23-
result: { try $0.currentWeather
23+
result: {
24+
try $0.currentWeather
2425
.unwrap(or: WeatherError.missingData(APIWeather.CodingKeys.currentWeather.rawValue))
2526
}
2627
)
@@ -30,7 +31,8 @@ public struct WeatherQuery<T> {
3031
public static var minute: WeatherQuery<Forecast<MinuteWeather>?> {
3132
WeatherQuery<Forecast<MinuteWeather>?>(
3233
queryType: .minute(APIWeather.CodingKeys.forecastNextHour.rawValue),
33-
result: { try $0.minuteForecast
34+
result: {
35+
try $0.minuteForecast
3436
.unwrap(or: WeatherError.missingData(APIWeather.CodingKeys.forecastNextHour.rawValue))
3537
}
3638
)
@@ -44,7 +46,8 @@ public struct WeatherQuery<T> {
4446
Date(),
4547
Date.hoursFromNow(24)
4648
),
47-
result: { try $0.hourlyForecast
49+
result: {
50+
try $0.hourlyForecast
4851
.unwrap(or: WeatherError.missingData(APIWeather.CodingKeys.forecastHourly.rawValue))
4952
}
5053
)
@@ -58,7 +61,8 @@ public struct WeatherQuery<T> {
5861
Date(),
5962
Date.daysFromNow(10)
6063
),
61-
result: { try $0.dailyForecast
64+
result: {
65+
try $0.dailyForecast
6266
.unwrap(or: WeatherError.missingData(APIWeather.CodingKeys.forecastDaily.rawValue))
6367
}
6468
)
@@ -67,13 +71,25 @@ public struct WeatherQuery<T> {
6771
/// The weather changes query.
6872
@available(macOS 11, iOS 13, watchOS 6, tvOS 13, visionOS 1, *)
6973
public static var changes: WeatherQuery<WeatherChanges?> {
70-
preconditionFailure()
74+
WeatherQuery<WeatherChanges?>(
75+
queryType: .changes(APIWeather.CodingKeys.weatherChanges.rawValue),
76+
result: {
77+
try $0.weatherChanges
78+
.unwrap(or: WeatherError.missingData(APIWeather.CodingKeys.weatherChanges.rawValue))
79+
}
80+
)
7181
}
7282

7383
/// The weather historical comparison query.
7484
@available(macOS 11, iOS 13, watchOS 6, tvOS 13, visionOS 1, *)
7585
public static var historicalComparisons: WeatherQuery<HistoricalComparisons?> {
76-
preconditionFailure()
86+
WeatherQuery<HistoricalComparisons?>(
87+
queryType: .comparisons(APIWeather.CodingKeys.historicalComparisons.rawValue),
88+
result: {
89+
try $0.historicalComparisons
90+
.unwrap(or: WeatherError.missingData(APIWeather.CodingKeys.historicalComparisons.rawValue))
91+
}
92+
)
7793
}
7894

7995
#if canImport(CoreLocation)

Tests/OpenWeatherKitTests/Utils/MockClient.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ actor MockClient: Client {
7474
forecastDaily: include.contains(.daily) ? MockData.dailyWeather : nil,
7575
forecastHourly: include.contains(.hourly) ? MockData.hourlyWeather : nil,
7676
forecastNextHour: include.contains(.nextHour) ? MockData.nextHourWeather : nil,
77-
weatherAlerts: include.contains(.alerts) ? MockData.alerts : nil
77+
historicalComparisons: include.contains(.nextHour) ? MockData.historicalComparisons : nil,
78+
weatherAlerts: include.contains(.alerts) ? MockData.alerts : nil,
79+
weatherChanges: include.contains(.nextHour) ? MockData.changes : nil
7880
)
7981
}
8082
}

0 commit comments

Comments
 (0)