diff --git a/Sources/MasKit/Controllers/MasStoreSearch.swift b/Sources/MasKit/Controllers/MasStoreSearch.swift index ccdf959..324280d 100644 --- a/Sources/MasKit/Controllers/MasStoreSearch.swift +++ b/Sources/MasKit/Controllers/MasStoreSearch.swift @@ -30,9 +30,9 @@ class MasStoreSearch: StoreSearch { /// /// - Parameter appName: MAS app identifier. /// - Returns: URL for the search service or nil if appName can't be encoded. - static func searchURL(for appName: String) -> URL? { + static func searchURL(for appName: String) -> URL { guard var components = URLComponents(string: "https://itunes.apple.com/search") else { - return nil + fatalError("URLComponents failed to parse URL.") } components.queryItems = [ @@ -40,20 +40,28 @@ class MasStoreSearch: StoreSearch { URLQueryItem(name: "entity", value: "macSoftware"), URLQueryItem(name: "term", value: appName), ] - return components.url + guard let url = components.url else { + fatalError("URLComponents failed to generate URL.") + } + + return url } /// Builds the lookup URL for an app. /// /// - Parameter appId: MAS app identifier. /// - Returns: URL for the lookup service or nil if appId can't be encoded. - static func lookupURL(forApp appId: Int) -> URL? { + static func lookupURL(forApp appId: Int) -> URL { guard var components = URLComponents(string: "https://itunes.apple.com/lookup") else { - return nil + fatalError("URLComponents failed to parse URL.") } components.queryItems = [URLQueryItem(name: "id", value: "\(appId)")] - return components.url + guard let url = components.url else { + fatalError("URLComponents failed to generate URL.") + } + + return url } /// Searches for an app. @@ -62,11 +70,7 @@ class MasStoreSearch: StoreSearch { /// - Parameter completion: A closure that receives the search results or an Error if there is a /// problem with the network request. Results array will be empty if there were no matches. func search(for appName: String) -> Promise<[SearchResult]> { - guard let url = MasStoreSearch.searchURL(for: appName) - else { - return Promise(error: MASError.urlEncoding) - } - + let url = MasStoreSearch.searchURL(for: appName) return loadSearchResults(url) } @@ -76,11 +80,7 @@ class MasStoreSearch: StoreSearch { /// - Returns: A Promise for the search result record of app, or nil if no apps match the ID, /// or an Error if there is a problem with the network request. func lookup(app appId: Int) -> Promise { - guard let url = MasStoreSearch.lookupURL(forApp: appId) - else { - return Promise(error: MASError.urlEncoding) - } - + let url = MasStoreSearch.lookupURL(forApp: appId) return loadSearchResults(url).map { results in results.first } } diff --git a/Sources/MasKit/Errors/MASError.swift b/Sources/MasKit/Errors/MASError.swift index 4fc0648..716f4ea 100644 --- a/Sources/MasKit/Errors/MASError.swift +++ b/Sources/MasKit/Errors/MASError.swift @@ -27,7 +27,6 @@ public enum MASError: Error, Equatable { case notInstalled case uninstallFailed - case urlEncoding case noData case jsonParsing(error: NSError?) } @@ -91,9 +90,6 @@ extension MASError: CustomStringConvertible { case .uninstallFailed: return "Uninstall failed" - case .urlEncoding: - return "Unable to encode service URL" - case .noData: return "Service did not return data" diff --git a/Tests/MasKitTests/Controllers/MasStoreSearchSpec.swift b/Tests/MasKitTests/Controllers/MasStoreSearchSpec.swift index 0542f2c..b5007a6 100644 --- a/Tests/MasKitTests/Controllers/MasStoreSearchSpec.swift +++ b/Tests/MasKitTests/Controllers/MasStoreSearchSpec.swift @@ -19,22 +19,16 @@ public class MasStoreSearchSpec: QuickSpec { describe("url string") { it("contains the app name") { let appName = "myapp" - let urlString = MasStoreSearch.searchURL(for: appName)?.absoluteString + let urlString = MasStoreSearch.searchURL(for: appName).absoluteString expect(urlString) == "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appName)" } it("contains the encoded app name") { let appName = "My App" let appNameEncoded = "My%20App" - let urlString = MasStoreSearch.searchURL(for: appName)?.absoluteString + let urlString = MasStoreSearch.searchURL(for: appName).absoluteString expect(urlString) == "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appNameEncoded)" } - // Find a character that causes addingPercentEncoding(withAllowedCharacters to return nil - xit("is nil when app name cannot be url encoded") { - let appName = "`~!@#$%^&*()_+ 💩" - let urlString = MasStoreSearch.searchURL(for: appName)?.absoluteString - expect(urlString).to(beNil()) - } } describe("store") { context("when searched") { diff --git a/Tests/MasKitTests/Errors/MASErrorTestCase.swift b/Tests/MasKitTests/Errors/MASErrorTestCase.swift index ed53777..cdefffd 100644 --- a/Tests/MasKitTests/Errors/MASErrorTestCase.swift +++ b/Tests/MasKitTests/Errors/MASErrorTestCase.swift @@ -116,11 +116,6 @@ class MASErrorTestCase: XCTestCase { XCTAssertEqual(error.description, "Uninstall failed") } - func testUrlEncoding() { - error = .urlEncoding - XCTAssertEqual(error.description, "Unable to encode service URL") - } - func testNoData() { error = .noData XCTAssertEqual(error.description, "Service did not return data")