Skip to content

Commit

Permalink
Multilanguage requests (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattes-bru authored Dec 9, 2024
1 parent 5adc849 commit 8c63091
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Sources/OpenWeatherKit/Internal/Route.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum Route {

switch self {
case let .availability(location): return "\(base)/api/v1/availability/\(location.latitude)/\(location.longitude)"
case let .weather(language, location): return "\(base)/api/v1/weather/\(language.languageCode)/\(location.latitude)/\(location.longitude)"
case let .weather(language, location): return "\(base)/api/v1/weather/\(language.rawValue)/\(location.latitude)/\(location.longitude)"
}
}()

Expand Down
21 changes: 9 additions & 12 deletions Sources/OpenWeatherKit/Public/WeatherService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ final public class WeatherService: Sendable {
/// Establishes the configuration for weather requests.
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *)
public struct Configuration: Sendable {
public enum Language: Sendable {
case englishUS

var languageCode: String {
switch self {
case .englishUS: return "en_US"
}
}

public enum Language: String, Sendable {
case englishUS = "en_US"
case germanDE = "de_DE"

}

public var jwt: @Sendable () -> String
Expand Down Expand Up @@ -153,8 +150,8 @@ final public class WeatherService: Sendable {
/// - Returns: The aggregate weather.
///
@inlinable
final public func weather(for location: LocationProtocol, countryCode: String) async throws -> Weather {
try await getWeather(location: location, countryCode: countryCode)
final public func weather(for location: LocationProtocol, countryCode: String, language: WeatherService.Configuration.Language? = nil) async throws -> Weather {
try await getWeather(location: location, countryCode: countryCode, language: language)
}

///
Expand Down Expand Up @@ -434,10 +431,10 @@ final public class WeatherService: Sendable {

extension WeatherService {
@usableFromInline
func getWeather(location: LocationProtocol, countryCode: String) async throws -> Weather {
func getWeather(location: LocationProtocol, countryCode: String, language: WeatherService.Configuration.Language? = nil) async throws -> Weather {
let proxy = try await networkClient.fetchWeather(
location: location,
language: Self.configuration.language,
language: language ?? Self.configuration.language,
queries: WeatherQuery<CurrentWeather>.current,
WeatherQuery<Forecast<MinuteWeather>?>.minute,
WeatherQuery<Forecast<HourWeather>>.hourly,
Expand Down
9 changes: 9 additions & 0 deletions Tests/OpenWeatherKitTests/OpenWeatherKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ final class OpenWeatherKitTests: XCTestCase {
longitude: 0
)
)

let _ = try await service.weather(
for: Location(
latitude: 0,
longitude: 0
),
countryCode: "US",
language: .germanDE
)
}

func testDataSet1() async throws {
Expand Down

0 comments on commit 8c63091

Please sign in to comment.