From 907ff3d457ad337b2aeb0e8b493cb66fdd1e7299 Mon Sep 17 00:00:00 2001 From: Adrian Schoenig Date: Wed, 27 Apr 2022 11:42:08 +1000 Subject: [PATCH] Maintenance (#171) - Renamed from `TKServer.developmentServer` to `TKServer.customBaseURL` and added docs - Update Japanese translation --- .../TripKit/Resources/ja.lproj/Shared.strings | 4 +-- .../Resources/ja.lproj/TripKit.strings | 8 +++--- Sources/TripKit/server/TKServer+Regions.swift | 8 +++--- Sources/TripKit/server/TKServer.swift | 27 ++++++++++++++++--- Tests/TripKitUITests/TKUIGeocoderTest.swift | 4 +-- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/Sources/TripKit/Resources/ja.lproj/Shared.strings b/Sources/TripKit/Resources/ja.lproj/Shared.strings index 387499a91..3a04d012b 100644 --- a/Sources/TripKit/Resources/ja.lproj/Shared.strings +++ b/Sources/TripKit/Resources/ja.lproj/Shared.strings @@ -98,7 +98,7 @@ "Don't have an account? Sign up" = "アカウントを持っていますか・サインアップ"; /* Done action */ -"Done" = "行われます"; +"Done" = "実行"; /* Drop new pin */ "Drop new pin" = "新しいピンをドロップしてください"; @@ -200,7 +200,7 @@ "Not wheelchair accessible" = "車椅子対応不可"; /* Countdown cell 'now' indicator */ -"Now" = "今"; +"Now" = "現在時刻"; /* Ok action */ "OK" = "わかりました"; diff --git a/Sources/TripKit/Resources/ja.lproj/TripKit.strings b/Sources/TripKit/Resources/ja.lproj/TripKit.strings index 344b72f98..21659ab21 100644 --- a/Sources/TripKit/Resources/ja.lproj/TripKit.strings +++ b/Sources/TripKit/Resources/ja.lproj/TripKit.strings @@ -75,7 +75,7 @@ "Arrive at %@ at %@" = "%2$@に %1$@に到着"; /* Arrive before button */ -"Arrive by" = "到着します。"; +"Arrive by" = "到着"; /* Title for when you'll arrive when on a trip. Countdown to arrival will be displayed below. */ "Arrive in" = "到着します。"; @@ -198,7 +198,7 @@ "Get directions" = "道順します。"; /* Instruction to get a service towards provided destination */ -"Get on service to %@" = "%@までサービスを開始します"; +"Get on service to %@" = "%@まで"; /* Title of button to start a trip */ "Go" = "開始"; @@ -355,10 +355,10 @@ "Routing from %@ to %@ is not yet supported" = "%1$@ から %2$@ へのルートはまだサポートされていません"; /* Label to indicate that the time for a service is the scheduled time, i.e., displayed as on the timetable with no real-time data available. */ -"Scheduled" = "予定済み"; +"Scheduled" = "発着予定"; /* No comment provided by engineer. */ -"Search for destination" = "宛先を検索"; +"Search for destination" = "目的地を検索"; /* Error recovery suggestion for when when server could not find a given transit stop. */ "Search for this stop again or try again later.." = "この停止を再度検索または、後でもう一度試してください."; diff --git a/Sources/TripKit/server/TKServer+Regions.swift b/Sources/TripKit/server/TKServer+Regions.swift index 6032b3319..0669ec809 100644 --- a/Sources/TripKit/server/TKServer+Regions.swift +++ b/Sources/TripKit/server/TKServer+Regions.swift @@ -11,7 +11,7 @@ import Foundation extension TKRegionManager { enum RegionError: Error { - case invalidDevelopmentServer + case invalidCustomBaseURL } /// Fetched the list of regions and updates `TKRegionManager`'s cache @@ -47,9 +47,9 @@ extension TKRegionManager { private func fetchRegions(forced: Bool) async throws { let regionsURL: URL - if let developmentServer = TKServer.developmentServer { - guard let url = URL(string: developmentServer) else { - throw RegionError.invalidDevelopmentServer + if let customBaseURL = TKServer.customBaseURL { + guard let url = URL(string: customBaseURL) else { + throw RegionError.invalidCustomBaseURL } regionsURL = url.appendingPathComponent("regions.json") } else { diff --git a/Sources/TripKit/server/TKServer.swift b/Sources/TripKit/server/TKServer.swift index d11abe62e..517f8dbcf 100644 --- a/Sources/TripKit/server/TKServer.swift +++ b/Sources/TripKit/server/TKServer.swift @@ -14,7 +14,7 @@ extension TKServer { public static func imageURL(iconFileNamePart: String?, iconType: TKStyleModeIconType? = nil) -> URL? { guard let iconFileNamePart = iconFileNamePart else { return nil } - let regionsURLString = TKServer.developmentServer ?? "https://api.tripgo.com/v1" + let regionsURLString = TKServer.customBaseURL ?? "https://api.tripgo.com/v1" let isPNG: Bool let fileNamePrefix: String @@ -77,12 +77,19 @@ extension TKServer { return URL(string: urlString) } + @available(*, unavailable, renamed: "customBaseURL") public static var developmentServer: String? { + get { customBaseURL } + set { customBaseURL = newValue } + } + + /// Custom base URL to use, instead of hitting SkedGo's default servers directly. + public static var customBaseURL: String? { get { UserDefaults.shared.string(forKey: "developmentServer") } set { - let oldValue = developmentServer + let oldValue = customBaseURL if var newValue = newValue, !newValue.isEmpty { if !newValue.hasSuffix("/") { newValue.append("/") @@ -99,8 +106,10 @@ extension TKServer { } } + /// Base URL to use for server calls when `customBaseURL` is not set and the server calls + /// do not specify a ```TKRegion```. public static var fallbackBaseURL: URL { - developmentServer.flatMap(URL.init) ?? URL(string: "https://api.tripgo.com/v1/")! + customBaseURL.flatMap(URL.init) ?? URL(string: "https://api.tripgo.com/v1/")! } } @@ -125,7 +134,7 @@ extension TKServer { @objc // so that subclasses can override func baseURLs(for region: TKRegion?) -> [URL] { - if let dev = Self.developmentServer.flatMap(URL.init) { + if let dev = Self.customBaseURL.flatMap(URL.init) { return [dev] } else if let urls = region?.urls, !urls.isEmpty { return urls @@ -231,6 +240,7 @@ extension TKServer { return try dataResult.orThrow(ServerError.noData).get() } + /// :nodoc: - Public for Objective-C only @objc(GET:paras:completion:) public func _get(url: URL, parameters: [String: Any]? = nil, completion: @escaping (Int, [String: Any], Any?, Data?, Error?) -> Void) { hit(.GET, url: url, parameters: parameters) { status, headers, result in @@ -244,6 +254,7 @@ extension TKServer { } } + /// :nodoc: - Public for Objective-C only @objc(POST:paras:completion:) public func _post(url: URL, parameters: [String: Any]? = nil, completion: @escaping (Int, [String: Any], Any?, Data?, Error?) -> Void) { hit(.POST, url: url, parameters: parameters) { status, headers, result in @@ -262,9 +273,17 @@ extension TKServer { // MARK: - Async/await extension TKServer { + /// Captures server response with HTTP status code, headers and typed response public struct Response { + + /// HTTP status code of response. Can be `nil` if request failed. public var statusCode: Int? + + /// HTTP response headers. Can be empty if request failed. public var headers: [String: Any] + + /// Types response, which can encapsulate a failure if the server returned an error + /// or if the server's data couldn't be decoded as the appropriate type. public var result: Result } diff --git a/Tests/TripKitUITests/TKUIGeocoderTest.swift b/Tests/TripKitUITests/TKUIGeocoderTest.swift index d4ea5e4bd..8099c5def 100644 --- a/Tests/TripKitUITests/TKUIGeocoderTest.swift +++ b/Tests/TripKitUITests/TKUIGeocoderTest.swift @@ -21,8 +21,8 @@ class TKUIGeocoderTest: XCTestCase { override func setUpWithError() throws { try super.setUpWithError() - if TKServer.developmentServer != nil { - TKServer.developmentServer = nil // make sure to hit production + if TKServer.customBaseURL != nil { + TKServer.customBaseURL = nil // make sure to hit production } geocoder = try aggregateGeocoder()